1

我需要为我的 spring 应用程序做一个 junit 测试,它是用 mongo 数据库完成的。我以前没有使用 spring 和 mongodb 嵌入 junit 测试的经验。任何回复,对我都有很大帮助...

感谢问候。

4

3 回答 3

0

我将从查看 JUnit 文档开始,具体来说,我将从Assertions开始。当使用依赖注入框架(例如 Spring)进行测试时,模拟框架是必不可少的。查看EasyMockMockito

于 2013-04-04T20:19:19.617 回答
0

pom.xml file Config as follows,

<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>....

After setting up the pom cofigurations, do the test case code as by the following steps

  • Assuming as you are using IDE's like STS or else, just right click on the corresponding class and click on Junit Test Case and create class under the src/test/java path.
  • For implementing for spring controller classes first we initialize all the objects like initialize values from the test case code of the corresponding class in the setUp() method.
  • Then call the corresponding methods from the test case and do the appropriate junit assertion check-in if the corresponding method returns values like string/int/boolean etc.,

Like below:

@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(); 
}

If it returns some UI page that is redirection to some other screen then check like below,

mockMvc = MockMvcBuilders.standaloneSetup(yourController).build();
    mockMvc.perform(get("/request/url").accept(MediaType.ALL))
    .andExpect(status().isOk());
  • Finally in the test case tearDown() method reset the initialized values.
于 2013-07-22T07:23:06.770 回答
0

我在 Spring MVC 应用程序和 JUnit 4.8.2 中使用 Spring mongo 模板进行单元测试。只需为您的 mongoTemplate 创建一个 bean 并使用 Autowired 将其注入您的类中。至于测试,请按照下列步骤操作:

1.创建一个新的JUnit测试用例(在Package Explorer中右击你的类,然后new->JUnit Test Case)。它将为您指定的类的每个方法创建一个测试方法。

2.现在你的测试在src/test/java和你的资源在src/test/resources。如果您仅为测试创建一个 spring 配置文件会更好,这样您就可以将测试指向本地 mongodb 实例,并将您的应用程序指向可能是 Development mongoDB 实例。因此,在src/test/resources创建一个配置文件并将其命名为testSpringConfig.xml或其他任何名称,然后在那里创建 bean:

    <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.在您的测试类中,使用注释来引用您的配置文件:

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

4.一旦你设置好你的测试类,注入mongo模板

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

5.现在你可以使用它直接插入/删除/查找/更新到mongo(尽管这将是一个集成测试而不是单元测试)例如,你可以使用Tear Down方法删除你在测试中插入的对象:

@After
public void tearDown() {
    mongoTemplate.remove(myObject);
}
于 2013-04-08T16:53:01.593 回答