测试类:

package com.jermey;

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 org.springframework.transaction.annotation.Transactional;

import com.jermey.MyTest;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FetchFolder {
	@Autowired
	private MyTest myTest;
	@Test
	public void test() throws Exception {
        System.out.println(myTest.test());
	}
}

问题原因:

默认情况下,@SpringBootTest不会启动服务器。

解决方法:

可以使用webEnvironment 属性@SpringBootTest来进一步优化测试的运行方式:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

更多参考:

spring官网