SpringBoot项目的测试类实例解析
这篇文章主要介绍了SpringBoot项目的测试类实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.
package soundsystem;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
CDPlayerTest使用了Spring的SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。
注解@ContextConfiguration会告诉它需要在CDPlayerConfig中加载配置。
因为CDPlayerConfig类中包含了@ComponentScan,因此最终的应用上下文中应该包含CompactDiscbean。
2.
package com.baizhi.cmfz;
import com.baizhi.cmfz.dao.BannerDao;
import com.baizhi.cmfz.entiy.Banner;
import com.baizhi.cmfz.service.BannerService;
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.SpringRunner;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BannerTest {
@Autowired
private BannerDao dao;
@Autowired
private BannerService service;
@Test
public void test1(){
List<Banner> list = dao.selectAllBanner(1,10,null);
for (Banner banner : list) {
System.out.println(banner);
}
}
}
其中类SpringRunner继承了了SpringJUnit4ClassRunner
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Java中stream处理中map与flatMap的比较和使用案例
这篇文章主要介绍了Java中stream处理中map与flatMap的比较和使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-03-03
java:java.lang.ExceptionInInitializerError报错解决过程
这篇文章主要给大家介绍了关于java:java.lang.ExceptionInInitializerError报错的解决过程,java.lang.ExceptionInInitializerError 是一个异常,表示在初始化一个类的静态变量或静态块时发生了错误,需要的朋友可以参考下2023-10-10
Spring boot测试找不到SpringRunner.class的问题
这篇文章主要介绍了Spring boot测试找不到SpringRunner.class的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01
IDEA 高版本 PlantUML 插件默认主题修改的详细过程
PlantUML 是非常不错的使用脚本画图的工具,效率很高,很多人会选择在 IDEA 中安装 PlantUML Integration 插件,这篇文章主要介绍了IDEA 高版本 PlantUML 插件默认主题修改,需要的朋友可以参考下2022-09-09
Java8中stream和functional interface的配合使用详解
这篇文章主要给大家介绍了关于Java8中stream和functional interface配合使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java8具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-11-11


最新评论