在 Eclipse 中运行 JUnit 时自动添加属性
为了在 Eclipse 上运行单元测试,我需要为 VM 设置一些属性。
因此,当我第一次运行 JUnit 测试时,我进入“打开运行对话框”,然后在该测试的 JUnit 配置中,进入“参数”选项卡并将所需的所有内容放入“VM 参数”文本区域中。
有没有办法在运行 JUnit 时自动添加一组属性,这样我就只能右键单击测试类,然后单击“Run as > Junit Test”来运行测试?
技术信息: Eclipse 3.3.2、JUnit 4、Java 5
Edit, regarding response from Aaron Digulla:
这些属性用于 Spring 配置文件*。 因此,我不能使用 Aaron 给出的想法,因为 Spring 将在测试运行之前初始化。
除此之外,我只需要知道是否可以在 Eclipse 中以简单的方式实现这一点。 因此,该解决方案不得对 Eclipse 之外的应用程序编译产生任何影响,因为我的应用程序最终将由 Maven2 编译(和测试)。
* 很少有“单元”测试确实需要我的 Spring 配置才能运行。 好吧,我知道这不是真正的单元测试;o)
编辑 2:事实上,我确实是通过测试单元启动 Spring 配置的。 因此,在启动 Spring 之前,我检查系统属性,如果未设置我的属性,则为它们提供所需的值...
但是,令我有点失望的是 Eclipse 无法自动为我执行此操作...
In order to run my unit tests on my Eclipse, I need to set some properties for the VM.
Thus, when I first run my JUnit test, I go in "Open Run Dialog", then in my JUnit configuration for this test, I go in "Arguments" tab and put everything I need in the "VM arguments" text area.
Is there a way to automatically add a set of properties when I run my JUnit, so I will be able to only right-click on the test class, and click on "Run as > Junit Test" to run a test?
Technical information:
Eclipse 3.3.2, JUnit 4, Java 5
Edit, regarding response from Aaron Digulla:
These properties are used in Spring configuration files*. Thus, I can't use the idea given by Aaron, as Spring will be initialized before the test is run.
In addition to that, I just need to know if I can achieve that in a easy way in Eclipse. Thus the solution must not have any impact on the compilation of the application outside Eclipse, as my application will finally be compiled (and tested) by Maven2.
* few "unit" tests indeed need my Spring configuration to be run. Ok, I know that it is not real unit tests ;o)
Edit 2: In fact, I was indeed starting the Spring configuration by a test unit. Thus, before starting Spring, I check the System properties, and if my properties are not set, then I give them the required value...
However, I am a little disappointed that Eclipse can't do that for me automatically...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试这样做 - 转到
ans 选择正在使用的 JVM,然后添加一个“默认 VM”参数,例如
您可以在 TestCase 中检查:
You could try this - go to
ans select the JVM in use, than put a "Default VM" prameter like
Than you can check from within your TestCase:
我的解决方案是为扩展 TestCase 的项目中的所有测试创建一个抽象测试基类。 它必须是抽象的,这样自动单元测试查找器就不会考虑它。
在此类的静态代码块中,我设置了所需的所有属性。 这确保了代码运行一次且仅运行一次,并且它在我的项目中的任何测试之前运行。
[编辑] 你说 Spring 在测试运行之前被初始化。 这是您项目中的一个错误:它必须是初始化 Spring 的测试。 否则,你总是会遇到必须测试你无法控制的东西的问题。
因此,我建议将Spring init代码移到一个可以在环境准备好时调用它的地方。
或者,检查 setUp() 中的环境设置是否正确,如果缺少属性,则抛出错误。 这样,您至少会知道为什么稍后测试会失败。 但我仍然更喜欢完全控制哪个子系统启动。 其他任何事情都只会带来灾难。
My solution is to create an abstract test base class for all tests in a project which extends TestCase. It has to be abstract so the automatic unit test finder will not consider it.
In static code block of this class, I set all properties I need. This ensures that the code runs once and only once and that it runs before any test in my project.
[EDIT] You say that Spring is initialized before the tests run. This is a bug in your project: It must be the tests who initialize Spring. Otherwise, you will always run into the problem that you have to test something outside of your control.
Therefore, I suggest to move the Spring init code into a place where you can call it at the point in time when the environment is ready.
Alternatively, check that the environment is correctly set up in setUp() and throw an error if a property is missing. This way, you will at least know why the tests would fail later. But I still prefer to have total control when which subsystem comes to life. Anything else just begs for disaster.
当我想为我的 junit 测试设置一些属性条目时,我实现以下
属性在调用测试方法编辑之前设置
:
您还可以从文件中读取属性并读取系统属性
When i want to set some properties entries for my junit test i implement the following
The properties are set before the test methode is called
EDIT:
You also can read the properties from a file and at thes to the System properties
我一直不明白为什么启动配置有办法定义环境变量,但添加系统属性的唯一方法似乎是添加 vm 参数。
我解决这个问题的方法是让测试(或抽象测试基类)测试所需属性是否存在,如果它们不存在,那么我从类路径上的 .properties 文件加载它们。
这是有效的,因为我仍然可以覆盖它们或从 ANT 或 Maven 指定它们,但也可以“右键单击”-> 运行方式-> Junit 测试各个测试文件。
编辑:这是一个让 Spring 以与上述相同的方式选择性加载属性文件的示例:
I never understood why the launch configurations have a way to define environment variables but the only way of adding a system property seems to be to add vm arguments.
The way I've worked around this is to have tests (or an abstract tests base class) test for the presence of required properties, if they aren't there then I load them from a .properties file on the classpath.
This works as I can still override them or specify them from ANT or Maven but can also 'right click' -> Run As -> Junit Test the individual test files.
edit: here is an example of getting Spring to optionally load a properties file in the same manner as described above:
在我的一次junit测试中同意以这种方式使用方法并且它有效
Agreed used method in this way in one of my junit tests and it worked