Testing domain usually implies testing business methods which in turns may call BO factories methods. Depending if your domain needs a database setted up or not your test cases can be as easy as inheriting from BasicDomainTestCase or DomainTestCase.
If your Domain test cases doesn't need a test database set up you can write your business logic test simply inheriting from BasicDomainTestCase, as in the following example:
import net.smartlab.web.test.*;
public class MyDomainTest extends BasicDomainTestCase {
public void testMyMethod {
// your code here
}
}The previoues example may need the presence of the file res/smartweb.jar.xml which will be used to configure your Domain instance in case it needs a configuration file. If you need to initialize your Domain with a different file you can simply re-implement the getDomainConfiguration method, as follows:
import net.smartlab.web.test.*;
public class MyDomainTest extends BasicDomainTestCase {
protected URL getDomainConfiguration() throws Exception {
return new File("your_domain_test_config_file").toURL();
}
public void testMyMethod {
// your code here
}
}If your Domain test cases needs a test database set up you can write your business logic test simply inheriting from DomainTestCase, as in the following example:
import net.smartlab.web.test.*;
public class MyDomainTest extends DomainTestCase {
protected String getDataSetFile() {
return "myDbUnitDataSetFile";
}
public void testMyMethod {
// your code here
}
}The previoues example needs the presence of the file res/smartweb.jar.hcf to initialize the Hibernate SessionFactory plus it may need the presence of the file res/smartweb.jar.xml which will be used to configure your Domain instance in case it needs a configuration file. If you need to initialize your Hibernate SessionFactory or Domain with different files you can simply re-implement the getFactoryConfiguration and getDomainConfiguration methods, as follows:
import net.smartlab.web.test.*;
public class MyDomainTest extends DomainTestCase {
protected String getDataSetFile() {
return "myDbUnitDataSetFile";
}
protected URL getDomainConfiguration() throws Exception {
return new File("your_domain_test_config_file").toURL();
}
protected URL getFactoryConfiguration() throws Exception {
return new File("your_hibernate_test_config_file").toURL();
}
public void testMyMethod {
// your code here
}
}If for any reason you can't extend the BasicDomainTestCase or DomainTestCase base classes you can still take advantage of the testing library using the DomainTestSupport class, as in the following example:
import net.smartlab.web.test.*;
import junit.framework.*;
public class MyFactoryTest extends TestCase {
protected void setUp() throws Exception {
DomainTestSupport support =
new DomainTestSupport("your_domain_test_config_file");
Domain.setConfigurationStrategy(support);
// Following code is needed only if you need a database setted up
BusinessObjectFactoryTestSupport support =
new BusinessObjectFactoryTestSupport("your_hibernate_test_config_file");
BusinessObjectFactory.setConfigurationStrategy(support);
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
// Needed only if you setted up the database
BusinessObjectFactory.close();
}
public void testMyMethod {
// your code here
}
}Obviously if you still need to setup a testing database you have to follow the DbUnit process for database testing with or without inheriting the DbUnit hierarchy, but this can became a complex process, exactly the process we are trying to hide through our classes!