2016-04-25 17 views
0

JUnitテストをSpring Controllerに開発すると、Controllerは一連のサービスを呼び出します。私のJUnitテスト・コードは上記です:Springで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テストを実行すると、私は私が私のProcessFileController.java

final Company company = companyService.getCompanyByUser(userName);

のコードのこの特定の行を呼び出すときに私がいることがわかりますNullPointerExcelptionを取得しますServiceNullです。

@Autowired private CompanyService companyService;

どれ手がかり:?私のProcessFileController.javaServiceのように宣言されて

前もって感謝します。

答えて

3

新しいキーワードを使用してコントローラを作成しています。 Springの依存関係を注入するためのSpring管理Beanでなければなりません。使用@Autowired

@Autowired 
ProcessFileController controller; 
+0

チャームのように働いた。ありがとう。 –

関連する問題