0

I am trying to write Test for this class (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

Specifically, I am trying to Mock this method call:

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

To do this, I tried to instantiate a Mock of the ServiceUtils object and use the .when and .thenReturn Mock methods.

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);
    }

But I am still getting the result of the original object of ServiceUtils instead of the Mocked method call result.

What am I doing wrong?

Kaj
  • 2,445
  • 3
  • 23
  • 34
  • 2
    @Jens: I wouldn't say this question is a duplicate. The question is more about Mockito, than NullPointerExceptions. – marstran Sep 28 '15 at 09:41
  • "`MockitoAnnotations.initMocks(this)` method has to called to initialize annotated fields." Stick it in your before. Can't provide as answer as currently closed. – weston Sep 28 '15 at 09:53
  • @weston: I added the initMocks method, the NullPointerException is gone now, but the method call does not result the Mocked result. It still calls the original object instead of the mocked one. (Updated the question) – Kaj Sep 28 '15 at 10:00
  • `productIntegration` has no reference to that mock. So why would it call it? – weston Sep 28 '15 at 10:26
  • This isn't getting reopened any time soon, I'd ask a new question. – weston Sep 28 '15 at 10:26

0 Answers0