Skip to content
Advertisement

Spring Boot controller test loading entire application context

Spring Boot here. I currently have the following REST controller:

JavaScript

I would like to write an integration test for it that:

  1. Mocks or stubs an HTTP request to the URL; and
  2. Allows me to inject the FizzbuzzController (under test) with a mocked FizzbuzzService or the real thing; and
  3. Allows me to inspect the HTTP response coming back from the method (check status code, check response entity, etc.)

My best attempt thus far:

JavaScript

When I run this, the test fails to run and it is clear (looking at the logs) that Spring is loading the entire application context of my app, whereas I just want it to isolate the context to this test class, the main FizzbuzzController class, and anything in the dependency tree underneath it.

Can anyone spot where I’m going awry?

Advertisement

Answer

You need another context for testing. I’m suggesting you to have a separate Test config:

JavaScript

and in a controller test build the context like below:

JavaScript
Advertisement