赞
踩
1.添加pom依赖,如下两个依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
2.创建测试类,范例如下:
package com.iborch.health.service.user;
import com.iborch.health.dao.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.Assert.*;
/**
* Created by lhp on 2017/9/4.
*/
@RunWith(SpringJUnit4ClassRunner.class)//指定执行类
@ContextConfiguration(locations = {"classpath*:spring-servlet.xml"})
//加载配置,将测试置于spring环境当中
public class UserServiceImplTest {
@Autowired
UserService userService;
@Resource
UserMapper userMapper;
@Before
public void setUp() throws Exception {
}
@Test
public void searchById() throws Exception {
User user = userService.searchById("029a34aa-b90a-47bb-acba-47362fd29d69");
System.out.println("测试的结果:返回用户的姓名:"+user.getName());
assertEquals("dap3533", user.getName());
}
}
3.批量执行测试类,找个包建如下类,右键运行,就实现了,@Suite.SuiteClasses({UserServiceImplTest.class})这个注解配置要执行的测试类,多个测试类之间用逗号隔开。
范例:
package com.iborch.health.service.user;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by lhp on 2017/9/5.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({UserServiceImplTest.class})
public class RunTest {
//没有任何的逻辑代码,为空
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。