JAVA && Spring && SpringBoot2.x — 学习目录
在使用Spring项目做单元测试时,测试类的注解的含义。
idea小技巧:在任意类,任意接口名上,可以通过Ctrl+Shift+T
来直接创建测试类,mac使用[command+N]
1. 测试方法注解的含义
1.1 Spring整合Junit
maven依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
测试类源码:
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional(transactionManager = "transactionManager")
@Rollback(value=false)
@ContextConfiguration(locations = {"classpath*:/applicationContext.xml"})
public class MultiThreadTest {
@Autowired
private ClassA classA;
@Test
public void testsetAsyncMethods() {
classA.setAsyncMethods();
}
}
含义解释:
-
RunWith注解:就是一个运行器,指定测试方法在哪里运行的,默认的配置是:
SpringJunit4ClassRunner.class
类。即Spring测试环境。RunWith(JUnit.class)就是使用JUnit来运行。
RunWith(SpringJunit4ClassRunner.class)让测试运行于Spring测试环境。
RunWith(Suite.class)的话就是一套测试集合。
-
ContextConfiguration注解:导入配置文件。
- @ContextConfiguration(Locations="../applicationContext.xml") 导入配置文件路径。
- @ContextConfiguration(classes = SimpleConfiguration.class)导入配置文件的class类。
-
Transactional注解:声明事务管理器。配合@Rollback注解使用。
@Rollback(value=true)
,那么表示测试时如果涉及到数据库操作,那么测试完毕,该操作会回滚,不会改变数据库内容。@Rollback(value=flase)
,那么表示测试时如果涉及到数据库操作,那么测试完毕,测试的内容中对数据库的操作会真实的执行到数据库中,不会回滚。
1.2 SpringBoot整合Junit
1.2.1 SpringBoot对Service实现单元测试
1. maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class LearnServiceTest {
@Autowired
private LearnService learnService;
@Test
public void getLearn(){
LearnResource learnResource=learnService.selectByKey(1001L);
Assert.assertThat(learnResource.getAuthor(),is("嘟嘟MD独立博客"));
}
}
1.2.2 @SpringBootTest中注解的含义
SpringBoot中提供了一个@SpringBootTest的注解,当您需要Spring Boot功能时,它可以用作标准
spring-test
中@ContextConfiguration
注解的替代方法,注解的工作原理是通过SpringApplication在测试中创建ApplicationContext。
- 在Spring Boot1.4以上的版本一般情况是这样的:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootStarterTests {
- 在普通Spring项目中的测试一般是这样的:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"})
public class MemberTest {
需要注意的是,在
@SpringBootTest
注解中可以指定启动类。
@SpringBootTest(classes = ApiApplication.class)
1.2.3 @AutoConfigureMockMvc注解的含义
引入MockMVC
[模拟]
进行控制层的单元测试。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockXXXTest {
@Autowired
protected MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void before() {
//获取mockmvc对象实例
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void TestXXX() throws Exception {
MvcResult result = mockMvc.perform(
MockMvcRequestBuilders.get("/xxxController/xxx_query")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.param("xxx","xxx")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
//输出状态码
int status = result.getResponse().getStatus();
//将返回参数转化为String
String contentAsString = result.getResponse().getContentAsString();
}
}
}
perform() [pəˈfɔːm] [执行]
执行一个MockMvcRequestBuilders请求。其中get()
表示发送get请求(可以使用get、post、put、delete等);contentType()
设置请求实体类型;param()
请求参数,可以带多个。andExpect()
添加 MockMvcResultMatchers验证规则,验证执行结果是否正确。andDo()
添加 MockMvcResultHandlers结果处理器,这是可以用于打印结果输出。andReturn()
结果还回,然后可以进行下一步的处理。result.getResponse().getContentAsString()
将响应内容转化成String字符串(JSON格式),后续可转换为对象。
2. classpath路径的含义
需要注意的是:@ContextConfiguration若是导入多个文件,可以使用{},即
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
,而且可以使用classpath路径。
classpath路径到底指的是什么?
- src路径下的文件在编译后就会放到WEB-INF/class路径下面,默认的classpath就是在这里。(敲黑板,划重点)直接放到WEB-INF/下的话是不在classpath下的。
- 用maven构建项目时,resources目录就是默认的classpath。
classpath和classpath*的区别?
* classpath只会在class路径下查找文件;classpath*不仅包含class路劲,还包括jar文件中(class路径)进行查找。
* 在多个classpath中存在同名资源,都需要加载时,那么classpath只会加载第一个;classpath*都会加载。
推荐阅读