模拟对象方法调用

时间:2015-09-28 09:32:12

标签: java unit-testing spring-boot mockito

我正在尝试为此类编写Test(Java Spring Boot):https://github.com/callistaenterprise/blog-microservices/blob/master/microservices/composite/product-composite-service/src/main/java/se/callista/microservices/composite/product/service/ProductCompositeIntegration.java

具体来说,我试图模拟这个方法调用:

URI uri = util.getServiceUrl("product");

为此,我尝试实例化ServiceUtils对象的Mock并使用.when和.thenReturn Mock方法。

private ProductCompositeIntegration productIntegration = new ProductCompositeIntegration();

    @Autowired
    private RestTemplate restTemplate;

    @Mock
    private ServiceUtils util;

    private MockRestServiceServer mockServer;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void myTest() {
        URI uri = URI.create("TestUrl");
        Mockito.when(util.getServiceUrl("product")).thenReturn(uri);
        ResponseEntity<Product> product = productIntegration.getProduct(1);
    }

但我仍然得到ServiceUtils的原始对象的结果而不是Mocked方法调用结果。

我做错了什么?

0 个答案:

没有答案