Results 1 to 2 of 2

Thread: Unit Testing Rest Controllers with @Autowired Service Class

  1. #1

    Default Unit Testing Rest Controllers with @Autowired Service Class

    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

    Code:
    @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 my configuration RegistrationControllerTest-context.xml

    Code:
    <?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>
    Here is the RegistrationController

    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);
    	}
    My component scan picks up the controller, but then �correctly� baths because the controller needs some autowired dependencies e.g (the applicationContextService).

    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.

  2. #2

    Default

    Im still having problems getting this to work and any advise will really be helpful. Does the above look ok?
    Last edited by DJC_Spring; Jul 19th, 2012 at 08:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •