代码之家  ›  专栏  ›  技术社区  ›  Vinod Kumar

使用Mockito在JUnit中获取BadRequestException

  •  0
  • Vinod Kumar  · 技术社区  · 6 年前

    我正在用Mockito为一个服务类编写一个Junit。应用程序是使用Java8、Spring5构建的。而我的其他测试方法运行得很好,除了这一个,我被卡住了,无法继续下去。

    @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() 调用另一个私有方法 updateCartidRefreshMap() . 我的JUnit在返回声明中失败 .

    0 回复  |  直到 6 年前