0

I have a spring boot rest controller which returns an object. Something like this:

@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
    
    @GetMapping("/{id}", produces = "application/json")
    public ResponseEntity<Book> getBook(@PathVariable int id) {
        return findBookById(id);
    }
 
    
}

I have a unit test that tests this controller method (mockito mocking the service calls inside). Something like:

@RunWith(MockitoJUnitRunner.Silent.class)
public class SimpleBookRestControllerTest {

    @Autowired
    @InjectMocks
    private SimpleBookRestController simpleBookRestController;
    @Mock
    private MyService myservice;

I want now to test what spring exactly does with the object being deserialized to a JSON string. In my test, I just test the objects, which is fine. But I have now a bug which I want to see the JSON string in a unit test.

How would you do that?

1 Answer 1

1

You'll need more than a simple unit test -- the SimpleBookRestController does not handle the conversion to JSON, so you can't just unit test it.

See this guide: https://spring.io/guides/gs/testing-web/, or look for examples of using @WebMvcTest, which is a way to test the whole web layer, including REST calls and their responses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.