在编写服务层的Junit测试用例时需要帮助

时间:2019-01-21 10:55:51

标签: java rest spring-boot mockito junit4

我刚接触Spring,我正在尝试为服务层编写单元测试用例,它必须通过RestTemplate调用rest URL,然后从此数据中筛选出我要传递并返回的输入的低音过滤后的数据。但是,当尝试执行此代码时,我得到了空指针异常。

URL1的Github链接:https://github.com/vk1993/telstra-developer-exercise

URL2的Github链接:https://github.com/vk1993/Support_Microservice

URL1是访问Support_Microservice URL并过滤数据并返回结果的项目。

这是服务等级

@Service@Slf4j
public class RetriveDiscountImpl implements RetriveDiscount {

    // http://localhost:9999/rest/v1/customers/qa-test-user 
    @Value("${getDisountURL}")
    public String Url;

    @Autowired
    public RestTemplate restTemplate ;

    @Override
    public ResponseEntity getRetrievedDiscound(String uuid, String pid) throws NullPointerException {
        log.info(Url);

        ResponseEntity<CustomerResourse> customerResourse = restTemplate.getForEntity(Url, CustomerResourse.class);
        ErrorResource errorResource;

        if(customerResourse.getStatusCode().equals(HttpStatus.OK)) {
             if(uuid.equals("qa-test-user")){

                 log.info(pid);

                 if(!(pid.trim().equals(""))){
                     log.info("inside if of pid");
                     if(pid.trim().equals("sku-1234567890")){
                         log.info("inside trim");
                         List discountPid = new ArrayList();

                   customerResourse.getBody().getEligibleDiscounts().forEach(discount -> {
                        if(discount.getProductId() != null && discount.getProductId().equals(pid)){
                            discountPid.add(discount);
                       }
                    });
                    return ResponseEntity.ok()
                               .body(discountPid);
                     }else{
                         errorResource = new ErrorResource("EE003", String.format("Product with %s is not avaliable",pid));
                         return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_JSON).body(errorResource);
                     }
                 }else{
                     log.info("inside if of pid to discount");
                     return ResponseEntity.ok()
                             .body(customerResourse.getBody().getEligibleDiscounts());
                 }
              }else {
                  errorResource = new ErrorResource("EE005", String.format("User Not Found with UUID: %s",uuid));
                  return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_JSON).body(errorResource);
              }
        }else {
            errorResource = new ErrorResource("EE006", String.format("Get Url is not working : ",Url));
            return ResponseEntity.status(HttpStatus.BAD_GATEWAY).contentType(MediaType.APPLICATION_JSON).body(errorResource);
        }

    }
}

Junit测试用例:

@RunWith(MockitoJUnitRunner.class)
public class RetriveDiscountImplTest {

  @Autowired
  MockMvc mockMvc;

  @InjectMocks
  private RetriveDiscountImpl empService = new RetriveDiscountImpl();

  CustomerResourse customer_Resourse ;
  List<Discount> discountList = new ArrayList<>();
  List<Discount> discountwithPid = new ArrayList<>();
  List<PruchasedProduct> pruchasedProductsList = new ArrayList<>();
  Discount discount1;
  Discount discount2;
  PruchasedProduct pruchasedProduct;
  ErrorResource errorResource;

  @Before
  public void setUp() throws Exception {

      customer_Resourse = new CustomerResourse();

      pruchasedProduct = new PruchasedProduct();
      pruchasedProduct.setDescription("12 month subscription to 'Horse and Hound'");
      pruchasedProduct.setFinalPrice(new BigDecimal(50));
      pruchasedProduct.setOriginalPrice(new BigDecimal(100));
      pruchasedProduct.setProductId("sku-1234567890");

       discount1 = new Discount("10-percent-off",
              "PERCENT",
              "Reduce the purchase price by 10%",
              new BigDecimal(10));


       discount2 = new Discount(
              "5-dollars-off",
              "AMOUNT",
              "Reduce the purchase price by $5",
              new BigDecimal(5),"sku-1234567890");
      discountwithPid.add(discount2);

      pruchasedProduct.setDiscountInformation(discount1);
      pruchasedProductsList.add(pruchasedProduct);

      discountList.add(discount1);
      discountList.add(discount2);

      customer_Resourse.setAddress("1 Main St");
      customer_Resourse.setName( "Test User");
      customer_Resourse.setUuid("qa-test-user");
      customer_Resourse.setEligibleDiscounts(discountList);
      customer_Resourse.setProducts(pruchasedProductsList);
  }

  @Test
  public void juintwithoutPID() {

      ResponseEntity result = new ResponseEntity(discountList, HttpStatus.OK);
      when(empService.getRetrievedDiscound("qa-test-user","")).thenReturn(result);
      assertEquals(result.getBody(),empService.getRetrievedDiscound("qa-test-user", ""));

  }
}

16:17:20.813 [main] INFO retrivediscountmicro.retrive_discount.service.RetriveDiscountImpl - null

0 个答案:

没有答案