nanospeck
nanospeck

Reputation: 3548

How to create and assert a Mockito list of custom object?

I'm trying to simplify a test case 1st code block to something similar shown in the 2nd code block. I have two questions:

  1. Can I mock a list of custom objects?
  2. Can I assert a mocked list of custom objects with the fields in a returned list of objects?

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

Answers (1)

glytching
glytching

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

Related Questions