lundi 16 février 2015

JUnit test for complex Java Models

i want to learn/know how can i test complex java classes with JUnit.


I have a java project, where i'm reading the content from a properties file , then i parse the content of the file to java objects. The properties file has a tree structure, therefore i have implement the Java Model likewise the composite pattern. enter image description here


So, when i want to write unit tests for the composite class or for any class of the project, how should i write those tests? How should the test look like? Should it look like this?



@Test
public void testComposition()
{
// Create a object of the Composite class, what you expect after reading
// from the file
Composite expectedObject = new Composite();
...

// Call the parser class of the file which returns a composite
Composite createdObject = PropertiesFileParser.parse(file);

// and then i have to assert the 2 object with a method from Assert
// e.g.
assertEqual(createdObject, expectedObject);
}


Do i have to create the expected Composite object expectedObject "by hand"? Or is there any other method to do things like that?


Are there some coding conventions in writing Junit test cases?


I'm grateful for every helpful answer! (Please excuse my english)


Aucun commentaire:

Enregistrer un commentaire