Reputation: 489
I am trying mockito with Junit for the first time. I have written test method to test students details. Below is code, Please help me in understanding better, I have created studentService Mock object, and calling it go get student details. I am getting test passed, But I am not sure whether I am doing right or not
@mock
StudentService client;
@Test
public void testGetStudentDetails() throws Exception {
Student student= new Student()
student.setCustomerId("123");
student.setRId("234");
student.setClassNumber("100");
Mockito.when(client.getStudentDetails(new Long(123), "1234")).thenReturn(student);
Student sd=client.getStudentDetails(new Long(123), "1234");
assertNotNull(sd);
}
Upvotes: 0
Views: 880
Reputation: 81862
This test doesn't make much sense right now.
In your setup you tell the mock how to behave:
Mockito.when(client.getStudentDetails(new Long(123), "1234")).thenReturn(student);
And in your actually test you call the mock, and check that it did what you told it to do:
Student sd=client.getStudentDetails(new Long(123), "1234");
assertNotNull(sd);
So you are only testing Mockito, which I assume is not what you intended.
Assuming you want to test getStudentDetails
you don't need Mockito for this as far as we can tell, just create a client, call the method and check that the thing happens, that is supposed to happen.
You will only mock things used by the object you want to test. (*) Since so far we see only the class you want to test and another class which is used, but doesn't need mocking since you simply created it, there is no need for mocking.
(*) This is a oversimplification, but will do for the beginning
Upvotes: 4