lundi 28 septembre 2015

Mock object method call [duplicate]

This question already has an answer here:

I am trying to write Test for this class (Java Spring Boot): http://ift.tt/1O4vGza

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

This results in getting a java.lang.NullPointerException

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire