在Eclipse中运行JUnit时自动添加属性 [英] Automatically add properties when running JUnit in Eclipse

查看:140
本文介绍了在Eclipse中运行JUnit时自动添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在我的Eclipse上运行我的单元测试,我需要为VM设置一些属性。

In order to run my unit tests on my Eclipse, I need to set some properties for the VM.

因此,当我第一次运行我的JUnit测试,进入打开运行对话框,然后在我的JUnit配置为这个测试,我进入参数选项卡,并把所有我需要在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.

当我运行我的JUnit时,有没有办法自动添加一组属性,所以我只能右键单击测试类,并单击运行方式> Junit测试运行测试?

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?

技术信息:
Eclipse 3.3.2,JUnit 4,Java 5

Technical information: Eclipse 3.3.2, JUnit 4, Java 5



编辑,关于Aaron Digulla的响应:


Edit, regarding response from Aaron Digulla:

这些属性用于Spring配置文件*。因此,我不能使用Aaron给出的想法,因为Spring将在运行测试之前被初始化。

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.

除此之外,我只需要知道if我可以在Eclipse中以一种简单的方式实现。因此,解决方案不必对Eclipse之外的应用程序的编译产生任何影响,因为我的应用程序将最终由Maven2编译(并测试)。

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.

*几个单位测试确实需要我的Spring配置运行。好吧,我知道它不是真正的单元测试; o)

* few "unit" tests indeed need my Spring configuration to be run. Ok, I know that it is not real unit tests ;o)

编辑2 :事实上,我确实开始了Spring配置测试单元。因此,在启动Spring之前,我检查系统属性,如果我的属性没有设置,那么我给他们所需的值...

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...

但是,我是一个没有什么失望,Eclipse不能为我自动...

However, I am a little disappointed that Eclipse can't do that for me automatically...

推荐答案

我的解决方案是创建一个抽象测试基类对于扩展TestCase的项目中的所有测试。它必须是抽象的,所以自动单元测试finder不会考虑它。

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.

你说Spring在测试运行前初始化。这是你的项目中的一个错误:它必须是初始化Spring的测试人员。否则,你总是会遇到这样的问题,你必须测试一些不受控制的东西。

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.

因此,我建议将Spring init代码移动到一个地方可以在环境准备就绪的时间点调用它。

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.

或者,检查环境是否在setUp()中正确设置,如果属性不见了。这样,你至少知道为什么测试会失败。但我仍然更喜欢有完全控制什么时候子系统生活。任何其他只是乞讨灾难。

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.

这篇关于在Eclipse中运行JUnit时自动添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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