1

I have function like following to generate an object of provided type from any payload:

public static <T> Optional<T> generateObject(String payloadJson, Class<T> type) {
        T objectPayload = null;
        try {
            objectPayload = objectMapper.readValue(payloadJson, type);
        }catch (IOException e) {
            log.info(e.getMessage());
        }
        return Optional.ofNullable(objectPayload);
    }

Had written the test case as follows

   @Test
    void generateObject() throws Exception {

        Mockito.when( eventUtil.generateObject("a", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
        Mockito.<Optional<Object>>when( eventUtil.generateObject("b", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
        Optional<Object> o2 = eventUtil.generateObject("b", Mockito.any(Class.class));
        assertEquals( "lol", o2.get() );
        Mockito.verify(eventUtil);

    }

The test execution is failing on Mockito.when statement with

java.lang.IllegalArgumentException: Unrecognized Type: [null]

**Note: I have tried providing null, another model class and even the parent class itself with thenReturn( Optional.of()) with no luck. Please point me to the correct direction in this case.

Codewrapper
  • 420
  • 3
  • 12
  • 2
    Why are you both mocking *and* calling the `generateObject`, what are you trying to test? – luk2302 Jul 06 '20 at 15:45
  • This is to check if this utility method behave as you expect in case the objectMapper throws exception or just works fine. – Codewrapper Jul 07 '20 at 02:55
  • 1
    No, it is not. Your test does nothing right now, it would always fail because the assert does not make any sense. And the entire test setup does not make any sense. You mock the call you then make, there is no point in it. Please delete the test and start from scratch, what dependency / 3rd party call do you want to mock? What method do you want to test, for what input? You should never mock anything on the unit under testing because at the end of the day you will end up with a test that tests that your test is written correctly, not that your code actually works. – luk2302 Jul 07 '20 at 07:10

2 Answers2

0

As explained Why doesn't Mockito mock static methods?

you cannot mock static method by using mockito. Anyway I guess your goal is to check if this utility method behave as you expect in case the objectMapper throws exception or just works fine. So you can make the method non static, then you can inject the objectMapper and try something like:

@Test
    void objectMapperThrowsException() throws Exception {
        ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
        EventUtil eventUtil = new EventUtil(mapper);
        Mockito.when(mapper.readValue(any(),any())).thenThrow(IOException.class);
        Optional<Object> result = eventUtil.generateObject("b", Object.class);
        assertTrue(result.isEmpty());
    }

and the positive case

@Test
        void objectMapperReturnAValidObject() throws Exception {
            Object anObject = new Object();
            ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
            EventUtil eventUtil = new EventUtil(mapper);
            Mockito.when(mapper.readValue(any(),any())).thenReturn(anObject);
            Optional<Object> result = eventUtil.generateObject("b", Object.class);
            assertFalse(result.isEmpty());
            assertEquals(anObject, result.get());
        }
Raffaele
  • 461
  • 1
  • 7
  • 20
0

The best method to test this method is to convert any data type to JSON and call the method to convert to the original one, try to use a helper class or any other data type

public class TargetTest {

   @Test
    void generateObject() throws Exception {
         // Arrange
         ObjectMapper mapper = new ObjectMapper();
         Helper helper = new Helper();
         helper.setName("test");
         String json = mapper.writeValueAsString(helper );

         // Act
         Helper actual = EventUtil.generateObject(json, Helper.class);

         // Assert
    }

    class Helper {
        private String name;
        
        // Getter and setter
    }
}

, If you want to mock it, try to use PowerMockito

0xh3xa
  • 4,801
  • 2
  • 14
  • 28