两种的区别:测试的位置不一样
第一种:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTests {
@Autowired
private UserService userService;
@Test
public void testAddUser() {
}
}
第二种
<!--单元测试的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
public class Test {
@Test
public void test(){
System.out.println("JUnit单元测试");
}
}
第二种 扩展
子服务
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
父项目
<!-- Junit 5 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.8.RELEASE</version>
<!--version>${spring.version}</version-->
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<!-- Junit 5 End -->
</dependencies>
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JosionMapperTest {
private SqlSession sqlSession;
private JosionUserMapper mapper;
@Before
public void setUp() throws Exception {
System.out.println("#setUp start...");
sqlSession = MybatisUtil.getSession();
mapper = sqlSession.getMapper(JosionUserMapper.class);
}
@After
public void tearDown() throws Exception {
System.out.println("#tearDown start...");
sqlSession.commit();
MybatisUtil.closeSession();
}
// @Test
public void maxId() {
JosionUserQO qo = new JosionUserQO();
qo.setTableName("T_USER0");
try {
Integer in = mapper.maxId();
System.out.println(in);
}catch(Exception e) {
System.out.println(e);
}
}