I am trying to unit test for structure of the object, I am making call to method getResults() and it returns me an object has properties like:
double mathMarks, double scienceMarks, double historyMarks, now how do I test the returned object’s structure in my Junit class?
i have vo where double mathMarks, double scienceMarks, double historyMarks are defined, I am not sure how can I check the structure of the returned object in my test class? Any suggestions?
In Java you would normally have an interface or a common super class which specifies the required members. You could then test with
assertTrue(a instanceof SomeClass)Note that this test fails even if the returned objects has the required members but doesn’t implement/extend SomeClass.If you want/have to work with fields, you can use reflection to check for the presence of the fields and there type.
A third alternative would be to use a language like groovy for your test. It would allow you to write code accessing the code, no matter if they are actually there or not. At least I think this should be possible, but since I never actually worked with groovy I can’t provide any details.
In order to choose the correct approach, the question is: how does the client code access the fields/members. Your tests should use the same approach.