Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

@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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading