JAVA && Spring && SpringBoot2.x — 學(xué)習(xí)目錄
在使用Spring項目做單元測試時,測試類的注解的含義。
idea小技巧:在任意類,任意接口名上,可以通過Ctrl+Shift+T來直接創(chuàng)建測試類,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注解:就是一個運(yùn)行器,指定測試方法在哪里運(yùn)行的,默認(rèn)的配置是:
SpringJunit4ClassRunner.class類。即Spring測試環(huán)境。RunWith(JUnit.class)就是使用JUnit來運(yùn)行。
RunWith(SpringJunit4ClassRunner.class)讓測試運(yùn)行于Spring測試環(huán)境。
RunWith(Suite.class)的話就是一套測試集合。
-
ContextConfiguration注解:導(dǎo)入配置文件。
- @ContextConfiguration(Locations="../applicationContext.xml") 導(dǎo)入配置文件路徑。
- @ContextConfiguration(classes = SimpleConfiguration.class)導(dǎo)入配置文件的class類。
-
Transactional注解:聲明事務(wù)管理器。配合@Rollback注解使用。
@Rollback(value=true),那么表示測試時如果涉及到數(shù)據(jù)庫操作,那么測試完畢,該操作會回滾,不會改變數(shù)據(jù)庫內(nèi)容。@Rollback(value=flase),那么表示測試時如果涉及到數(shù)據(jù)庫操作,那么測試完畢,測試的內(nèi)容中對數(shù)據(jù)庫的操作會真實(shí)的執(zhí)行到數(shù)據(jù)庫中,不會回滾。
1.2 SpringBoot整合Junit
1.2.1 SpringBoot對Service實(shí)現(xiàn)單元測試
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獨(dú)立博客"));
}
}
1.2.2 @SpringBootTest中注解的含義
SpringBoot中提供了一個@SpringBootTest的注解,當(dāng)您需要Spring Boot功能時,它可以用作標(biāo)準(zhǔn)
spring-test中@ContextConfiguration注解的替代方法,注解的工作原理是通過SpringApplication在測試中創(chuàng)建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
[模擬]進(jìn)行控制層的單元測試。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockXXXTest {
@Autowired
protected MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void before() {
//獲取mockmvc對象實(shí)例
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();
//輸出狀態(tài)碼
int status = result.getResponse().getStatus();
//將返回參數(shù)轉(zhuǎn)化為String
String contentAsString = result.getResponse().getContentAsString();
}
}
}
perform() [p??f??m] [執(zhí)行]執(zhí)行一個MockMvcRequestBuilders請求。其中get()表示發(fā)送get請求(可以使用get、post、put、delete等);contentType()設(shè)置請求實(shí)體類型;param()請求參數(shù),可以帶多個。andExpect()添加 MockMvcResultMatchers驗(yàn)證規(guī)則,驗(yàn)證執(zhí)行結(jié)果是否正確。andDo()添加 MockMvcResultHandlers結(jié)果處理器,這是可以用于打印結(jié)果輸出。andReturn()結(jié)果還回,然后可以進(jìn)行下一步的處理。result.getResponse().getContentAsString()將響應(yīng)內(nèi)容轉(zhuǎn)化成String字符串(JSON格式),后續(xù)可轉(zhuǎn)換為對象。
2. classpath路徑的含義
需要注意的是:@ContextConfiguration若是導(dǎo)入多個文件,可以使用{},即
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" }),而且可以使用classpath路徑。
classpath路徑到底指的是什么?
- src路徑下的文件在編譯后就會放到WEB-INF/class路徑下面,默認(rèn)的classpath就是在這里。(敲黑板,劃重點(diǎn))直接放到WEB-INF/下的話是不在classpath下的。
- 用maven構(gòu)建項目時,resources目錄就是默認(rèn)的classpath。
classpath和classpath*的區(qū)別?
* classpath只會在class路徑下查找文件;classpath*不僅包含class路勁,還包括jar文件中(class路徑)進(jìn)行查找。
* 在多個classpath中存在同名資源,都需要加載時,那么classpath只會加載第一個;classpath*都會加載。
推薦閱讀