JUnit5学习之一:基本操作 (3)

写一些最简单的业务代码,首先是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工程,接下来一起为该工程编写单元测试用例;

编写测试代码

在下图红框位置新增单元测试类:

在这里插入图片描述


2. 测试类的内容如下,涵盖了刚才提到的常用注解,请注意每个方法的注释说明:

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); } /** * 不会被执行的测试方法 */ @Test @Disabled void neverExecute() { log.info("execute neverExecute"); } /** * 调用一个耗时1秒的方法,用Timeout设置超时时间是500毫秒, * 因此该用例会测试失败 */ @Test @Timeout(unit = TimeUnit.MILLISECONDS, value = 500) @Disabled void remoteRequest() { assertThat(helloService.remoteRequest()).isEqualTo(true); } }

接下来执行测试用例试试,点击下图红框中的按钮:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssyyf.html