xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
simplebean
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
com.bolingcavalry
junitpractice
1.0-SNAPSHOT
pom
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
org.projectlombok
lombok
1.16.16
本篇的源码工程
接下来咱们准备一个简单的SpringBoot工程用于做单元测试,该工程有service和controller层,包含一些简单的接口和类;
- 创建名为junit5experience的子工程,pom.xml如下,注意单元测试要依赖spring-boot-starter-test:
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.bolingcavalry
junitpractice
1.0-SNAPSHOT
…/pom.xml
com.bolingcavalry
junit5experience
0.0.1-SNAPSHOT
junit5experience
Demo project for simplebean in Spring Boot junit5
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-webflux
test
org.springframework.boot
spring-boot-maven-plugin
- 写一些最简单的业务代码,首先是service层的接口HelloService.java:
package com.bolingcavalry.junit5experience.service;
public interface HelloService {
String hello(String name);
int increase(int value);
/**
-
该方法会等待1秒后返回true,这是在模拟一个耗时的远程调用
-
@return
*/
boolean remoteRequest();
}
- 上述接口对应的实现类如下,hello和increase方法分别返回String型和int型,remoteRequest故意sleep了1秒钟,用来测试Timeout注解的效果:
package com.bolingcavalry.junit5experience.service.impl;
import com.bolingcavalry.junit5experience.service.HelloService;
import org.springframework.stereotype.Service;
@Service()
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello " + name;
}
@Override
public int increase(int value) {
return value + 1;
}
@Override
public boolean remoteRequest() {
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
return true;
}
}
- 添加一个简单的controller:
package com.bolingcavalry.junit5experience.controller;
import com.bolingcavalry.junit5experience.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
public String hello(@PathVariable String name){
return helloService.hello(name);
}
}
- 启动类:
package com.bolingcavalry.junit5experience;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Junit5ExperienceApplication {
public static void main(String[] args) {
SpringApplication.run(Junit5ExperienceApplication.class, args);
}
}
- 以上就是一个典型的web工程,接下来一起为该工程编写单元测试用例;
编写测试代码
- 在下图红框位置新增单元测试类:
- 测试类的内容如下,涵盖了刚才提到的常用注解,请注意每个方法的注释说明:
package com.bolingcavalry.junit5experience.service.impl;
import com.bolingcavalry.junit5experience.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@Slf4j
class HelloServiceImplTest {
private static final String NAME = “Tom”;
@Autowired
HelloService helloService;
/**
- 在所有测试方法执行前被执行
*/
@BeforeAll
static void beforeAll() {
log.info(“execute beforeAll”);
}
/**
- 在所有测试方法执行后被执行
*/
@AfterAll
static void afterAll() {
log.info(“execute afterAll”);
}
/**
- 每个测试方法执行前都会执行一次
*/
@BeforeEach
void beforeEach() {
log.info(“execute beforeEach”);
}
/**
- 每个测试方法执行后都会执行一次
*/
@AfterEach
void afterEach() {
log.info(“execute afterEach”);
}
@Test
@DisplayName(“测试service层的hello方法”)
void hello() {
log.info(“execute hello”);
assertThat(helloService.hello(NAME)).isEqualTo("Hello " + NAME);
}
/**
- DisplayName中带有emoji,在测试框架中能够展示
*/
@Test
@DisplayName(“测试service层的increase方法\uD83D\uDE31”)
void increase() {
log.info(“execute increase”);
assertThat(helloService.increase(1)).isEqualByComparingTo(2);
}
/**
- 不会被执行的测试方法
*/
写在最后
很多人感叹“学习无用”,实际上之所以产生无用论,是因为自己想要的与自己所学的匹配不上,这也就意味着自己学得远远不够。无论是学习还是工作,都应该有主动性,所以如果拥有大厂梦,那么就要自己努力去实现它。
最后祝愿各位身体健康,顺利拿到心仪的offer!
由于文章的篇幅有限,所以这次的蚂蚁金服和京东面试题答案整理在了PDF文档里
i,在测试框架中能够展示
*/
@Test
@DisplayName(“测试service层的increase方法\uD83D\uDE31”)
void increase() {
log.info(“execute increase”);
assertThat(helloService.increase(1)).isEqualByComparingTo(2);
}
/**
- 不会被执行的测试方法
*/
写在最后
很多人感叹“学习无用”,实际上之所以产生无用论,是因为自己想要的与自己所学的匹配不上,这也就意味着自己学得远远不够。无论是学习还是工作,都应该有主动性,所以如果拥有大厂梦,那么就要自己努力去实现它。
最后祝愿各位身体健康,顺利拿到心仪的offer!
由于文章的篇幅有限,所以这次的蚂蚁金服和京东面试题答案整理在了PDF文档里
[外链图片转存中…(img-8tDUzwyN-1714408133305)]
[外链图片转存中…(img-GghIETMa-1714408133306)]
[外链图片转存中…(img-ptz8IvgI-1714408133307)]