SpringBoot框架下基於Junit的單元測試
前言
Junit是一個Java語言的單元測試框架,被開發者用於實施對應用程序的單元測試,加快程序編制速度,同時提高編碼的質量。是一個在發展,現在已經到junit5,在javaEE開發中與很多框架相集成,使得開發者很方便。
Junit常用註解:
@Before:初始化方法
@After:釋放資源
@Test:測試方法,在這裏可以測試期望異常和超時時間
@Ignore:忽略的測試方法
@BeforeClass:針對所有測試,只執行一次,且必須為static void
@AfterClass:針對所有測試,只執行一次,且必須為static void
@RunWith:指定使用的單元測試執行類
Junit測試用例執行順序:
過程:就是先加載模擬的環境,再進行測試。
測試準備
依賴版本(不同版本存在一些差異)
junit 4.12
spring-test 4.3.6
spring-boot-test 1.5.1
添加依賴(必須)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12</version> </dependency>
編輯器(非必須)
IntellijIDEA
測試代碼
測試代碼如下:
import cn.yjxxclub.springboot.entity.Member; import cn.yjxxclub.springboot.mapper.MemberMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Author: 遇見小星 * Email: [email protected] * Date: 17-6-16 * Time: 下午12:18 * Describe: member應用測試類 */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MemberTest { /** * Spring RestTemplate的便利替代。你可以獲取一個普通的或發送基本HTTP認證(使用用戶名和密碼)的模板 * 這裏不使用 */ @Autowired private TestRestTemplate testRestTemplate; @Autowired MemberMapper memberMapper; /** * 2017-06-16 14:08:09.884 INFO 13803 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited size:5 -----測試完畢------- 2017-06-16 14:08:09.955 INFO 13803 --- [ Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@fd07cbb: startup date [Fri Jun 16 14:08:04 CST 2017]; root of context hierarchy */ @Test public void test(){ Map<String,Object> map = new HashMap(); map.put("start",0); map.put("size",8); List<Member> list = memberMapper.list(map); System.out.println("size:"+list.size()); System.out.println("-----測試完畢-------"); } }
代碼說明
@RunWith 是junit提供的,前言已經說了
SpringRunner是spring-test提供的測試執行單元類(SpringJUnit4ClassRunner的新名字)
@SpringBootTest is saying “bootstrap with Spring Boot’s support”,類似springboot程序的測試引導入口
具體請看spring.io解釋:
後記
在springboot1.4.1以前的版本時候,網上用如下加載方式(這個方式筆者沒試過,因為是aliyun的依賴庫1.4.1以前的已經不支持了)
@RunWith(SpringRunner.class) @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class) public class MemberTest {
- 在spring的其他項目中一般加載是
@RunWith(SpringRunner.class) @ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"}) public class MemberTest {
spring-boot-test還提供@DataJpaTest,@JsonTest,@JdbcTest註解,非常方便。
不管spring提供的有多方便,還是開始說的那句話:先加載模擬的環境,再進行測試
參考文章
https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
http://www.jianshu.com/p/c0f5545f8ba3
http://blog.csdn.net/catoop/article/details/50752964
SpringBoot框架下基於Junit的單元測試