import com.butler.websocket.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* Created by czw on 2017/6/30
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class DbTest {
private Logger logger = LoggerFactory.getLogger(DbTest.class);
@Test
public void test(){
logger.info("单元测试 ========> ~@^_^@~");
}
}
解释:
//添加SpringJUnit支持
@RunWith(SpringJUnit4ClassRunner.class)
//指定SpringBoot启动类
@SpringApplicationConfiguration(classes = SpringBootApplication.class)
//Junit模拟ServletContext需要加上WebAppConfiguration
@WebAppConfiguration
我使用的是spring boot 1.3.3版本
1.4版本以后,SpringApplicationConfiguration将被SpringBootTest取而代之