使用Mockito在JUnit中获取BadRequestException

时间:2018-08-14 11:06:43

标签: junit java-8 mockito spring-webflux

我正在使用Mockito为服务类编写Junit。应用程序是使用Java 8,Spring 5构建的。尽管我的其余测试方法运行良好,但我被困住了,无法继续进行下去。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = CommerceConnectorApplication.class)
@ActiveProfiles("test")
@AutoConfigureWebTestClient
public class CommerceRepositoryDetailsTest {

  @Autowired
  private RepositoryDetails repositoryDetails;

  @MockBean
  private AsyncRunner asyncRunner;

  @MockBean
  private CommerceTenantUserRepository tenantUserRepository;

  @Test
  public void whenUpdateCartId_ThenReturnUpdatedTenantUserDetails(){
    String tenantId = "2737363";
    String userId = "763sssj";
    String cartId = "827";

    TenantUserKey tenantUserKey = new TenantUserKey();
    tenantUserKey.setTenantId(tenantId);
    tenantUserKey.setUserId(userId);

    CommerceTenantUser commerceTenantUser = new CommerceTenantUser(tenantUserKey, "xyz@abc.com", cartId, LocalDate.now());
    Optional <CommerceTenantUser> commerceTenantUser1 = Optional.of(commerceTenantUser);


    Mockito.when(tenantUserRepository.findById(tenantUserKey)).thenReturn(commerceTenantUser1);

    Mono<CommerceTenantUser> actualMono = repositoryDetails.updateCartId(tenantId, userId, cartId);
    CommerceTenantUser commerceTenantUser2 = actualMono.block();

    assertThat(commerceTenantUser2, is(commerceTenantUser));

  }
}

错误:-

2018-08-14 16:19:38.843  INFO 106888 --- [           main] c.s.c.c.c.s.CommerceRepositoryDetails    : Entering updateCartId@CommerceSystemDetailService

com.sap.chatbot.commerceconnector.exception.BadRequestException: error in updating cart

    at com.sap.chatbot.commerceconnector.common.security.CommerceRepositoryDetails.updateCartId(CommerceRepositoryDetails.java:139)
    at com.sap.chatbot.commerceconnector.common.security.CommerceRepositoryDetails$$FastClassBySpringCGLIB$$fd3bfefe.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.cache.interceptor.CacheInterceptor.lambda$invoke$0(CacheInterceptor.java:53)
    at org.springframework.cache.interceptor.CacheAspectSupport.invokeOperation(CacheAspectSupport.java:337)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:392)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:317)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.sap.chatbot.commerceconnector.common.security.CommerceRepositoryDetails$$EnhancerBySpringCGLIB$$3b388d4d.updateCartId(<generated>)
    at com.sap.chatbot.commerceconnector.CommerceRepositoryDetailsTest.whenUpdateCartId_ThenReturnUpdatedTenantUserDetails(CommerceRepositoryDetailsTest.java:124)

主要测试方法:-

    @Override
    @CachePut(value = CommerceConnectorConstants.CommerceCache.COMMERCE_CACHE_TENANT_USER_DATA_MAP)
    public Mono<CommerceTenantUser> updateCartId(String tenantId, String userId, String cartId) {
        logger.info("Entering updateCartId@CommerceSystemDetailService");

        final TenantUserKey commerce = getTenantUserObj(tenantId, userId);
        Optional<CommerceTenantUser> tenantUser = commerceTenantUserRepository.findById(commerce);

        return tenantUser.map(tenantUserObj -> updateCartIdAndRefreshMap(tenantUserObj, cartId))
                .orElse(Mono.error(new BadRequestException("error in updating cart")));

    }

    private Mono<CommerceTenantUser> updateCartIdAndRefreshMap(CommerceTenantUser tenantUserObj, String cartId) {
        tenantUserObj.setCartId(cartId);
        final Mono<CommerceTenantUser> commerceTenantUser = asyncRunner
                .one(() -> commerceTenantUserRepository.saveAndFlush(tenantUserObj))
                .doOnNext(value -> commerceCacheService.refreshMap())
                .doOnError(error -> Mono.error(new BadRequestException("Error ocurred in updating cart id ")))
                .map(commerceTenantUserObj -> commerceTenantUserObj);
        return commerceTenantUser;

    }

方法 updateCartId()调用另一个私有方法 updateCartIdAndRefreshMap()。我的JUnit在 updateCartId()中的返回声明中失败。

请提供有关如何修复我的JUnit以测试此特定updateCartId()方法的建议。

0 个答案:

没有答案