Results 1 to 2 of 2

Thread: Unit Testing with Spring-MVC-Test - @Autowire attributes are null

Threaded View

  1. #1

    Default Unit Testing with Spring-MVC-Test - @Autowire attributes are null

    Hi guys,

    Im confused when trying to write a unit test for my rest controller as I cannot seem to mock my @Autowire properties in the controller.

    Here is my Test

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration()
    public class RegistrationControllerTesting {
    	
    	private MockMvc mockMvc;
    	private LicenseSecurityContextHelper licenseSecurityContextHelper;
    	        
    	private ILicenseAuthentication licenseAuthentication;
    	
    	@Before
    	public void setup() {		
    		licenseAuthentication = mock(LicenseAuthentication.class);
    		mockMvc = MockMvcBuilders.standaloneSetup(new RegistrationController()).build();
    		licenseSecurityContextHelper = mock(LicenseSecurityContextHelper.class);
    		when(licenseSecurityContextHelper.getLicenseAuthentication()).thenReturn(licenseAuthentication);
    	}
    	
    	@Test
    	public void test() throws Exception {		
    		DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/rest/auth/register")
    			.contentType(MediaType.APPLICATION_JSON)
    			.body(JsonHelperProvider.registrationJson.getBytes());
    		mockMvc.perform(requestBuilder);
    
    	}
    My config in RegistrationControllerTesting-context.xml

    Code:
    <bean id="licenseSecurityContextHelper" class="org.mockito.Mockito" factory-method="mock"> 
     		<constructor-arg value="uk.co.company.common.license.spring.LicenseSecurityContextHelper" />
     	</bean> 
    		
    	<bean id="rollupScheduleService" class="org.mockito.Mockito" factory-method="mock">
    		<constructor-arg value="uk.co.company.product.service.IRollupScheduleService" />
    	</bean>
    	
    	<bean id="productApplicationContextService" class="org.mockito.Mockito" factory-method="mock">
    		<constructor-arg value="uk.co.company.product.service.IproductApplicationContextService" />
    	</bean>
    My controller has autowire fields

    Code:
    @Autowired
    private IproductApplicationContextService productApplicationContextService;
    	
    @Autowired
    private IRollupScheduleService rollupScheduleService;
    	
    @Autowired
    private LicenseSecurityContextHelper licenseSecurityContextHelper;
    However in my controller the object licenseSecurityContextHelper is null i.e

    Code:
    ILicenseAuthentication licenseAuthentication = licenseSecurityContextHelper.getLicenseAuthentication();
    Im confused because the beans seem to be autowired in the log file. I proved this by removing the mocked bean definitions from my xml config and this produces the error

    Code:
    "Could not autowire field: private uk.co.company.product.service.IproductApplicationContextService"
    Can someone please tell me what ive done wrong?
    Last edited by DJC_Spring; Jul 27th, 2012 at 04:15 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
  •