第一步,在pom.xml文件中增加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
第二步,准备好启动类
package org.test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //扫描servlet注解
public class Application {
public static void main(String[] args) {
org.springframework.boot.SpringApplication.run(Application.class, args);
}
}
第三步,新建测试类,并设置注解
@RunWith: 整合Junit与spring
@SpringBootTest:加载Spring Boot启动类
在方法上用加上@test注解就可以运行测试方法了
package org.test.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.test.Application;
import org.test.entity.TestTable;
import org.test.service.TestTableService;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class) //整合junit和spring环境整合
@SpringBootTest(classes = {Application.class}) //加载Spring Boot启动类
public class GeZhongTest {
@Resource
private TestTableService service;
@Test
public void test(){
List<TestTable> list = service.findAll();
System.out.println(list.spliterator().toString());
}
}
另:
要在控制台显示mybatis执行的sql语句的配置(application.yml)如下
logging:
level:
org.test.dao : debug