springboot整合测试

第一步:添加测试的依赖

 <!--springboot整合测试  -->
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
     </dependency>
     
     <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
     </dependency>

第二步:创建测试类

package www.it.test.com;

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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import junit.framework.TestCase;
import www.it.com.controller.RedisController;

/**
* @author 作者 :wangjie
* @version 创建时间:2019年8月25日 下午3:45:54
* 类说明 @SpringBootTest指定测试的类 
        @RunWith(SpringJUnit4ClassRunner.class)指定用哪一个类进行测试
*/
@SpringBootTest(classes=RedisController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class SpringbootTest {
	
	@Autowired
	private RedisController redisController;

	public RedisController getRedisController() {
		return redisController;
	}

	public void setRedisController(RedisController redisController) {
		this.redisController = redisController;
	}
	
	/**
	 * 编写测试类
	 */
	@Test
	public void test() {
		//firstParent方法的返回值是否和期待的值一样
		TestCase.assertEquals(this.redisController.firstParent(), "第一个父级依赖的工程");
	}

}
小蘑菇
原文地址:https://www.cnblogs.com/wang66a/p/12069305.html