Im trying to do unit tests for a Rest Controller. I did a stub(~mock) for the manager to the database acces and it works well. My only issue is that when I start my unit test it doesn't start the Application.
How can I start the application from my unit test ?
I'm using spring 4.2.3, spring boot 1.3.7, junit 4.12.
Here are my classes :
TestRestController
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/mvc/mvc-test-context.xml")
public class RestControllerTest extends AbstractTransitionnalTest {
@Autowired
private IManager Manager;
@Test
public void getTestSingleItem(){
Item itm = myTestItemPreInitiallized;
Manager.save(itm);
List<Map> apiResponse = restTemplate.getForObject(networkAddress + "/items", List.class);
// Assertions on apiResponse
}
}
RestController:
@RestController
@RequestMapping("/items")
class RestController {
@Autowired
private IManager Manager;
// Controller content
}
Beans in mvc-test-context.xml
<bean
id="IManager"
class="com.service.ManagerStub">
</bean>
<bean
id="RestController"
class="com.controller.RestController">
</bean>
Application class that contains the main
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@ImportResource({ "classpath:/META-INF/spring/context-application.xml" })
public class Application {
If I run it as it is now the application class isn't started and i get the following erreor : I/O error on GET request for adress:Connection refused
If you don't have the exact solution or would like to propose another way to do this or a workaround, what I wish for is to have the ManagerStub
to be inserted in the @Autowired
manager instead Manager
class only when I launch my test.