Showing posts with label unitils. Show all posts
Showing posts with label unitils. Show all posts

Thursday, February 24, 2011

Finding a file in Unit Tests

A common problem is if you need to access a file in a JUnit test in Eclipse and also in the unit tests run in Hudson.
For Eclipse, if you do

File dir = new File(".");

this directory will point to the root directory of the current project.
So all relative paths should refer to this root directory.

In Hudson, the mapping is not quite the same.


So in my Unit tests I use:

import org.unitils.core.Unitils;
import org.unitils.dbunit.datasetfactory.DataSetResolver;
import org.unitils.dbunit.datasetfactory.impl.DefaultDataSetResolver;

        static DataSetResolver datasetResolver;

 @BeforeClass
 public static void initUnitiles() {
  Unitils unitilsInstance = Unitils.getInstance();
  unitilsInstance.init();
  datasetResolver = new DefaultDataSetResolver();
  datasetResolver.init(unitilsInstance.getConfiguration());
 }

@Test
   public void testIcoli() {

        File file = datasetResolver.resolve(MyTest.class, "sampleFile.xml");


}

and I place the sampleFile.xml in the same directory and the MyTest class...

Go look in the DefaultDataSetResolver code to see how the magic is done... it's quite easy anyway, just do

URL myURL = MyTestClass.getResource("myfile.xml");
File myFile = toFile(myURL);

(where toFile is an Apache Commons function).

Thursday, February 17, 2011

DataSet annotation in Unitils

a cool feature is to initialize the DB before each test.

Since most data are common to all tests, it pays to factor them out to a shared DataSet (xml file)

Unitils supports this feature and this is the syntax:

@DataSet(value={"one.xml","two.xml"})

http://www.unitils.org/apidocs/org/unitils/dbunit/annotation/DataSet.html

Wednesday, February 16, 2011

JpaUnitils.assertMappingWithDatabaseConsistent() is your friend

This baby detects the differences between your schema and your entities, and generates the DDL to adjust your schema to the Entities (of course if you use a schema-first approach you would run the DDL on the Entities, not on the schema!)


Priceless!

Funnily enough, I have discovered that running the same tests using different versions of Oracle DB (Oracle XE and the "real" Oracle DB) can give different results concerning the DB Metadata, speficically for the error:

junit.framework.AssertionFailedError: Found mismatches between Java objects and database tables. Applying following DDL statements to the database should resolve the problem:

(list of DDL commands follows)


Tuesday, February 15, 2011

Unitils: @InjectIntoByType and @TestedObject

@TestedObject
AlertService alertService = new AlertService();

@InjectIntoByType(target="alertService")
Mock mockSchedulerService;
@InjectIntoByType(target="alertService")
Mock mockMessageService;

the target="alertService" is not required, since there is only one TestedObject.

instead of passing the Mock objects in the AlertService constructor, we can simply inject them using InjectIntoByType.

What I love is that no XML is required. XML sucks because it's too loosely connected with code.

See AlertServiceAnnotatedTest in http://javatoolsforweblogic.googlecode.com/files/TestUnitilsV2.zip for a working example.

Again, it's a pain to get all the required jars in the classpath... welcome to Java world :o(

Friday, February 11, 2011

Unitils mocks: easy and cool

I am not a fan of EasyMock, I think mockito is far superior.

Yet I found Unitils mocks - which are based on EasyMock - quite easy to use.

Here http://javatoolsforweblogic.googlecode.com/files/TestUnitils.zip a sample Eclipse project to illustrate the mocks - it's basically takes from Unitils tutorial http://unitils.org/tutorial.html


The only hassle is putting together all the required jars (adjust .classpath file)