简体   繁体   English

如何调用采用 ServerHttpResponse 类型参数的方法?

[英]How can I call a method which takes a parameter of type ServerHttpResponse?

I am writing a Junit Test Case with @WebFluxTest for a Service class.我正在使用 @WebFluxTest 为服务 class 编写一个 Junit 测试用例。

Since I need to test a method for a Service Class I need to call the method and check whether it returns a correct Http Status ie 200/"OK", and I need help regarding the same.由于我需要测试服务 Class 的方法,我需要调用该方法并检查它是否返回正确的 Http 状态,即 200/“OK”,我需要有关相同的帮助。

Service Class Method which I want to test: Service Class 我要测试的方法:

public Mono<Product> addProduct(Product productData, ServerHttpResponse response) {
        productData.getFcData().replaceAll((fcCode,fcData)-> {
                fcData.setBatchWiseData(fcData.getBatchWiseData().parallelStream()
                    .map(d->{
                        if(d.getLastUpdated() ==null)
                            d.setLastUpdated(LocalDateTime.now());
                        return d;
                    } ).collect(Collectors.toList()));
                    return fcData;
            }
        );
        
        Mono<Boolean> success = redisTemplate.opsForHash()
                .put(PRODUCT_PREFIX+productData.getProductCode(), 
                        productData.getProductCode(), productData);
            success.subscribe();
            response.setStatusCode(HttpStatus.CREATED);
        return success.flatMap(x->getProduct(productData.getProductCode()));
    }

My test case:我的测试用例:

    @Test
    void testAddProduct(){
        Product prodObj = new Product();
        prodObj.setProductCode("789123");

        FcWiseData fcWiseDataObj = new FcWiseData();
        fcWiseDataObj.setFcCode("50001");
        BatchWiseData batchWiseDataObj = new BatchWiseData();
        batchWiseDataObj.setBatchNo("Uni001");
        batchWiseDataObj.setExpiryDate(LocalDate.of(2020, 1, 8));
        batchWiseDataObj.setMrp(100.0);
        batchWiseDataObj.setStockQty(1L);
        batchWiseDataObj.setLastUpdated(LocalDateTime.of(2021, Month.JULY, 29, 19, 30, 40));
        fcWiseDataObj.setBatchWiseData(List.of(batchWiseDataObj));
        Map<String, FcWiseData> fcDataObj = Map.of("52001", fcWiseDataObj);
        prodObj.setFcData(fcDataObj);

        Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOperations);
        Mockito.doReturn(Mono.just(true)).when(hashOperations)
        .put("Product" + prodObj.getProductCode(), prodObj.getProductCode(), prodObj);
        
    //  ServerHttpResponse response = Mockito.mock(ServerHttpResponse.class);
    //  assertNotNull(prodService.addProduct(prodObj, response));
         
    // what should I write here to test the above method?   
    }

You can use the class MockHttpServletResponse for the method params in your tests.您可以将 class MockHttpServletResponse用于测试中的方法参数。

Something like this should work.这样的事情应该有效。

HttpServletResponse httpServletResponse = new MockHttpServletResponse();

For more details, please refer to the javadoc here有关详细信息,请参阅此处的 javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 8 中定义一个以 lambda 作为参数的方法? - How do I define a method which takes a lambda as a parameter in Java 8? 无法调用JSP中带有参数的方法 - Can not call method that takes parameter in JSP 如何声明和调用从RPG获取二维bye数组参数的java方法? - How to declare and call a java method which takes two dimensional bye array parameter from RPG? 如何从C ++ / JNI调用Java方法,该方法采用Android Context参数 - How to call a Java method from C++ / JNI which takes an Android Context parameter 如何调用从Java提取哈希值的Ruby方法? - How can I call a Ruby method that takes a Hash from Java? 如何在该接口类型的对象上调用实现接口的类中的方法? - How can I call a method which is in a class which implements an Interface, on an object of that interface's type? 为什么我不能用错误类型的参数调用方法? - Why can't I call a method with a parameter of the wrong type? 如何实现具有通用返回类型和通用参数的通用方法? - How can I implement generic method which has generic return type and generic parameter? 如何调用从 Kotlin 获取非空无效参数的 Java 方法? - How do I call a Java method that takes a Non-null Void parameter from Kotlin? 如何验证以Lambda为参数的ThreadPool执行方法 - How to verify ThreadPool execute method which takes Lambda as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM