package com.cn.thinkchen.springbootjpa.test;
import com.cn.thinkchen.springbootjpa.SpringbootjpaApplication;
import com.cn.thinkchen.springbootjpa.dao.UserDao;
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 org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class) //就是指用SpringJUnit4ClassRunner来运行
@WebAppConfiguration //申明这是一个测试
@SpringBootTest(classes=SpringbootjpaApplication.class)//引入springboot的入口
@Transactional //开启事务
public class d {
@Autowired //自动注入
private UserDao dao;
@Test //开启测试
public void dd(){
System.out.println(dao.findAll().toString());
}
}
以上就是springbboot的测试用例
运行结果如下
2019-01-08 10:27:01.174 INFO 3880 --- [ main] com.cn.thinkchen.springbootjpa.test.d : Started d in 6.291 seconds (JVM running for 8.032)
2019-01-08 10:27:01.223 INFO 3880 --- [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@3eb81efb testClass = d, testInstance = com.cn.thinkchen.springbootjpa.test.d@4b9b5da5, testMethod = dd@d, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1ae8bcbc testClass = d, locations = '{}', classes = '{class com.cn.thinkchen.springbootjpa.SpringbootjpaApplication, class com.cn.thinkchen.springbootjpa.SpringbootjpaApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5dda768f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2aceadd4, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@23348b5d, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4961f6af], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@40d370fa]; rollback [true]
2019-01-08 10:27:01.351 INFO 3880 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select user0_.id as id1_0_, user0_.name as name2_0_ from user user0_
[com.cn.thinkchen.springbootjpa.entity.User@19855799]
2019-01-08 10:27:01.499 INFO 3880 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@3eb81efb testClass = d, testInstance = com.cn.thinkchen.springbootjpa.test.d@4b9b5da5, testMethod = dd@d, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1ae8bcbc testClass = d, locations = '{}', classes = '{class com.cn.thinkchen.springbootjpa.SpringbootjpaApplication, class com.cn.thinkchen.springbootjpa.SpringbootjpaApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5dda768f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2aceadd4, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@23348b5d, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4961f6af], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]
2019-01-08 10:27:01.504 INFO 3880 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-08 10:27:01.505 INFO 3880 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-08 10:27:01.507 INFO 3880 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-01-08 10:27:01.512 INFO 3880 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Disconnected from the target VM, address: '127.0.0.1:49535', transport: 'socket'
Process finished with exit code 0