第一、新建maven工程,修改pom.xml,引入Jar包。
本例以**Spring Boot整合Mybatis**为基准,请先学习之后,再看下面内容。
第二、新建测试类:com.shi.test.SpringBootJunitTest.java
package com.shi.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.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import com.shi.RunApplication;
import com.shi.service.UserService;
/**
* 单元测试类
* @author shixiangcheng
* 2019-09-08
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=RunApplication.class)//属性用于指定引导类
public class SpringBootJunitTest {
@Autowired
private UserService userService;
@Test
public void testQueryList(){
System.out.println(JSON.toJSON(this.userService.queryList("大")));
}
}
运行测试类
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-09-08 21:46:10.314 INFO 6512 --- [ main] com.shi.test.SpringBootJunitTest : Starting SpringBootJunitTest on AndyShi-PC with PID 6512 (started by Andy.Shi in D:\maven\workspace\SpringBoot)
2019-09-08 21:46:10.317 INFO 6512 --- [ main] com.shi.test.SpringBootJunitTest : No active profile set, falling back to default profiles: default
2019-09-08 21:46:14.140 INFO 6512 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-08 21:46:15.016 INFO 6512 --- [ main] com.shi.test.SpringBootJunitTest : Started SpringBootJunitTest in 5.431 seconds (JVM running for 7.039)
2019-09-08 21:46:15.781 INFO 6512 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-09-08 21:46:16.635 INFO 6512 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-09-08 21:46:16.651 DEBUG 6512 --- [ main] com.shi.mapper.UserMapper.queryList : ==> Preparing: SELECT t.id id,t.name name FROM t_user t where t.name like '%大%'
2019-09-08 21:46:16.702 DEBUG 6512 --- [ main] com.shi.mapper.UserMapper.queryList : ==> Parameters:
2019-09-08 21:46:16.733 DEBUG 6512 --- [ main] com.shi.mapper.UserMapper.queryList : <== Total: 3
[{"name":"大侠","id":4},{"name":"小小大","id":5},{"name":"小小大侠客","id":6}]
2019-09-08 21:46:17.044 INFO 6512 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-09-08 21:46:17.047 INFO 6512 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-09-08 21:46:17.124 INFO 6512 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
欢迎大家积极留言交流学习心得,谢谢!