如何创建SpringBoot代码测试类?
1、从实际的项目角度来讲,必须要求考虑到代码的测试问题,而且现在的程序代码属于SpringBoot,需要在你的项目之中进行如下的pom.xml文件修改:

2、修改microboot-base的pom.xml文件,追加SpringBoot的测试支持类。
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gwolf</groupId>
<artifactId>microboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.gwolf</groupId>
<artifactId>microboot-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microboot-base</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>microcloud-base</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encode>${project.build.sourceEncoding>}</encode>
</configuration>
</plugin>
</plugins>
</build>
</project>

3、只要进行java测试,最简单使用的就是junit,所以这个开发包一定要将测试进行导入。

4、在microboot-base建立测试程序类,测试SampleController类:TestSampleController

5、在TestSampleController类中增加测试类注解。
package org.microboot.base;
import javax.annotation.Resource;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=SampleController.class)
@WebAppConfiguration
public class TestSampleController {
@Resource
private SampleController sampleController;
@Test
public void testHome() {
TestCase.assertEquals(this.sampleController.home(), "Hello World!");
}
}

6、运行测试类,查看结果:
