SpringBoot2_Junit

准备

在这里插入图片描述

1.control

@RestController
@RequestMapping("/demo")
public class PersonController {

    @Autowired
    PersonService personService;


    @RequestMapping("/hello")
    public String hello() {
        return "suc!!";
    }

    @RequestMapping("/person")
    public Person getPerson() {
        return personService.getPerson();
    }

}

2.service

public interface PersonService {
    Person getPerson();
}

@Service
public class PersonServiceImpl implements PersonService {

    @Override
    public Person getPerson() {
        return Person.builder().pId(1).pName("测试人员").build();
    }
}

3.service

@Data
@Builder
public class Person {
    private Integer pId;
    private String pName;

}

1.Junit5简介

JUnit 5跟以前的JUnit版本不一样,它由几大不同的模块组成,这些模块分别来自三个不同的子项目。
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit平台,其主要作用是在JVM上启动测试框架。它定义了一个抽象的TestEngineAPI来定义运行在平台上的测试框架,同时还支持通过命令行、Gradle和Maven来运行平台。
  • JUnit Jupiter,包含了JUnit5最新的编程模型和扩展机制。
  • JUnit Vintage,允许在平台上运行JUnit3和JUnit4的测试用例。
  • JUnit5对Java运行环境的最低要求是Java8,同时也兼容测试旧版本JDK编译出来的代码。

在这里插入图片描述

1.1 添加依赖

切记,springboot2.2之前的版本,若只是添加@SpringBootTest注解,注入是不会生效的,也就是说2.2以后,才支持junit5

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

	<dependencies>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-test</artifactId>
	        </dependency>
	</dependencies>

1.2 使用

@SpringBootTest
@Slf4j
public class ServiceTest {

    @Resource
    PersonService personService;

    @Test
    public void testService(){
        log.info(personService.getPerson().toString());
    }

}

在这里插入图片描述

2.Junit4简介

2.1 引入依赖

这里还是使用2.1版本


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.15.RELEASE</version>
    </parent>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <!--排除junit引擎,后续可以使用spring和junit4整合的那一套-->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.2 service层访问

//Junit5这里只写SpringBootTest这个注解; 如果是junit4的话, 就要加上@RunWith(SpringRunner.class),指定了junit和spring的整合类
@RunWith(SpringJUnit4ClassRunner.class)
//需要指明当前启动类,加载上下文环境
@SpringBootTest(classes = AppMain.class)
@Slf4j
public class ServiceTest {

    @Resource
    PersonService personService;

    @Test
    public void testService(){
        log.info(personService.getPerson().toString());
    }

}

在这里插入图片描述

2.3 control层访问

@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc  //相当于new MockMvc,提供自动注入
@SpringBootTest(classes = AppMain.class)
@Slf4j
public class ServiceTest {

    //mock对象
    @Resource
    private MockMvc mockMvc;

    @Resource
    PersonService personService;

    @SneakyThrows
    @Test
    public void testControl(){

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/demo/hello"))
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        String contentAsString = mvcResult.getResponse().getContentAsString();
        log.info(contentAsString);
    }

}

在这里插入图片描述

参考

SPRING BOOT JUNIT5单元测试@EXTENDWITH无法工作

Spring Boot 基于 JUnit 5 实现单元测试

SpringBoot单元测试

SpringBoot2 + Junit5测试案例

Spring Boot+JUnit5+Mockito单元测试

springboot2 junit4测试controller

spring boot 单元测试使用new MockMvc和@RunWith+@AutoConfigureMockMvc注解的区别

Springboot 2.1.5 单元测试

spring boot(三)Junit 测试controller

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值