mockito/junit5 org.opentest4j.assertionfailederror

vwkv1x7d  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(854)

如果我在下面的方法中进行junit mockito测试,它将失败,org.opentest4j.assertionfailederror:expected:notnull。但是使用verify()和assertions.assertnotnull()之前的mockito-(parameter-)测试是可以的。我做错了什么?我不明白什么?这是我的密码:
junit5/模拟测试:

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

测试方法:

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;
}
dbf7pr2w

dbf7pr2w1#

抱歉,你的测试代码太复杂了。
它过于精细地描述了被测试方法中操作的对象的调用流。
除此之外,您还模拟了许多东西:依赖项、被测试方法的参数。
最后,将被测试方法的返回赋值给引用mock参数的变量。这让事情变得很不清楚。
考试必须是直截了当的,而事实并非如此。
我花了大约5百万才明白错误的原因。对于这样一个简单的代码来说,这是非常重要的。
此Assert失败:

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

是因为在测试方法中 getMynEntityA() 只能是 null 作为 myentityBmock 是一个mock,在这里设置字段对实际字段和关联的getter行为没有影响:

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

实际上,您的测试方法主要描述被测试方法的调用流,这就足够了:

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

因为你不能测试 setMyentityA() .
但实际上,我强烈建议您对方法进行单元测试,而不要模拟已测试方法的参数。
它可能看起来像:

@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());
}

没有测试过的代码,但它应该可以帮助你理解我的意图。

相关问题