2014-09-26 146 views
3

我想測試我的Spring網絡應用程序,但我有一些問題。春季測試和maven

我在行家

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class UserServiceTest { 

    @Autowired 
    private UserService userService; 

    @Test 
    public void testName() throws Exception { 
     List<UserEntity> userEntities = userService.getAllUsers(); 

     Assert.assertNotNull(userEntities); 
    } 
} 

添加一個測試類,但我對userService得到了NullPointerException當我嘗試運行此測試。 的IntelliJ說「無法自動裝配。‘UserService’式的無豆中。 加入@RunWith(SpringJUnit4ClassRunner.class)後,我得到這個例外

java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration 

我該如何解決呢?我想我需要在運行這個測試我的tomcat服務器,但我怎樣才能部署測試與IntelliJ?(如命令'mvn乾淨安裝tomcat7:只能運行戰爭')

+1

不需要在tomcat上運行此測試,您必須告訴它要加載哪個配置文件/類。 – 2014-09-26 09:08:07

回答

5

您必須提供您的Spring上下文文件的位置,以便在測試開始之前進行初始化。

測試類

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" }) 
public class UserServiceTest extends AbstractJUnit4SpringContextTests { 

    @Autowired 
    private UserService userService; 

    @Test 
    public void testName() throws Exception { 
     List<UserEntity> userEntities = userService.getAllUsers(); 

     Assert.assertNotNull(userEntities); 
    } 
} 

您-彈簧的context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <bean id="userService" class="java.package.UserServiceImpl"/> 

</beans> 
+0

我的春天上下文是在WEB-INF文件夾中,我可以從這裏訪問它嗎? – alvinmeimoun 2014-09-26 10:24:10

+0

spring上下文應該在src/main/resources或者src/test/resources中,但是你可以在這裏看到(http://stackoverflow.com/a/14946430/3364187)web-inf文件夾的解決方案 – Xstian 2014-09-26 10:26:36

2

我不知道,但認爲你需要位置屬性添加到ContextConfiguration標註在那裏你可以指定XML文件所有的豆都存在。此外,我猜獲取所有用戶的操作是與數據庫相關的,所以添加TransactionConfiguration註釋是很好的。因此,你應該有這樣的事情:

@ContextConfiguration(位置= { 「類路徑:彈簧的配置,junit.xml」}) @TransactionConfiguration(transactionManager的= 「transactionManager的」,defaultRollback = TRUE)

3

根據您試圖通過測試達到的目標,您需要編寫單元測試或Spring集成測試。

在您需要更改您的代碼後一種情況:

@ContextConfiguration(locations = {"classpath:/applicationContext.xml"}) 
@RunWith(SpringJUnit4ClassRunner.class) 
public class UserServiceTest { 

    @Autowired 
    private UserService userService; 

    //test whatever 
} 

進行這樣的集成測試,你可能會想鉤到Spring Profile機制。通過利用它,您將能夠在選擇性地替換某些bean(例如通過將生產數據源交換到內存數據庫中)的同時重用您在生產代碼中使用的相同配置。

如果你使用Spring 4,那麼Conditional會給你更多的靈活性。

我建議你給this部分Spring測試文檔一個很好的閱讀,以獲得Spring集成測試可以爲你做什麼的概述。


你介紹的情況我不知道權證本身的集成測試(詳情什麼UserService實際上並會給一個更清晰的畫面)。

例如,如果UserService使用UserDao,然後對從DAO結果一些定製邏輯,具有嘲笑UserDao創建UserService一個單元測試將是一個很好的選擇。

如果您正在使用構造函數注入來向服務提供dao,那麼構建該服務是毫不費力的。如果您通過@Autowired使用現場注入(如果您可以避開它,則不應該這樣做),那麼您需要通過反射注入模擬。一個超級實用工具是Spring的ReflectionTestUtils

然後你可以創建一個集成測試UserDao這將測試它的能力,正確地從數據庫中讀取數據。


最後請注意,上面的註釋獨立於您如何運行測試(IDE或Maven)。如果配置正確,工具將會很好地運行它