受保护的单元测试失败

阿拉法

我有一堂课EllaService,我想在EllaService::isTrafficIgnored那里测试该方法。下面提供了代码,

@Service
public class EllaService {


    @Autowired
    @Qualifier( ELLA_CONNECTOR_BEAN_NAME )
    private EntityServiceConnectable<EllaResponseDto> connector;

    @Autowired
    @Getter
    private EllaFilterConfigHolder configHolder;

    @Autowired
    @Getter
    private EllaConfiguration config;

    @Autowired
    private Environment env;


    protected boolean isTrafficIgnored( IrisBo irisBo ) {

        if( config.isExternalCostumerFilter( this.env ) && irisBo.getBuyer().isExternalKnownCustomer() ) {
            return true;
        }

        if( config.isInternalCostumerFilter( this.env ) && irisBo.getBuyer().isInternalKnownCustomer() ) {
            return true;
        }

        return checkIfShopIdFilterIsApplied( irisBo );
    }

    // ========================================================================

    private boolean checkIfShopIdFilterIsApplied( IrisBo irisBo ) {
        return configHolder.getShopIdsToFilterSet().contains( irisBo.getOrder().getShopId() );
    }

    // ........................

}

我为测试类中的方法提供了 2 个测试,

@RunWith( PowerMockRunner.class )
@PrepareForTest( EllaDtoConverter.class )
public class EllaServiceTest {

    private static final String VALID_TRX_ID = "valid-trx-id";
    private static final String VALID_GW_ID = "valid-gw-id";

    @InjectMocks
    private EllaService ellaService;

    private IrisBo validIrisBo;

    @Mock
    private EllaRequestDto ellaRequestDto;

    @Mock
    private EntityServiceConnectable<EllaResponseDto> entityServiceConnector;

    @Mock
    private EllaResponseDto ellaResponseDto;

    @Mock
    private EllaConfiguration ellaConfiguration;

    @Mock
    private EllaFilterConfigHolder ellaFilterConfigHolder;

    @Mock
    private EllaService ellaServiceMock;

    @Mock
    private Environment environment;

    @SuppressWarnings( "unchecked" )
    @Before
    public void setup() {

        PowerMockito.mockStatic( EllaDtoConverter.class );
        when( EllaDtoConverter.convertToRequest( any() ) ).thenReturn( ellaRequestDto );

        ServiceResponse<EllaResponseDto> validServiceResponseMock = mock( ServiceResponse.class );
        when( entityServiceConnector.call( any(), (HttpHeaders) any() ) ).thenReturn( validServiceResponseMock );

        when( validServiceResponseMock.isSuccess() ).thenReturn( true );
        when( validServiceResponseMock.getResponse() ).thenReturn( ellaResponseDto );
        when( validServiceResponseMock.getErrorMessage() ).thenReturn( "" );

        when( ellaServiceMock.getConfigHolder() ).thenReturn( ellaFilterConfigHolder );
        when( ellaServiceMock.getConfig() ).thenReturn( ellaConfiguration );
        when( ellaConfiguration.isExternalCostumerFilter( any() ) ).thenReturn( false );
        when( ellaConfiguration.isInternalCostumerFilter( any() ) ).thenReturn( false );
        when( ellaConfiguration.extractShopIdsToFilter( any() ) ).thenReturn( "" );

        when( ellaFilterConfigHolder.getShopIdsToFilterSet() ).thenReturn( new HashSet<>() );

        validIrisBo = new IrisBo();

        RequestInformation requestInfo = Mockito.mock( RequestInformation.class );

        when( requestInfo.getTransactionId() ).thenReturn( VALID_TRX_ID );
        when( requestInfo.getGatewayRequestId() ).thenReturn( VALID_GW_ID );

        OrderBo orderBo = new OrderBo();

        orderBo.addProduct( INVOICE );
        orderBo.setShopId( 123 );

        AddressBo addressBo = new AddressBo();

        addressBo.setStreetName( "Rutherfordstraße" );
        addressBo.setHouseNumber( "2" );
        addressBo.setZipCode( "12489" );

        ServiceBo serviceBo = new ServiceBo();

        serviceBo.setDate( LocalDate.parse( "2018-11-26" ) );
        serviceBo.setExistingCustomer( Boolean.TRUE );

        TransactionBo transactionBo = new TransactionBo();

        transactionBo.setTrxId( "valid-trx-id" );
        transactionBo.setCurrencyCode( "EUR" );
        transactionBo.setAmount( 12.5 );

        BuyerBo buyerBo = new BuyerBo();

        buyerBo.setBillingAddress( addressBo );
        buyerBo.setDeliveryAddress( addressBo );

        validIrisBo.setRequestInfo( requestInfo );
        validIrisBo.setOrder( orderBo );
        validIrisBo.setBuyer( buyerBo );

        validIrisBo.getBuyer().setBillingAddress( addressBo );
        validIrisBo.getBuyer().setDeliveryAddress( addressBo );

        validIrisBo.setTrackingId( "9241999998422820706039" );
        validIrisBo.setEmail( "[email protected]" );
        validIrisBo.setIp( "123.120.12.12" );
        validIrisBo.setFirstName( "Max" );
        validIrisBo.setLastName( "Musterman" );
    }



    @Test
    public void testIsTrafficIgnoredWhenExternalCostumerFilterReturnsTrueAndBuyerIsExternalKnownCustomer() {

        when( ellaConfiguration.isExternalCostumerFilter( environment ) ).thenReturn( true );
        validIrisBo.getBuyer().setExternalKnownCustomer( true );

        assertEquals( true, ellaService.isTrafficIgnored( validIrisBo ) );
    }

    @Test
    public void testIsTrafficIgnoredWhenInternalCostumerFilterReturnsTrueAndBuyerIsInternalKnownCustomer() {

        when( ellaConfiguration.isInternalCostumerFilter( environment ) ).thenReturn( true );
        validIrisBo.getBuyer().setInternalKnownCustomer( true );

        assertEquals( true, ellaService.isTrafficIgnored( validIrisBo ) );
    }


}

现在,我想测试

一种。如果条件

if( config.isExternalCostumerFilter( this.env ) && irisBo.getBuyer().isExternalKnownCustomer() ) 

是假的

条件

if( config.isInternalCostumerFilter( this.env ) && irisBo.getBuyer().isInternalKnownCustomer() )

false

C。

checkIfShopIdFilterIsApplied( irisBo ) 

返回 true

然后isTrafficIgnored也将返回true

下面提供了我的测试方法,

@Test
    public void testIsTrafficIgnoredWhenAllOtherConditionsAreFalseButCheckIfShopIdFilterIsAppliedReturnsTrue() throws Exception {

        when( ellaConfiguration.isExternalCostumerFilter( environment ) ).thenReturn( false );
        when( ellaConfiguration.isInternalCostumerFilter( environment ) ).thenReturn( false );

        PowerMockito.when( ellaServiceMock, "checkIfShopIdFilterIsApplied", validIrisBo ).thenReturn( true );

        assertTrue( ellaService.isTrafficIgnored( validIrisBo ) );
    }

我得到NullPointerException下面提供的例外,

java.lang.NullPointerException
    at com.ratepay.iris.ella.service.EllaService.checkIfShopIdFilterIsApplied(EllaService.java:118)

我更新了提供的测试方法,

    @Test
    public void testIsTrafficIgnoredWhenAllOtherConditionsAreFalseButCheckIfShopIdFilterIsAppliedReturnsTrue() throws Exception {

        EllaFilterConfigHolder e = spy( new EllaFilterConfigHolder() );

        doReturn( true ).when( e.getShopIdsToFilterSet().contains( validIrisBo.getOrder().getShopId() ) );

        when( ellaConfiguration.isExternalCostumerFilter( environment ) ).thenReturn( false );
        when( ellaConfiguration.isInternalCostumerFilter( environment ) ).thenReturn( false );

        PowerMockito.when( ellaServiceMock, "checkIfShopIdFilterIsApplied", validIrisBo ).thenReturn( true );

        assertTrue( ellaService.isTrafficIgnored( validIrisBo ) );
    }

我得到提供的错误堆栈,

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.ratepay.iris.ella.service.EllaServiceTest.testIsTrafficIgnoredWhenAllOtherConditionsAreFalseButCheckIfShopIdFilterIsAppliedReturnsTrue(EllaServiceTest.java:216)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed


    at com.ratepay.iris.ella.service.EllaServiceTest.testIsTrafficIgnoredWhenAllOtherConditionsAreFalseButCheckIfShopIdFilterIsAppliedReturnsTrue(EllaServiceTest.java:216)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)

如何更正我的测试?

更新

我感觉我的问题不清楚。我想知道如何模拟checkIfShopIdFilterIsApplied( irisBo )返回的语句true

阿拉法

我设法编写了测试并在下面提供。

@Test
    public void testIsTrafficIgnoredWhenAllOtherConditionsAreFalseButCheckIfShopIdFilterIsAppliedReturnsTrue() throws Exception {

        when( ellaConfiguration.isExternalCostumerFilter( environment ) ).thenReturn( false );
        when( ellaConfiguration.isInternalCostumerFilter( environment ) ).thenReturn( false );

        Set<Integer> shopIdsForTheOrders = new HashSet<>();

        shopIdsForTheOrders.add( 145 );
        shopIdsForTheOrders.add( 500 );
        shopIdsForTheOrders.add( SHOP_ID_FOR_ORDER );

        when( ellaFilterConfigHolder.getShopIdsToFilterSet() ).thenReturn( shopIdsForTheOrders );
        assertTrue( ellaService.isTrafficIgnored( validIrisBo ) );
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

受保护的通用方法单元测试的过载导致AmbiguousMatchException

来自分类Dev

受保护的通用方法单元测试的过载导致AmbiguousMatchException

来自分类Dev

单元测试失败

来自分类Dev

Umbraco单元测试失败

来自分类Dev

验证单元测试失败

来自分类Dev

“单元测试失败”的BeautifulSoup

来自分类Dev

Umbraco单元测试失败

来自分类Dev

Rails单元测试失败

来自分类Dev

对基类中的受保护属性进行单元测试的正确方法

来自分类Dev

PHP单元测试抽象类中的受保护变量

来自分类Dev

单元测试端点受内存身份验证引擎保护

来自分类Dev

角度单元测试失败,但本地失败

来自分类Dev

如何获取方法单元测试中分配给受保护对象的HttpContext.Current.Server.MapPath的伪路径?

来自分类Dev

如何获取方法单元测试中分配给受保护对象的HttpContext.Current.Server.MapPath的伪路径?

来自分类Dev

Laravel单元测试迁移失败

来自分类Dev

无条件失败单元测试

来自分类Dev

模板别名导致单元测试失败

来自分类Dev

单元测试HtmlHelper扩展方法失败

来自分类Dev

随机失败的C#单元测试

来自分类Dev

客户ActionFilterAttribute的单元测试失败

来自分类Dev

Scala期货中的单元测试失败

来自分类Dev

Android单元测试改造失败

来自分类Dev

使用React钩子的单元测试失败

来自分类Dev

由于会话,单元测试失败

来自分类Dev

单元测试文件上传失败

来自分类Dev

单元测试仅在ARM中失败

来自分类Dev

Visual Studio单元测试检测失败

来自分类Dev

当指令是属性时,单元测试失败

来自分类Dev

jsonwebtoken导致单元测试失败