如何判断代码是否在JUnit测试中运行? [英] How can I find out if code is running inside a JUnit test or not?

查看:171
本文介绍了如何判断代码是否在JUnit测试中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我只有在JUnit测试中运行时才需要进行某些修复。如何判断代码是否在JUnit测试中运行?是否有类似JUnit.isRunning()== true的东西?

In my code I need to do certain fixes only when it is run inside a JUnit test. How can I find out if code is running inside a JUnit test or not? Is there something like JUnit.isRunning() == true ?

推荐答案

首先,这可能不是一个好主意。您应该对实际生产代码进行单元测试,而不是稍微不同的代码。

First of all, this a probably not a good idea. You should be unit testing the actual production code, not slightly different code.

如果你真的想这样做,你可以看一下堆栈跟踪,但既然你正在改变你的程序,你也可以介绍一个新的静态代码中的布尔字段 isUnitTesting ,并让JUnit将其设置为true。保持简单。

If you really want to do this, you could look at the stacktrace, but since you are changing your program for this anyway, you might just as well introduce a new static boolean field isUnitTesting in your code, and have JUnit set this to true. Keep it simple.

这篇关于如何判断代码是否在JUnit测试中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆