springboot項目進行單元測試


使用springboot開發項目時,通過簡單的注解可以方便地單元測試,方式如下:

一、引入springboot-test依賴

<!-- for test -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

二、在src/test/java目錄下創建測試類

由於我們在上一步引入dependence的時候指定了scope為test,所以只能在test目錄下創建測試類。

package com.test;

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.test.context.junit4.SpringRunner;

import com.winterchen.Springboot2MybatisDemoApplication;
import com.zxy.demo.redis.RedisUtil;

@SpringBootTest(classes = Springboot2MybatisDemoApplication.class)
@RunWith(SpringRunner.class)
public class RedisTest {

	@Autowired
	RedisUtil r;

	@Test
	public void reidsTest() {
		r.set("luyiming", "is a programmer");
		System.out.println(r.get("luyiming"));
	}

}

其中@SpringBootTest注解里需要寫明springboot的加載類,然后讓該測試類 Run As ->Junit Test 即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM