0

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?
    }
2
  • The only thing thats worth mocking here is opusPackageRepo. Whats the point of mocking a list here Commented Oct 2, 2017 at 11:39
  • I think the answer below by @glitch was what I was looking for - "Your test does not need to know about the contents of this List" Commented Oct 3, 2017 at 2:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.