2016-10-28 5 views
0

私のユニットテストでは常にヌルログインサービスを受け取りました。 はxml構成でautowiringなしでspringを使用していました。JUnit Spring with xml NoアノテーションでBeanをロードできません

junitのテストとは別に、tomcatを実行してもエラーは発生しませんでした。

私はJUnitの-4.12、hamcrestライブラリ-1.3、hamcrestコア-1.3

<util:properties location="classpath:user-credentials.properties" id="userCredentials"` /> 

<bean id="loginServiceBean" class="com.company.service.LoginService"> 
     <property name="userCredentials" ref="userCredentials" /> 
</bean> 
私のJUnitテストで

@ContextConfiguration("classpath:WEB-INF/beans.xml") 
public class LoginServiceTest { 

    private LoginService loginService; 

    @Before 
    public void setUp() throws Exception { 
    } 

    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void loginTest() { 

     User user = createUserModel(); 
     try { 
      loginService.login(user); 
     } catch (LoginException e) { 
      fail(e.getMessage()); 
     } 

    } 

    private User createUserModel() { 
     User user = new User(); 
     user.setName("user"); 
     user.setPassword("pass"); 
     return user; 
    } 

    public LoginService getLoginService() { 
     return loginService; 
    } 

    public void setLoginService(LoginService loginService) { 
     this.loginService = loginService; 
    } 
} 
+0

'loginService'が初期化されていません – Danh

答えて

1

beans.xmlの相続人は私のサンプルIを得ましたあなたのクラスでこの注釈が欠けていると思われる

@RunWith(SpringJUnit4ClassRunner.class) 

あなたは注入がうまくいくようにSpringと一緒に実行する必要があることをjunitに伝えなければなりません。

loginServiceプロパティを@AutoWiredでアナウンスし、xmlのbeanの名前をloginServiceに変更する必要があります。

あなたの属性とBeanの名前は、Springで同じでなければなりません。

関連する問題