I am trying to write an integration test using Spring Boot that tests the transaction logic in one of my controllers.
What the test should do, is the following:
- Inject one of my controllers using @Inject
- Replace an email dependency in the controllers dependencies with a Mock, to avoid actually sending an email during the integration test.
- Call a method of the controller
- Assert that the transactions of the called method are properly rolled back when the mail sending mock throws an exception.
Now my problem is that, when the test runs, the controller is injected into my test class but all its dependencies are null
. Here is my integration test:
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
@SpringApplicationConfiguration(App.class)
@WebIntegrationTest
public MyIntegrationTest () {
@Inject MyController controller;
@Before
public void before () {
// replace one particular dependency of controller with a mock
}
@Test
public void testFoo () { ... }
}
Due to the test being an integration test which starts up a full spring web application context, I was expecting that my controller would have all its dependencies already autowired, but that is obviously not the case and instead all dependencies are set to null.
Question: Do I need to use some additional annotations, or setup something in my @Before
method? Or am I approaching the problem from a completely wrong side?
Update: Is it possible to test my Spring MVC Layer, without testing via HTTP such as with TestRestTemplate or MockMvc? But by directly