确保模拟调用时条件返回 false



我有一个类,下面提供了代码,

@Service
public class EllaService {
    private static final String SHOP_ID_STRING_SEPARATOR = ",";
    public static final int IRIS_ACCEPT = 0;
    public static final int IRIS_REJECT = 100;
    @Autowired
    @Qualifier( ELLA_CONNECTOR_BEAN_NAME )
    private EntityServiceConnectable<EllaResponseDto> connector;
    @Autowired
    @Getter
    private EllaFilterConfigHolder configHolder;
    @Autowired
    @Getter
    private EllaConfiguration config;
    @Autowired
    private Environment env;
    /**
     * Initialize the component.
     */
    @PostConstruct
    public void initialize() {
        createCustomFilters();
    }
    // ========================================================================
    /**
     * Asynchronously call Ella. Determine if traffic is applicable for Ella and if yes forward to Ella.
     *
     * @param irisBo
     * @return List<ResultBo>
     * @throws EllaGatewayUnsuccessfulResponseException
     */
    @Async
    public void invokeEllaAsync( final IrisBo irisBo ) throws EllaGatewayUnsuccessfulResponseException {
        if( !isTrafficIgnored( irisBo ) ) {
            try {
                callEllaService( irisBo );
            }
            catch( EllaGatewayUnsuccessfulResponseException ex ) {
                throw new EllaGatewayUnsuccessfulResponseException( ex.getMessage(), ex.getCause() );
            }
        }
    }
    // ========================================================================
    private 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() );
    }
    // ========================================================================
    private void callEllaService( final IrisBo irisBo ) throws EllaGatewayUnsuccessfulResponseException {
        HttpHeaders elladHeaders = createRequestHeaders( irisBo );
        ServiceResponse<EllaResponseDto> response = connector.call( EllaDtoConverter.convertToRequest( irisBo ), elladHeaders );
        if( !response.isSuccess() ) {
            throw new EllaGatewayUnsuccessfulResponseException( response.getErrorMessage(), response.getException().getCause() );
        }
    }
    // ========================================================================
    private void createCustomFilters() {
        configHolder.setExternalCustomerFilterEnabled( config.isExternalCostumerFilter( env ) );
        configHolder.setInternalCustomerFilterEnabled( config.isInternalCostumerFilter( env ) );
        configHolder.setShopIdsToFilterSet( new HashSet<>( getShopIdsToFilterAsList( config ) ) );
    }
    // ========================================================================
    private List<Integer> getShopIdsToFilterAsList( EllaConfiguration config ) {
        String shopIdListStr = config.extractShopIdsToFilter( this.env );
        return Arrays.asList( shopIdListStr.split( SHOP_ID_STRING_SEPARATOR ) ).stream().map( s -> Integer.parseInt( s ) )
                .collect( Collectors.toList() );
    }
    // ========================================================================
    private HttpHeaders createRequestHeaders( final IrisBo irisBo ) {
        HttpHeaders ellaHeaders = new HttpHeaders();
        ellaHeaders.add( ACCEPT, APPLICATION_JSON_UTF8_VALUE );
        RatepayHeaders.append( ellaHeaders, irisBo.getRequestInfo() );
        return ellaHeaders;
    }
}

我想测试EllaService::invokeEllaAsync方法。我如何嘲笑当检查条件if( !isTrafficIgnored( irisBo ) )时它会返回false

我看到不同的方法,一种是:

为 EllaConfiguration 引入一个模拟,并使其在调用时返回 true config.isExternalCostumerFilter(any)。还要确保irisBo.getBuyer().isExternalKnownCustomer()返回 true。

这将导致

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

返回真。这将使if( !isTrafficIgnored( irisBo ) )返回 false。

有一些框架可以帮助您做到这一点。使用莫克托例如:

MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
String element = listMock.get(1);
assertThat(element, is(equalTo("Always the same")));

您可以在官方文档中阅读有关Mockito的信息:这里

相关内容

  • 没有找到相关文章

最新更新