• jmockit、junit


    单元测试:

      springboot maven junit集成只需引入

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>


    springboot jmockit集成 只需引入
    <dependency>
    <groupId>org.jmockit</groupId>
    <artifactId>jmockit</artifactId>
    <version>1.36</version>
    <scope>test</scope>
    </dependency>


    例子
    import com.huatai.junit.demo.dao.Car;
    import com.huatai.junit.demo.service.impl.DoCarServiceImpl;
    import mockit.Expectations;
    import org.junit.Assert;
    import org.junit.jupiter.api.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.junit4.SpringRunner;
    import org.springframework.web.client.RestTemplate;
    @RunWith(SpringRunner.class)
    @SpringBootTest
    class DoCarTest {

    @Autowired
    DoCarServiceImpl doCarService;

    @Test
    public void DemoTest1(){
    Car car = new Car();

    //断言出数据
    Car car2 = new Car();
    car2.setPrice(2000000);
    car2.setLogo("benci");
    car2.setColor("red");
    RestTemplate restTemplate = new RestTemplate();
    new Expectations(RestTemplate.class){
    {
    restTemplate.postForObject(anyString,car,Car.class);
    result = car2;
    }
    };
    Car car1 = doCarService.doCar(car);
    Assert.assertEquals("red",car1.getColor());
    Assert.assertEquals("benci",car1.getLogo());
    Assert.assertEquals(2000000,car1.getPrice());
    }

    }

    这样就可以保证代码质量啦~



    talk is cheap. show me the code.
  • 相关阅读:
    MySQL 安装
    MySQL简介
    图解十大经典的机器学习算法
    基于Python预测股价
    10_通过httprequest获取跟请求相关的信息
    07_参数提交时action的写法
    08_ServletConfig介绍
    05_servlet生命周期介绍
    06_url_pattern三种写法
    03_servletHello完成
  • 原文地址:https://www.cnblogs.com/yushizhang/p/11062952.html
Copyright © 2020-2023  润新知