Mockito / Junit5 org.opentest4j.AssertionFailedError for getter()

12,598

Sorry your testing code is really too complex.
It describes too finely the flow of invocations of the objects manipulated in the method under test.
Besides you mock many things : dependencies, parameters of the method under tests.
At last you assign to the variable referencing the mocked parameter the return of the method under test. It makes things really unclear.

A test has to be straight understandable and that is not.
It took me about 5 mn to understand the error cause. It is a lot for such a simple code.

This assertion failure :

Assertions.assertNotNull( myentityBmock.getMyentityA() );  // --> org.opentest4j.AssertionFailedError: expected: not <null>

is due to the fact that in the tested method getMynEntityA() can only be null as myentityBmock is a mock and setting a field as you do here will have no effect on the real field and the associated getter behavior :

if ( myentityA != null ) {
    entity.setMyentityA( myentityA ); // here you invoke a mocked method.
    entity.setModuser( "root" );
    return myentityBdao.save( entity );
}

In fact with your way of testing that mainly describes the invocation flow of the tested method, that is enough :

mockito.verify( myentityBmock ).setMyentityA( myentityAArgument.capture() );

as you cannot test the side effect of setMyentityA().

But really I strongly advise you to unit test your method without mocking the parameter of the tested method.
It could look like :

@Test
public void setMyentityAOfTeIfNullByLanr17() {
    MyentityB entityB = new MyentityB(...) ;
    entityB.setLanr7( "1234567" );
    final MyentityA myentityA = new MyentityA();
    mockito.when( myentityAServiceImplmock.findMyentityAByLanr17( entityB.getLanr7() ) ).thenReturn( myentityA );
    mockito.when( myentityBdaomock.save(entityB) ).thenReturn(entityB);

    // action
    MyentityB entityActualB = teServiceImpl.setMyentityAOfTeIfNullByLanr17(entityB);

    // Perform content/logic assertion and no flow assertion :
    Assertions.assertEquals(myEntityA, entityActualB.getMyEntityA());
    Assertions.assertEquals("root", entityActualB.getModuser());
}

No tested code at all but it should help you to understand my intention.

Share:
12,598

Related videos on Youtube

Horst Rothenholzer
Author by

Horst Rothenholzer

Updated on June 04, 2022

Comments

  • Horst Rothenholzer
    Horst Rothenholzer almost 2 years

    If I make a JUnit Mockito test in the following method, it fails with org.opentest4j.AssertionFailedError: expected: not null. But the Mockito-(Parameter-)Test before with verify() and Assertions.assertNotNull() is ok. What am I doing wrong or what I don't understand? Here's my code:

    JUnit5/mock-Test:

    @Mock
    private MyentityAServiceImpl myentityAServiceImplmock;
    @Captor
    private ArgumentCaptor<MyentityA> myentityAArgument;
    @Mock
    private MyentityBdao myentityBdaomock;
    @Mock
    private MyentityB myentityBmock;
    @Mock
    private Logger loggermock;
    @InjectMocks
    private MyentityBServiceImpl teServiceImpl;
    
    @Test
    public void setMyentityAOfTeIfNullByLanr17() {
        myentityBmock.setLanr7( "1234567" );
        final MyentityA myentityA = new MyentityA();
        mockito.when( myentityAServiceImplmock.findMyentityAByLanr17( myentityBmock.getLanr7() ) ).thenReturn( myentityA );
        mockito.when( myentityBdaomock.save( myentityBmock ) ).thenReturn( myentityBmock );
        myentityBmock = teServiceImpl.setMyentityAOfTeIfNullByLanr17( myentityBmock );
        mockito.verify( myentityBmock ).setMyentityA( myentityAArgument.capture() );
        Assertions.assertNotNull( myentityAArgument );
        Assertions.assertNotNull( myentityBmock.getMyentityA() );  // --> org.opentest4j.AssertionFailedError: expected: not <null>
    }
    

    Method to test:

      public MyentityB setMyentityAOfTeIfNullByLanr17( final MyentityB entity ) {
        MyentityA myentityA = entity.getMyentityA();
        if ( myentityA != null ) {
            return entity;
        }
        final String lanr17 = entity.getLanr7();
        myentityA = myentityAServiceImpl.findMyentityAByLanr17( lanr17 );
        if ( myentityA != null ) {
            entity.setMyentityA( myentityA );
            entity.setModuser( "root" );
            return myentityBdao.save( entity );
        }
        return entity;
    }
    
  • Horst Rothenholzer
    Horst Rothenholzer over 5 years
    It works now. As you said, too many things were mixed and mocked which make it too complicated. I understand my error now - thanks!
  • davidxxx
    davidxxx over 5 years
    You are welcome. Glad that you grasped the overall idea.
  • GhostCat
    GhostCat over 5 years
    I see what you mean ;-)