Hello,
I am having problems understanding the correct way to unit test a restful controller that depends on a Autowired �Service� class. The problem I have is that when I run my unit test. I get lots of bean dependency errors that seem to go on forever. I get the feeling that I am configuring the application over again just for my unit test.
Im using @ContextConfiguation annotation in this case:
Here is my Unit Test
Here is my configuration RegistrationControllerTest-context.xmlCode:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class RegistrationControllerTest { private RegistrationController registrationController; private MockMvc mockmvc; @Before public void setUp() throws Exception { registrationController = new RegistrationController(); assertThat(registrationController, notNullValue()); mockmvc = MockMvcBuilders.standaloneSetup(registrationController).build(); } @Test public void json() throws Exception { this.mockmvc .perform(post("/rest/auth/register") .accept(MediaType.APPLICATION_JSON).body(new String("{\"name\":springRestTest, \"user\":Darren, \"description\":Yes,it works}").getBytes())); } }
Here is the RegistrationControllerCode:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="uk.co.myCompany.mvc" /> <mvc:resources mapping="/resources/**" location="/resources/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
My component scan picks up the controller, but then �correctly� baths because the controller needs some autowired dependencies e.g (the applicationContextService).Code:@Controller @RequestMapping("rest/auth") public final class RegistrationController { private static final ISaltGenerator SALT_GENERATOR = new RandomSaltGenerator(); private static final Logger logger = Logger.getLogger(RegistrationController.class); @Autowired private IApplicationContextService applicationContextService; @Autowired private IRollupScheduleService rollupScheduleService; public RegistrationController() { logger.info(RegistrationController.class); }
So what I could do is create a bean for the applicationContextService which in turn requires a DAO class to be injected. The DAO class then needs a session factory injected etc�
Is there any other quicker way to do this without having to configure the beans for the unit test. I just want to unit test the rest controller and do not need not worry about the service classes at this point.. I guess I need a way where @ContextConfiguration can kinda mock up the service classes, so I don�t have to configure them?
Is this possible or am I going about it the wrong way?
Any help would be appreciated .
Thanks
Darren.


Reply With Quote