Aditya
Aditya

Reputation: 1058

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

Upvotes: 4

Views: 3818

Answers (3)

chujudzvin
chujudzvin

Reputation: 1303

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

Upvotes: 1

Aditya
Aditya

Reputation: 1058

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

Upvotes: 9

user2021515
user2021515

Reputation:

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

Upvotes: 1

Related Questions