当前位置:网站首页>如何在代码中得知是否在JUNIT环境运行?
如何在代码中得知是否在JUNIT环境运行?
2022-04-22 05:49:00 【bglmmz】
在项目中,写UT的时候,有时候需要区分正式环境和测试环境,比如在测试controller时,发现有个interceptor需要用户登录才行,那如何在UT的时候跳过用户登录校验呢?当然有很多方法,这里介绍一种方法,就是在代码中检查是否在junit环境。
public static boolean isJUnitTest() {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
if (element.getClassName().startsWith("org.junit.")) {
return true;
}
}
return false;
}
或者这样
private Boolean isRunningTest = null;
private boolean isRunningTest() {
if (isRunningTest == null) {
isRunningTest = true;
try {
Class.forName("org.junit.Test");
} catch (ClassNotFoundException e) {
isRunningTest = false;
}
}
return isRunningTest;
}
版权声明
本文为[bglmmz]所创,转载请带上原文链接,感谢
https://blog.csdn.net/bglmmz/article/details/120502310
边栏推荐
猜你喜欢
随机推荐
指针所指的地址值及指针所指对象的值(学习笔记)
茉莉X4矿池链接方法
Grafana InfluxDB 简单脚本
difflib比对两个文件
laravel数据库
Pytest(二)
Opencv recent learning test code
Solve the dislocation problem of El table scroll bar (perfect level)
JS调试干扰 - 无限debugger 绕过
JSON.
pip freeze 导出含有路径 (@ file:///) 问题小记
搞懂Promise async await 之间的联系
js 昨天,前天等某天日期
Complete binary tree
Pytest(一)
FastApi(三)
x86汇编语法:AT&T和Intel
两指针相加?(合法or不合法)
盒子塌陷 和 margin塌陷
IPI中断









