介紹
最近在弄單元測試,之前基礎(chǔ)版本的測試基本都是寫好的,但是缺少高級版本的測試,又不想把代碼copy一份,要就地實現(xiàn)基礎(chǔ)版和高級版的測試復(fù)用,并且需要從配置讀取測試策略來指定當(dāng)前測試策略。因為基礎(chǔ)版和高級版只是大部分功能差不多,因此還要做到通過注解的方式來限定當(dāng)前測試方法或測試類是否運行于當(dāng)前測試策略。下面廢話少說,Let's Go!
實踐
之前也沒有好好深入junit,仔細(xì)想了一下,我們要做到以下兩點:
1.需要做到通過注解限定當(dāng)前測試類或測試方法是否執(zhí)行。這個使用Category注解可以實現(xiàn),但是Category的作用太過于簡單,僅僅只能做到限定測試是否執(zhí)行。
2.需要做到針對同一測試方法在不同測試策略下可以有不相同的處理,其中可能請求地址不一樣,返回結(jié)果以及斷言處理不一樣等。這個之前最初考慮的時候太過于復(fù)雜,當(dāng)時想實現(xiàn)自定義注解,然后通過aop攔截去改寫請求參數(shù)和處理流程。這樣做太過于復(fù)雜,而且不同測試策略不會同時運行。最后的做法就是將測試流程提取到接口,各自的測試策略自己去實現(xiàn),然后在初始化時,根據(jù)當(dāng)前測試策略去綁定對應(yīng)的實例,測試使用時直接使用接口就可以實現(xiàn)多策略測試的切換,再結(jié)合第一點基本上就可以滿足需求了。
主要代碼如下:
package com.lux.junit;
import com.lux.junit.category.BasicCategory;
import com.lux.junit.category.SeniorCategory;
import com.lux.junit.helper.BaseUserHelper;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JunitApplicationTests {
@Autowired
private BaseUserHelper userHelper;
@Test
public void contextLoads() {
}
@Test
@Category({BasicCategory.class, SeniorCategory.class})
public void getUserTest() {
System.out.println(userHelper.getUser());
}
@Test
@Category(SeniorCategory.class)
public void getSeniorUserTest() {
System.out.println(userHelper.getUser());
}
}
package com.lux.junit.configure;
import com.lux.junit.category.BasicCategory;
import com.lux.junit.helper.BaseUserHelper;
import com.lux.junit.helper.BasicUserHelper;
import com.lux.junit.helper.SeniorUserHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created with IntelliJ IDEA.
* User: chenfeilong
* Date: 2018/1/28
* Time: 23:30
* Description:
*/
@Configuration
public class CategoryConfigure {
@Value("${test.category}")
String currentCategory;
@Bean("userHelper")
public BaseUserHelper userHelper() {
if (BasicCategory.class.getName().equals(currentCategory)) {
return new BasicUserHelper();
} else {
return new SeniorUserHelper();
}
}
}
最后
Category需要傳遞includeCategories參數(shù),其實它可以是多個策略。通常使用命令行傳入或使用gradle傳入。但是我們需要從配置文件讀入,需要像下面這樣做。
test {
useJUnit {
includeCategories getCategory()
}
}
String getCategory() {
def category = doGetCategory()
println("current catagory: " + category)
return category
}
String doGetCategory() {
def propertiesFilePath = "src/main/resources/application.properties"
def defaultCategory = "com.lux.junit.category.BasicCategory"
File propFile = new File(propertiesFilePath)
if (propFile.canRead()) {
Properties props = new Properties()
props.load(new FileInputStream(propFile))
return props.getProperty("test.category")
}
println("can't find catagory config,use default catagory " + defaultCategory)
return defaultCategory
}
參考目錄
2.完整代碼