当前位置:   article > 正文

SpringBoot test_springboot test assert

springboot test assert

原文地址:https://www.jianshu.com/p/72b19e24a602

前言

mac上idea快捷键,command+shift+T根据类生成快捷键。

对spring容器中的类做单元测试

在src/main下建立UserService类,对其进行单于测试,生产其单元测试类(使用command+shift+T快捷键),生成的test类在src/test下

  1. @Service
  2. public class UserService {
  3. public Integer addUser(String username){
  4. System.out.println("user dao adduser [username="+username+"]");
  5. if(username == null){
  6. return 0;
  7. }
  8. return 1;
  9. }
  10. }

springboot启动类:

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class,args);
  5. }
  6. }

测试类:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class UserServiceTest {
  4. @Autowired
  5. private UserService userService;
  6. @Test
  7. public void addUser() throws Exception {
  8. Assert.assertEquals(Integer.valueOf(1),userService.addUser("zhihao.miao"));
  9. Assert.assertEquals(Integer.valueOf(0),userService.addUser(null));
  10. }
  11. }

盲点扫描

RunWith注解,SpringRunner类,SpringJUnit4ClassRunner类,SpringBootTest注解的解释。

RunWith注解

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development. While it seems powerful we expect the runner API to change as we learn how people really use it. Some of the classes that are currently internal will likely be refined and become public.
当一个类用@RunWith注释或继承一个用@RunWith注释的类时,JUnit将调用它所引用的类来运行该类中的测试而不是开发者去在junit内部去构建它。我们在开发过程中使用这个特性。

For example, suites in JUnit 4 are built using RunWith, and a custom runner named Suite:
比如说,suites使用RunWith注解构建,

  1. @RunWith(Suite.class)
  2. @SuiteClasses({ATest.class, BTest.class, CTest.class})
  3. public class ABCSuite {
  4. }

SpringRunner注解

SpringRunner is an alias for the SpringJUnit4ClassRunner.
SpringRunnerSpringJUnit4ClassRunner的一个别名。

To use this class, simply annotate a JUnit 4 based test class with @RunWith(SpringRunner.class).
使用这个类,简单注解一个JUnit 4 依赖的测试@RunWith(SpringRunner.class).

If you would like to use the Spring TestContext Framework with a runner other than
this one, use org.springframework.test.context.junit4.rules.SpringClassRule
and org.springframework.test.context.junit4.rules.SpringMethodRule.
如果你想使用Spring测试上下文而不是使用这个,你可以使用org.springframework.test.context.junit4.rules.SpringClassRuleorg.springframework.test.context.junit4.rules.SpringMethodRule.

SpringJUnit4ClassRunner

SpringJUnit4ClassRunner is a custom extension of JUnit's
BlockJUnit4ClassRunner which provides functionality of the
Spring TestContext Framework to standard JUnit tests by means of the
TestContextManager and associated support classes and annotations.
SpringJUnit4ClassRunner是JUnit's的BlockJUnit4ClassRunner类的一个常规扩展,提供了一些spring测试环境上下文去规范JUnit测试,意味着TestContextManager和支持相关的类和注解。

SpringBootTest注解

Annotation that can be specified on a test class that runs Spring Boot based tests.
Provides the following features over and above the regular Spring TestContext
Framework:
注解制定了一个测试类运行了Spring Boot环境。提供了以下一些特性:

Uses SpringBootContextLoader as the default ContextLoader when no specific ContextConfiguration#loader() @ContextConfiguration(loader=...) is defined.
当没有特定的ContextConfiguration#loader()(@ContextConfiguration(loader=...))被定义那么就是SpringBootContextLoader作为默认的ContextLoader。

Automatically searches for a SpringBootConfiguration @SpringBootConfiguration when nested @Configuration is not used, and no explicit #classes() classes are
specified.
自动搜索到SpringBootConfiguration注解的文件。

Allows custom Environment properties to be defined using the properties() properties attribute}.
允许自动注入Environment类读取配置文件。

Provides support for different #webEnvironment() webEnvironment modes,
including the ability to start a fully running container listening on a
WebEnvironment#DEFINED_PORT defined or WebEnvironment#RANDOM_PORT
random port.
提供一个webEnvironment环境,可以完整的允许一个web环境使用随机的端口或者自定义的端口。

Registers a org.springframework.boot.test.web.client.TestRestTemplate
TestRestTemplate bean for use in web tests that are using a fully running container.
注册了TestRestTemplate类可以去做接口调用。

springboot测试步骤

  • 直接在测试类上面加上如下2个注解
    @RunWith(SpringRunner.class)
    @SpringBootTest
    就能取到spring中的容器的实例,如果配置了@Autowired那么就自动将对象注入。

在测试环境中获取一个bean,在项目中新建User类,然后在测试模块进行测试

在src/main下新建一个实例User

  1. @Component
  2. public class User {
  3. }

src/test下创建测试类测试:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class UserTest {
  4. @Autowired
  5. public ApplicationContext context;
  6. @Test
  7. public void testNotNull(){
  8. Assert.assertNotNull(context.getBean(User.class));
  9. }
  10. }

只在测试环境有效的bean

在src/test下新建二个类,我们发现分别使用@TestComponent和@TestConfiguration二个注解修饰,这些类只在测试环境生效

  1. @TestComponent
  2. public class Cat {
  3. public void index(){
  4. System.out.println("cat index");
  5. }
  6. }
  1. @TestConfiguration
  2. public class TestBeanConfiguration {
  3. @Bean
  4. public Runnable createRunnable(){
  5. return () -> System.out.println("=====createRunnable=======");
  6. }
  7. }

测试类:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes = {TestBeanConfiguration.class,Cat.class})
  3. public class TestApplication {
  4. @Autowired
  5. public ApplicationContext context;
  6. @Test
  7. public void testNotNull(){
  8. Runnable runnable = context.getBean(Runnable.class);
  9. runnable.run();
  10. System.out.println("--------");
  11. Cat cat = context.getBean(Cat.class);
  12. cat.index();
  13. }
  14. }

需要在@SpringBootTest注解的参数classes中加入参数,表示将某些类纳入测试环境的容器中。

TestComponent注解

TestConfiguration注解

配置文件属性的读取

springboot会只会读取到src/test/resources下的配置,不会读到正式环境下的配置文件(跟以前1.4.*版本的不一样,以前是优先读取测试环境配置文件,然后读取正式环境的配置)

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class EnvTest {
  4. @Autowired
  5. public Environment environment;
  6. @Test
  7. public void testValue(){
  8. Assert.assertEquals("zhihao.miao",environment.getProperty("developer.name"));
  9. }
  10. }

除了在配置文件中设置属性,测试环境加载一些配置信息的二种方式:
第一种是使用@SpringBootTest注解,注解参数properties指定其value值,第二种使用EnvironmentTestUtils.addEnvironment方法进行设置。
测试:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(properties = {"app.version=1.0"})
  3. public class EnvTest2 {
  4. @Autowired
  5. private ConfigurableEnvironment environment;
  6. @Before
  7. public void init(){
  8. EnvironmentTestUtils.addEnvironment(environment,"app.admin.user=zhangsan");
  9. }
  10. @Test
  11. public void testApplication(){
  12. Assert.assertEquals("1.0",environment.getProperty("app.version"));
  13. Assert.assertEquals("zhangsan",environment.getProperty("app.admin.user"));
  14. }
  15. }

Mock方式的测试

正式环境只是一个接口,并没有实现,也并没有纳入spring容器进行管理。

  1. public interface UserDao {
  2. Integer createUser(String userName);
  3. }

测试

  1. @RunWith(SpringRunner.class)
  2. public class UserDaoTest {
  3. //使用MockBean是因为此时容器中没有UserMapper这个对象
  4. @MockBean
  5. public UserDao userDao;
  6. //使用BDDMockito对行为进行预测,
  7. @Before
  8. public void init(){
  9. BDDMockito.given(userDao.createUser("admin")).willReturn(1);
  10. BDDMockito.given(userDao.createUser("")).willReturn(0);
  11. BDDMockito.given(userDao.createUser(null)).willThrow(NullPointerException.class);
  12. }
  13. @Test(expected=NullPointerException.class)
  14. public void testCreateUser() {
  15. Assert.assertEquals(Integer.valueOf(1),userDao.createUser("admin")) ;
  16. Assert.assertEquals(Integer.valueOf(0),userDao.createUser("")) ;
  17. Assert.assertEquals(Integer.valueOf(1),userDao.createUser(null)) ;
  18. }
  19. }

对controller进行测试

第一种方式:
定义一个Controller,用作测试:

  1. @RestController
  2. public class UserController {
  3. private Logger logger = LoggerFactory.getLogger(getClass());
  4. @GetMapping("/user/home")
  5. public String home(){
  6. logger.info("user home");
  7. return "user home";
  8. }
  9. @GetMapping("/user/show")
  10. public String show(@RequestParam("id") String id){
  11. logger.info("book show");
  12. return "show"+id;
  13. }
  14. }

使用浏览器访问

  1. http://localhost:8080/user/home
  2. http://localhost:8080/user/show?id=100

使用测试类测试

  1. @RunWith(SpringRunner.class)
  2. //指定web环境,随机端口
  3. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  4. public class UserControllerTest {
  5. //这个对象是运行在web环境的时候加载到spring容器中
  6. @Autowired
  7. private TestRestTemplate testRestTemplate;
  8. @Test
  9. public void testHome(){
  10. String context = testRestTemplate.getForObject("/user/home",String.class);
  11. Assert.assertEquals("user home",context);
  12. }
  13. @Test
  14. public void testShow(){
  15. String context = testRestTemplate.getForObject("/user/show?id=100",String.class);
  16. Assert.assertEquals("show10",context);
  17. }
  18. }

第二种方式,使用@WebMvcTest注解

  1. @RunWith(SpringRunner.class)
  2. @WebMvcTest(controllers = UserController.class)
  3. public class UserControllerTest2 {
  4. @Autowired
  5. public MockMvc mockMvc;
  6. @Test
  7. public void testHome() throws Exception {
  8. mockMvc.perform(MockMvcRequestBuilders.get("/user/home")).andExpect(MockMvcResultMatchers.status().isOk());
  9. mockMvc.perform(MockMvcRequestBuilders.get("/user/home")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("user home"));
  10. }
  11. @Test
  12. public void testShow() throws Exception {
  13. mockMvc.perform(MockMvcRequestBuilders.get("/user/show").param("id", "400")).andExpect(MockMvcResultMatchers.status().isOk());
  14. mockMvc.perform(MockMvcRequestBuilders.get("/user/show").param("id", "400")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("show400"));
  15. }
  16. }

WebMvcTest

@WebMvcTest 不需要运行在web环境下,但是,需要指定controllers,表示需要测试哪些controllers。
这种方式只测试controller,controller里面的一些依赖,需要你自己去mock
@WebMvcTest 不会加载整个spring容器。

第三种方式
使用@SpringBootTest()与@AutoConfigureMockMvc结合,@SpringBootTest使用@SpringBootTest加载测试的spring上下文环境,@AutoConfigureMockMvc自动配置MockMvc这个类,

  1. /**
  2. * @SpringBootTest 不能和 @WebMvcTest 同时使用
  3. * 如果使用MockMvc对象的话,需要另外加上@AutoConfigureMockMvc注解
  4. */
  5. @RunWith(SpringRunner.class)
  6. @SpringBootTest()
  7. @AutoConfigureMockMvc
  8. public class UserControllerTest3 {
  9. @Autowired
  10. private MockMvc mvc;
  11. @Test
  12. public void testHome() throws Exception {
  13. mvc.perform(MockMvcRequestBuilders.get("/user/home")).andExpect(MockMvcResultMatchers.status().isOk());
  14. mvc.perform(MockMvcRequestBuilders.get("/user/home")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("user home"));
  15. }
  16. @Test
  17. public void testShow() throws Exception {
  18. mvc.perform(MockMvcRequestBuilders.get("/user/show").param("id", "400")).andExpect(MockMvcResultMatchers.status().isOk());
  19. mvc.perform(MockMvcRequestBuilders.get("/user/show").param("id", "400")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("show400"));
  20. }
  21. }

一个注解可以使测试类可以自动配置MockMvc这个类。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/653849
推荐阅读
相关标签
  

闽ICP备14008679号