2013-04-04 180 views
1

我需要爲我的spring應用程序執行junit測試,並且使用mongo數據庫完成測試。 我以前沒有用spring和mongodb嵌入junit測試的經驗。 任何答覆,將對我很有幫助...使用Spring框架和mongodb進行Junit測試的示例

感謝致以問候。

+0

感謝Steven對於回覆,我從未做過像自動化那樣的任何測試。仍然我只做了手動測試。其實我正在衝浪的一些例子在春季框架體系結構中用Mongodatabase或任何數據庫進行junit測試。如何開始實施junit測試,如何檢查控制器/服務層,如何爲添加/編輯/刪除值等進行數據庫測試, – 2013-04-04 06:06:05

+0

@ lk.annamalai如果答案對您有幫助,我會很感激您將其標記爲已接受。這會幫助我和其他人有類似的問題。謝謝 – AntonioOtero 2013-06-27 13:58:29

回答

0

我會先看看JUnit文檔,具體來說,我會從Assertions開始。當使用依賴注入框架(例如Spring)進行測試時,模擬框架是必不可少的。檢查出EasyMockMockito

0

我在Spring MVC應用程序中使用Spring mongo模板,在單元測試中使用JUnit 4.8.2。 只需爲您的mongoTemplate創建一個bean並使用Autowired將其注入到您的類中。至於測試,請按照下列步驟操作:

1.創建一個新的JUnit測試用例(右鍵單擊Package Explorer中的類,然後單擊新建 - > JUnit測試用例)。它將爲您指定的每個類的方法創建一個測試方法。

2.Now你有你在的src/test/java下測試和尤爾在的src /測試/資源資源。最好是爲測試創建一個spring配置文件,以便您可以將測試指向本地mongodb實例,將您的應用程序指向Development mongoDB實例。因此,在SRC創建一個配置文件/測試/資源並將其命名爲testSpringConfig.xml或任何與創造有豆:

<mongo:db-factory dbname="myDB" host="localhost" 
    username="myDbUser" password="myPass"/>  

    <beans:bean id="mongoTemplate" 
    class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <beans:constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
    </beans:bean> 

    <beans:bean id="mongoTemplateLibrary"  
    class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <beans:constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
    </beans:bean> 

3.In您的測試類,使用註釋引用您的配置文件:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"/testSpringConfig.xml"}) 
public class MyDaoTest { 

4.一旦你有你的測試類設置,注入蒙戈模板

@Autowired 
@Qualifier("mongoTemplate") 
private MongoTemplate mongoTemplate; 

5.現在你可以用它直接插入/刪除/發現/更新到mongo(儘管這可能是一個集成測試而不是單元測試) 例如,你可以使用Tear來移除你在測試中插入的對象向下方法:

@After 
public void tearDown() { 
    mongoTemplate.remove(myObject); 
} 
+0

嗨AntonioOtero,謝謝你的回覆。讓我試試這個,讓你知道更新很快。 – 2013-04-09 04:50:54

+0

@ lk.annamalai它是否適合你? – AntonioOtero 2013-06-25 15:48:50

+0

請參閱我使用的以下信息。 – 2013-09-24 03:47:57

0

pom.xml文件配置如下,

<properties> 
    .... 
    <junit.version>4.10</junit.version> 
    .... 
</properties> 
<dependencies> 
    ..... 
    <!-- Test dependencies --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>${junit.version}</version><!-- JUNIT CHANGES --> 
     <scope>test</scope>   
    </dependency> 

    <dependency> 
     <groupId>de.flapdoodle.embed</groupId> 
     <artifactId>de.flapdoodle.embed.mongo</artifactId> 
     <version>1.26</version> 
     <scope>test</scope> 
    </dependency>    

    <!-- Mockito --><!-- JUNIT MOCKITO DEPENDENCY ADDED --> 
    <dependency> 
     <groupId>org.mockito</groupId> 
     <artifactId>mockito-all</artifactId> 
     <version>1.8.4</version> 
    </dependency> 
    <!-- Mockito --> 
    ...... 
</dependencies> 
<build> 
     .... 
     <plugins> 
      ...... 
      <!-- JUNIT PLUGIN CODE ADDED--> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <source>1.5</source> 
       <target>1.5</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.14</version> 
      <configuration> 
     <useSystemClassLoader>false</useSystemClassLoader> 
      </configuration> 
      <dependencies> 
       <dependency> 
       <groupId>org.apache.maven.surefire</groupId> 
       <artifactId>surefire-junit47</artifactId> 
       <version>2.14</version> 
       </dependency> 
      </dependencies> 
      </plugin> 
     <!-- END OF JUINT CODE --> 
      ..... 
     </plugins> 
     <sourceDirectory>src/main/java</sourceDirectory> 
     <testSourceDirectory>src/test/junit</testSourceDirectory> 
</build>.... 

建立POM cofigurations後,執行測試用例代碼通過以下步驟

  • 假設您使用IDE的STS或其他類型,只需右鍵單擊相應的類並單擊Junit測試用例並在src/test/java路徑下創建類。
  • 爲了實現Spring控制器類,首先我們初始化所有的對象,如setUp()方法中相應類的測試用例代碼的初始化值。
  • 然後從測試用例中調用相應的方法,並在相應的方法返回值如string/int/boolean等時執行相應的junit斷言簽入,

象下面這樣:

@Test 
    public void testSave() { 
    MessageValue messageValue = saveController.saveValue(value+"::Test", model); 
    Query query = new Query(Criteria.where("value").is("Test").and("_id").is(id)); 
    SaveValue saveValueThis = template.findOne(query, Save.class); 
    assertEquals("Success", messageValue.getMessage()); 
    // delete the saved value at the end of test case 
     testDelete(); 
} 

如果返回一些UI頁面是重定向到其他屏幕,然後檢查如下圖所示,

mockMvc = MockMvcBuilders.standaloneSetup(yourController).build(); 
    mockMvc.perform(get("/request/url").accept(MediaType.ALL)) 
    .andExpect(status().isOk()); 
  • 最後的測試案例tearDown()方法重置初始化值。
相關問題