Reputation: 3548
I'm trying to simplify a test case 1st code block to something similar shown in the 2nd code block. I have two questions:
Is something similar achieveable?
@Test
public void testStudentSuccess() {
Student st = new Student();
st.setRollNumber("1234");
st.setFeeAmount("1000");
List<Student> stList = new ArrayList<Student>();
stList.add(st);
Map<String, Object> simpleJdbcCallResult = new HashMap<String, Object>();
simpleJdbcCallResult.put("X_GET_DATA", stList);
when(simpleJdbcCall.getSimpleJdbcCall(any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(String.class), any(String.class), any(String.class),
any(StudentRowMapper.class))).thenReturn(simpleJdbcCallResult);
List<Student> studentList = opusPackageRepo.getPolicyStudent("123", "123");
Student student = studentList.get(0);
assertEquals(student.getRollNumber(), "1234");
assertEquals(student.getFeeAmount(), "1000");
}
To something like this (see the comments -> //):
@Test
public void testStudentSuccess() {
List<Student> stList = Mockito.mock(new ArrayList<Student>()); // 1. IS SOMTHING SIMILAR TO THIS POSSIBLE?
Map<String, Object> simpleJdbcCallResult = new HashMap<String, Object>();
simpleJdbcCallResult.put("X_GET_DATA", Mockito.mock(new ArrayList<Student>()));
when(simpleJdbcCall.getSimpleJdbcCall(any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(String.class), any(String.class), any(String.class),
any(StudentRowMapper.class))).thenReturn(simpleJdbcCallResult);
List<Student> studentList = opusPackageRepo.getPolicyStudent("123", "123");
assertEquals(studentList, stList); // 2. CAN I ASSERT EQUALS A MOCK WITH A RETURNED LIST?
}
Upvotes: 0
Views: 1848
Reputation: 47935
In the original test in your OP you are creating an expected result for the simpleJdbcCall.getSimpleJdbcCall()
as follows:
Student st = new Student();
st.setRollNumber("1234");
st.setFeeAmount("1000");
List<Student> stList = new ArrayList<Student>();
stList.add(st);
Map<String, Object> simpleJdbcCallResult = new HashMap<String, Object>();
simpleJdbcCallResult.put("X_GET_DATA", stList);
Then you assert that the result (which opusPackageRepo.getPolicyStudent()
must access via the "X_GET_DATA"
key) matches your expectation:
Student student = studentList.get(0);
assertEquals(student.getRollNumber(), "1234");
assertEquals(student.getFeeAmount(), "1000");
You could simplfy this without any need to think about mocks ...
List<Student> expected = new ArrayList<Student>();
Map<String, Object> simpleJdbcCallResult = new HashMap<String, Object>();
simpleJdbcCallResult.put("X_GET_DATA", expected);
...
List<Student> actual = opusPackageRepo.getPolicyStudent("123", "123");
assertSame(expected, actual);
This verifies that the result returned by getPolicyStudent
is the same as the list returned by the simpleJdbcCall.getSimpleJdbcCall()
invocation.
Your test does not need to know about the contents of this List since your test is just concerned with verifying that the List returned by simpleJdbcCall.getSimpleJdbcCall()
is correctly passed on by getPolicyStudent
reading it from this key: "X_GET_DATA"
. I think this is what you were hinting at when you wrote "simplify a test case" and it can be easily done but it does not need any mocking.
Upvotes: 1