单元测试与集成测试是开发人员调试代码使用的,下面是使用方式介绍:
1、pom引用:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.boge</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、项目结构:
3、src/main/java文件:
Application:
package cn.com.boge;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan("cn.com.*")
public class Application {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
PrintService:
package cn.com.boge.service;
public interface PrintService {
public void sysout();
public void sysout(String string);
}
PrintServiceImpl:
package cn.com.boge.service.impl;
import org.springframework.stereotype.Service;
import cn.com.boge.service.PrintService;
@Service
public class PrintServiceImpl implements PrintService{
@Override
public void sysout() {
System.out.println("------------PrintServiceImpl-----------");
}
@Override
public void sysout(String string) {
System.out.println("------------My name is -----------"+string);
}
}
UnitTest:
package cn.com.boge.test;
import org.junit.Before;
import org.junit.Test;
import cn.com.boge.service.PrintService;
import cn.com.boge.service.impl.PrintServiceImpl;
/**
* @author 宋波
* 该类用于单元测试
*/
public class UnitTest {
PrintService printService;
@Before
public void init(){
System.out.println("-----初始化-------");
printService = new PrintServiceImpl();
}
@Test
public void sysout(){
printService.sysout("宋波");
}
}
PrintServiceImplTest:
package cn.com.boge.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.com.boge.Application;
import cn.com.boge.service.PrintService;
/**
* @author 宋波
* 该类用于集成测试使用
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("dev")
public class PrintServiceImplTest {
@Autowired
private PrintService printService;
@Test
public void sysout() {
System.out.println("---进来了--");
printService.sysout();
System.out.println("---结束了--");
}
}
完整项目下载路径: git@github.com:MyHeartWillFoever/UnitTest.git