单元测试:
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());
}
}
这样就可以保证代码质量啦~