• 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);
        }
    }
  • 相关阅读:
    《英文论文写作再也不难了(工具篇),不收藏就找不到了》
    teachable-machine:探索机器学习如何工作,浏览器中实时浏览
    rasa_core:基于机器学习的对话引擎
    DeepMoji:机器学习模型分析情绪, 情感
    TensorFlow LSTM 注意力机制图解
    Fabrik – 在浏览器中协作构建,可视化,设计神经网络
    移动深度学习 Mobile-deep-learning(MDL)
    Serpent.AI
    face-alignment:用 pytorch 实现的 2D 和 3D 人脸对齐库
    ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
  • 原文地址:https://www.cnblogs.com/pangkang/p/8301003.html
Copyright © 2020-2023  润新知