2016-04-25 93 views
0

我正在开发一个JUnit测试到Spring Controller中,Controller调用了一堆服务。我的JUnit测试代码是上面:在运行JUnit测试时发现空服务弹出

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {WebApplicationConfiguration.class, DatabaseConfiguration.class, ServiceConfiguration.class}) 
@WebAppConfiguration 
    public class ProcessFileControllerTest { 

     private MockMvc mockMvc; 


     @Test 
     public void getPageTest() throws Exception{ 
      final ProcessFileController controller = new ProcessFileController(); 
      SecurityHelperUtility.setupSecurityContext("user", "pass", "ROLE_ADMIN"); 
      mockMvc = standaloneSetup(controller).build(); 

      mockMvc.perform(get(URI.create("/processFile.html")).sessionAttr("freeTrialEmailAddress", "")).andExpect(view().name("processFile")); 
     } 

当我运行JUnit测试中,我得到一个NullPointerExcelption当我打电话的代码,我ProcessFileController.java

final Company company = companyService.getCompanyByUser(userName);

这一行,我可以看到, ServiceNull

在我ProcessFileController.javaService被声明为:

@Autowired private CompanyService companyService;

任何线索?提前致谢。

回答

3

您正在使用new关键字创建控制器。它应该是一个春季管理bean来注入它的依赖关系。使用@Autowired

@Autowired 
ProcessFileController controller; 
+0

工作就像一个魅力。谢谢。 –