springboot如何单元测试
pom.xml加上单元测试的依赖
<!-- springboot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
加上一些注解点击运行就可以运行了
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Autowired
CategoryDAO dao;
@org.junit.Test
public void test(){
List<Category> cs = dao.findAll();
for(Category c : cs){
System.out.println("Name: " + c.name);
}
}
}
控制台显示结果
2019-11-24 11:50:22.684 INFO 7456 --- [pdateTask][T#1]] org.elasticsearch.gateway : [Katherine Reynolds] recovered [0] indices into cluster_state
2019-11-24 11:50:22.913 INFO 7456 --- [ main] com.how2java.tmall.pojo.Test : Started Test in 8.495 seconds (JVM running for 9.327)
2019-11-24 11:50:23.122 INFO 7456 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select category0_.id as id1_0_, category0_.name as name2_0_ from category category0_
Name: 瓦达瓦
Name: 123
Name: 12321
Name: 1
Name: 1
Name: 213
Name: 213
Name: 312
Name: 3
Name: 2
Name: 1
Name: 45
Name: 4
Name: 3
Name: 12
Name: 312123
Name: 123123
Name: 12
Name: 123123
Name: 12
Name: 12
Name: 12
Name: 12
Name: 12
Name: 12
Name: 12
Name: 12
Name: 请问请问
Name: 21
Name: 66
Name: 34534
Name: 1111
Name: 23123
Name: 132312
Name: 花好月圆夜
2019-11-24 11:50:23.268 INFO 7456 --- [ Thread-5] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@a370f4: startup date [Sun Nov 24 11:50:14 CST 2019]; root of context hierarchy
2019-11-24 11:50:23.271 INFO 7456 --- [ Thread-5] s.b.a.d.e.ElasticsearchAutoConfiguration : Closing Elasticsearch client
2019-11-24 11:50:23.271 INFO 7456 --- [ Thread-5] org.elasticsearch.node