'spring boot unit testing using testng

How to write a POST method test case if the return type of a particular create method in the service layer is ResponseEntity<Object>?

This is my createOffer method:

public ResponseEntity<Object> createOffer(Offer offer) {
    Offer uoffer = offerRepository.save(offer);


    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{jobTitle}").
                    buildAndExpand(uoffer.getJobTitle()).toUri();

    return ResponseEntity.created(location).build();

}

and this is its corresponding test class method:

@Test
public void testCreateOffer() {
    Offer offer = new Offer("SE",new Date(),5);
    Mockito.when( offerRepository.save(offer)).thenReturn( offer);
    assertThat(offerServiceImpl.createOffer(offer)).isEqualTo(offer);
}

Here I am getting an error while running this test case which is no current servlet request attributes and exception is:

java.lang.IllegalStateException

Why is it coming



Solution 1:[1]

This answers the above question.

Hope it helps when someone finds the same issue !!!

@Test
public void testCreateOffer() {
    Offer offer = new Offer("SE",new Date(),5);


    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));


    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{jobTitle}").
            buildAndExpand(offer.getJobTitle()).toUri();

    ResponseEntity<Object> response = ResponseEntity.created(location).build();
    Mockito.when( offerRepository.save(offer)).thenReturn(offer);
    assertThat( offerServiceImpl.createOffer(offer)).isEqualTo(response);
}

Solution 2:[2]

Problem is that in your method you want to get infromation from class ServletUriComponentsBuilder. When you open this class in comment is

UriComponentsBuilder with additional static factory methods to create links based on the current HttpServletRequest.

So it means when your application is running on server (e.g. tomcat) you have context and you can read information from HttpServletRequest. But in junit you don't have context and you can't get this iformation. So when your code is runnig and reach the ServletUriComponentsBuilder.fromCurrentRequest() then the code is done. So you have to mock it. Look at this link it can help you.

ServletUriComponentsBuilderTests

Solution 3:[3]

Kotlin.

I was getting java.lang.IllegalStateException: No current ServletRequestAttributes cause I had this line in my service:

val location = ServletUriComponentsBuilder.fromCurrentRequest().build().toUri()

I have put the following into my setUp() function:

   @BeforeEach
    fun setup() {
        MockitoAnnotations.openMocks(this)
        val request = MockHttpServletRequest()
        RequestContextHolder.setRequestAttributes(ServletRequestAttributes(request))
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Diana