javajunitmockito

@Mock and assertNotNull(mock)


Does it make sense to write assertNotNull(mockObject) in header of test method body? When is a null check necessary for a mocked object?

@ExtendWith(MockitoExtension.class)
class SomeTest {
    @Mock
    private SomeClass mockObject;

    @Test
    void testMethod() {
        assertNotNull(mockObject);
        // other assertions...
    }
}

I tried assertNotNull(mockObject) in my project, and it successfully passes the JUnit5 test assertion.


Solution

  • From my experience, it does not make much sense to check the Mock of an object. That way you are checking if the framework actually works instead of any functionality (as Andrew S). In short, sure you can use it but it is just redundant code (I never use it). Also, I took a look at the website that you referenced and I think the AssertNotNull is used as a poor example there.