前言

  • springboot 2.0.0.RELEASE
  • maven 3.5.0
  • 参考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-testing

引入dependency

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

Junit测试类:简写

Junit测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestProperties4Debug {private Logger log = LoggerFactory.getLogger(this.getClass());@Value("${debug}")private boolean isDebug;@Testpublic void t1() throws Exception {this.log.debug("debug : {}", this.isDebug);System.out.println(this.isDebug);}
}
  • SpringBootTest 默认寻找 @SpringBootConfiguration。寻找顺序:src/test/java > src/main/java。

    • @SpringBootApplication 包含 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan)
    • @SpringBootConfiguration 包含 @Configuration
  • SpringBootTest 默认寻找 application.properties。寻找顺序:src/test/resources > src/main/resources。

Junit测试类:@ContextConfiguration、@TestPropertySource

Junit测试类

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes=Application.class)
@TestPropertySource("classpath:application.properties")
public class TestProperties4Debug2 {@Value("${debug}")private boolean isDebug;@Testpublic void t1() throws Exception {Assert.assertTrue(this.isDebug);}}

Application类

@SpringBootApplication
public class Application {public static void main(String[] args) throws IOException {SpringApplication.run(Application.class, args);}@Beanpublic CommandLineRunner commandLineRunner(ApplicationContext ctx) {return args -> {System.out.println("Application startup done.");};}
}

application.properties

debug=true

【sprinb-boot】Junit测试相关推荐

  1. Spring Boot JUnit 测试 Controller

    Spring Boot JUnit 测试 Controller Controller层代码如下: @RestController public class HelloController {Logge ...

  2. spring boot(三)Junit 测试controller

    Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法 一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期 ...

  3. Maven找不到要运行的JUnit测试

    我有一个Maven程序,它可以正常编译. 当我运行mvn test它不会运行任何测试(在TESTs标头下显示There are no tests to run. ). 我已经用一个非常简单的设置重新创 ...

  4. java junit autowired_写Junit测试时用Autowired注入的类实例始终为空怎么解?

    踩坑半天多,终于在网上寻觅到了解决方案,特此分享一下. 重要前提:src/main/java下的根包名必须和src/test/main的根包名完全一致,否则就会发生死活不能注入的情况,要继续进行下面的 ...

  5. junit测试spring_使用Spring JUnit规则进行参数化集成测试

    junit测试spring Spring 4.2附带了全新的JUnit规则: SpringClassRule和SpringMethodRule . 使用JUnit规则的主要优点是让开发人员摆脱Spri ...

  6. 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试

    From: https://blog.wuwii.com/springboot-test.html 前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提 ...

  7. SpringBoot入门系列: Spring Boot的测试

    Spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具.当然,这里的测试,主要还是后台代码的测试. 主要需要注意的地方仅有三点: 1.依赖包的引入:pom.xml中仅依赖s ...

  8. SpringBoot集成JUnit测试

    在一些企业的实践中,要求开发人员编写测试编码来测试业务逻辑,以提高编码的质量.降低错误的发生概率以及进行性能测试等.这些IDE在创建Spring Boot应用的时候已经引入了测试包,只需要看到pom. ...

  9. SpringBoot整合Junit测试

    文章目录 SpringBoot整合Junit测试 1.SpringBoot引入springboot的测试依赖 2.生成测试方法 3.测试结果 SpringBoot整合Junit测试 假设已对mybat ...

最新文章

  1. JBoss 系列十六:JBoss7/WildFly配置domain模式
  2. 求s = k ! + n ! / m !的值
  3. 《智源社区周刊:预训练模型》第2期:Facebook用“预微调”改进预训练性能、再议GPT-3中的东方主义偏见...
  4. python推荐淘宝物美价廉商品 2.0
  5. ir指令、立即数的作用_立即数的判断方法一
  6. MyBatis中多表查询(多表查询语句实现)重点
  7. 唐山师范学院计算机二级报名,2017年3月唐山师范学院计算机等级考试报名时间(河北)...
  8. bugku 杂项 就五层你能解开吗_长春老旧小区加装电梯,你家符合条件吗?_媒体_澎湃新闻...
  9. gin c.Next()方法
  10. 在Android中处理屏幕布局变化
  11. 【金融】【python】三因子(three factor)简单模型实证
  12. c语言第1次作业答案,C语言第五次上机作业参考答案
  13. 【格雷码】LeetCode 89. Gray Code
  14. python只保留大写字母_python - 匹配某一行并保留大写字母?
  15. Autodesk Eagle入门之-开启旅程
  16. 港台术语与内地术语之对照
  17. 华为eSight网络监控平台配置短信功能说明
  18. 计算机网络基础之互联网总结
  19. Linux运维遇到的基本问题解决大全
  20. 语音识别笔记(二)计算音系学

热门文章

  1. Golang学习笔记——Slice
  2. ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系
  3. [Elasticsearch2.x] 多字段搜索 (三) - multi_match查询和多数字段 译
  4. Python基础教程---读书笔记四
  5. 划分VLAN,根据每个VLAN通过DHCP分配IP地址
  6. 配置apache虚拟主机
  7. flex module不编译的问题
  8. Maven根据不同环境打包不同配置文件
  9. svn文件重进更改服务器路径,svn 设置 服务器路径
  10. git命令之git tag 给当前分支打标签