1. 添加依赖
<!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
添加完依赖以后如果POM文件中报错,类型
Failure to transfer org.hamcrest:hamcrest-core:jar:1.3 .... was chached in the local repository
这是说在本地有缓存,把本地repository里对应的包删除掉,然后右键项目->maven->update project,报哪个包的错就删掉那个包。
造成这个错误的原因是对应的包下存在有 .lastupdated文件,删掉这些文件就可以。
stackoverflow上有类似解决办法。
2.简单JAVA测试
在方法上加@Test注解,run as Junit即可
package UtilitiesTest; import org.junit.Assert; import org.junit.Test; public class SimpleJavaTest { @Test public void EquelTest(){ Integer i = 128; Integer j = 128; Assert.assertEquals(true, i != j); } }
结果
3. 测试service
写测试类:
package ServiceTest; import java.util.ArrayList; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.pkyou.Sample.Main; import com.pkyou.Sample.Entyties.IndoorCheckItemEntity; import com.pkyou.Sample.ServiceImp.ControllerService; @RunWith(SpringRunner.class) @SpringBootTest(classes = Main.class) @AutoConfigureMockMvc public class ControllerServiceTest { @Autowired private ControllerService service; private ArrayList<IndoorCheckItemEntity> entities; @Test public void ResultTest(){ entities = service.GetIndoorCheckItemEntities(); Assert.assertNotNull(entities); Assert.assertEquals(3, entities.size()); } }
以上注解意义,测试方法参考官网。这种测试方法每次都会启动tomcat
测试结果:
4. 测试controller
测试类
package ControllerTest; import java.net.URL; import java.util.ArrayList; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import com.pkyou.Sample.Main; import com.pkyou.Sample.Entyties.IndoorCheckItemEntity; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Main.class) public class HelloControllerTest { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void SetUp() throws Exception { this.base = new URL("http://192.168.7.11:" + port + "/controller/GetIndoorCheckItemEntities"); } @Test public void GetIndoorInfo() throws Exception{ ResponseEntity<Object> entities = template.getForEntity(base.toString(),Object.class); ArrayList<IndoorCheckItemEntity> body = (ArrayList<IndoorCheckItemEntity>)entities.getBody(); Assert.assertNotNull(body); Assert.assertEquals(body.size(), 3); } }