0

I am writing integration testing for the REST API. I am facing an issue while while testing the logic for validateDate in MyService class. I would like to mock the currentDate() method MyService class in order to write the different test scenarios. How can I achieve this in spring boot?

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyControllerTest {

    TestRestTemplate restTemplate = new TestRestTemplate();

    @LocalServerPort
    private int port;   

    //@MockBean
    //MyService service;

    @Test
    public void testRetrieveStudentCourse() {
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);

        ResponseEntity<String> response = restTemplate.exchange(
                createURLWithPort("/validatedate"),
                HttpMethod.GET, entity, String.class);

        String expected = "true";

        assertEquals(expected, response.getBody(), false);
    }

    private String createURLWithPort(String uri) {
        return "http://localhost:" + port + uri;
    }





import java.util.Date;

import org.springframework.stereotype.Service;

public interface IMyService {

    public boolean validateDate(Date date);
}

@Service
public class MyService implements IMyService{

    public boolean validateDate(Date date) {
        if(currentDate().before(date)) {
            return true;
        }
        return false;
    }

    private Date currentDate() {
        return new Date();
    }
}


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    IMyService service;

    @GetMapping(value = "/validatedate", produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean validateDate() {
        // date object will be retrieved.
        Date date = somedate;
        service.validateDate(date);
    }
}



Mockito.when(myservice.currentDate()).thenReturn(null);

         doReturn(null).when(myservice.currentDate());
user414967
  • 5,225
  • 10
  • 40
  • 61
  • Why `@MockBean` and subsequent stubbing in the target test don't work for you? – Artem Bilan Oct 22 '18 at 15:21
  • I tested @Mockbean, then the method validateDate() inthe service class did not execute. May be my lack of knowledge. – user414967 Oct 22 '18 at 15:41
  • OK. If you want the real method to be called consider to use a `@SpyBean` instead. Then stubbing should do a `InvocationOnMock.callRealMethod()` – Artem Bilan Oct 22 '18 at 15:52
  • @Artem. thank you. The call is coming on the method. But I am unable to change the value for currentDate(). It is alwasys giving currentDate.I wanted to mock date as null and yesterday's date and tomorrow's date for testng scenarios. – user414967 Oct 22 '18 at 16:18
  • Well, you can't stub `private` methods. That's true. Maybe consider to make that method as `public` as well? – Artem Bilan Oct 22 '18 at 16:24
  • What I thought is I dont want to change from private to public for testing the method,Even after changing to public method, it does not changing the value. Mockito.when(myservice.currentDate()).thenReturn(null); doReturn(null).when(myservice.currentDate()); – user414967 Oct 22 '18 at 16:32
  • Yo have logic there like this: `if(currentDate().before(date))`. It's not going to work against `null`, if your `currentDate()` returns `null` – Artem Bilan Oct 22 '18 at 16:35
  • what I meant is even after mocking the currentDate value as null by using Mockito.when(myservice.currentDate()).thenReturn(null); .Still give me the current date. Ideally i am expepcting a currentDate() value as null. That is not happening here. – user414967 Oct 22 '18 at 16:38
  • here is what you'r looking for https://stackoverflow.com/questions/24491260/mocking-time-in-java-8s-java-time-api – Pianov Oct 22 '18 at 19:22

1 Answers1

0
import static org.mockito.Mockito.when;
........................
public class MyControllerTest {
    @Mock
    IMyService service

    .........................................
    @Test
    public void myTest(){
        when(service.validateDate(date)).thenReturn(false);
    ......... do test .............................................
    }
}
Vasily Komarov
  • 1,405
  • 9
  • 11
  • @Vassily this is not possible for me. I am doing integration testing. So if possible if I could set the value date, then it is fine. – user414967 Oct 22 '18 at 15:51