NoSQL Zone is brought to you in partnership with:

Alex Soto discovered Java during second year of his studies in computer science and wrote his Master's Thesis on Emergent Intelligence applied to Data Mining, winning the second prize of Hermano Salvador González contest. During his 11 years of software development, Alex has worked in several companies as technical consultant, and currently is working as technical analyst at a major international pharmaceutical company developing in-vitro medical software meeting FDA standards, where performance and safety is "a must". Alex also works as tutor at Open University of Catalonia (UOC), teaching Compilers course. He spends his free time to write in his blog (http://www.lordofthejars.com) where some posts are published in sites like dzone, theserverside and springsource. Alex has spoken at Devoxx 2011 and Linuxtag 2012. Currently is developing NoSQLUnit, a JUnit extension for testing NoSQL systems. Alex is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

NoSQLUnit 0.6.0 Released!

11.04.2012
| 2882 views |
  • submit to reddit

 NoSQLUnit is a JUnit extension to make writing unit and integration tests of systems that use NoSQL backend easier. Visit official page for more information.

In 0.6.0 release, one new NoSQL system is supported and is HBase. Apache HBase is an open-source, distributed, versioned, column-oriented store. As all databases supported by NoSQLUnit, two set of rules are provided to write HBase tests: First set of JUnit Rules are those responsible of managing database lifecycle; basically starting and stopping HBase instance.
  • Embedded: com.lordofthejars.nosqlunit.hbase.EmbeddedHBase
  • Managed: com.lordofthejars.nosqlunit.hbase.ManagedHBase
Depending on the kind of tests you are implementing (unit test, integration test, deployment tests, …) you will require an embedded, managed or remote approach. Note that for now I haven't implemented an In-Memory approach because there is no in-memory HBase  instance, but embedded strategy for unit tests will be the better one.  Second set of rules are those responsible of maintaining database into known state;
  • NoSQLUnit Management: com.lordofthejars.nosqlunit.hbase.HBaseRule
And finally default dataset file format in HBase is json. Dataset in HBase is the same used by Cassandra-Unit but not all fields are supported. Only fields available in TSV HBase application can be set into dataset. We will use a very simple example used in HBase tutorial as an example of how to write unit tests for systems that uses HBase database as backend. First of all, dataset used to maintain HBase into known state:
{
    "name" : "person",
    "columnFamilies" : [{
        "name" : "personFamilyName",
        "rows" : [{
            "key" : "john",
            "columns" : [{
                "name" : "age",
                "value" : "22"
            },
            {
                "name" : "car",
                "value" : "toyota"
            }]
        },
        {
            "key" : "mary",
            "columns" : [{
                "name" : "age",
                "value" : "33"
            },
            {
                "name" : "car",
                "value" : "ford"
            }]
        }]
    }]
}
and finally the test case:
public class WhenPersonWantsToKnowItsCar {

	@ClassRule
	public static EmbeddedHBase embeddedHBase = newEmbeddedHBaseRule().build();
	
	@Rule
	public HBaseRule hBaseRule = newHBaseRule().defaultEmbeddedHBase(this);
	
	@Inject
	private Configuration configuration;
	
	
	@Test
	@UsingDataSet(locations="persons.json", loadStrategy=LoadStrategyEnum.CLEAN_INSERT)
	public void car_should_be_returned() throws IOException {

		PersonManager personManager = new PersonManager(configuration);
		String car = personManager.getCarByPersonName("john");
		
		assertThat(car, is("toyota"));		
	}
	
}
Stay in touch with the project and of course I am opened to any ideas that you think that could make NoSQLUnit better.



Published at DZone with permission of Alex Soto, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)