Reputation: 361
I am new to Junit. I am trying to follow TDD. My task is to write a test case for a new method. My method's signature is public Message getMessage(String id)
The responsibility getMessage(String id)
is to take messageId as parameter and query DB and populate Message object from resultSet and return.
Message Bean has four members
My confusion is :
Upvotes: 1
Views: 3343
Reputation: 167
When I have some complex object as "expected", I use Spring to build it.
For example:
Entity class:
public class Person {
private long id;
private String name;
private int age;
// getters, setters, constructors goes here
// ! IMPORTANT ! equals and toString must be implemented properly.
}
Method to test:
public class SomeClass {
public static Person getPerson(long id) {
// return some real Person object from database
}
}
And to write my "expected" object I use Spring:
<bean id="person" class="Person">
<property name="id" value="1">
<property name="name" value="John">
<property name="age" value="42">
</bean>
Then in Test method:
Person expected = springContext.getBean("person", Person.class);
Person actual = SomeClass.getPerson(1);
assertEquals(expected, actual);
If you don't have implemented getPerson
yet, you can use Something like Mockito to mock this method and return dumm object, which can be constructed also with Spring.
Upvotes: 1
Reputation: 285
There are two ways.
If you want to test data access layer code within this method, use DBUnit. You need to insert sample data first and then query it using this function. Once test is done, remove the sample data.
If you just want to check business logic. Then you can mock all method call within getMessage
method using powermock or easymock. And test this method only. Check powermock here
https://github.com/jayway/powermock
Upvotes: 1