zoukankan      html  css  js  c++  java
  • springboot 集成单元测试

    官网参考地址

    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);
        }
    }
  • 相关阅读:
    项目文件.NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
    消息错误INSTALL_FAILED_INSUFFICIENT_STORAGE
    元素链表数据结构:静态链表
    对象类Android应用程序资源管理器(Asset Manager)的创建过程分析
    23种模式中的外观模式
    接口与抽象类的区别
    java中通用的数据库连接与关闭方法类的简单写法
    对Hello China操作系统比较客观的评价
    Hello China V1.68版本源代码、安装包和相应文档,欢迎下载适用
    使用Service Bus Explorer 工具来管理和测试Topics、 Queues 和 Relay Services
  • 原文地址:https://www.cnblogs.com/pangkang/p/8301003.html
Copyright © 2011-2022 走看看