在 Eclipse 中运行 JUnit 时自动添加属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/633700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:39:07  来源:igfitidea点击:

Automatically add properties when running JUnit in Eclipse

eclipseconfigurationjunit

提问by Romain Linsolas

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

为了在我的 Eclipse 上运行我的单元测试,我需要为 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 配置中,我进入“参数”选项卡并将我需要的所有内容放在“VM 参数”文本区域中。

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?

有没有办法在运行 JUnit 时自动添加一组属性,以便我只能右键单击测试类,然后单击“运行方式 > Junit 测试”来运行测试?

Technical information: Eclipse 3.3.2, JUnit 4, Java 5

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



Edit编辑,关于 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.

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

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.

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

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

* 少数“单元”测试确实需要运行我的 Spring 配置。好的,我知道这不是真正的单元测试;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...

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

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

但是,我有点失望 Eclipse 不能自动为我做到这一点......

采纳答案by Aaron Digulla

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.

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

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.

[编辑] 您说 Spring 在测试运行之前已初始化。这是您项目中的一个错误:初始化 Spring 的必须是测试。否则,您将始终遇到必须测试超出您控制范围的问题的问题。

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.

因此,我建议将 Spring init 代码移到环境准备好的时间点可以调用的地方。

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.

或者,检查是否在 setUp() 中正确设置了环境,如果缺少属性则抛出错误。这样,您至少会知道为什么稍后测试会失败。但我仍然更喜欢在哪个子系统出现时拥有完全的控制权。其他任何事情都只会带来灾难。

回答by siddhadev

You could try this - go to

你可以试试这个 - 去

 Window->Preferences->Java->Installed JREs

ans select the JVM in use, than put a "Default VM" prameter like

ans 选择正在使用的 JVM,而不是像这样放置“默认 VM”参数

  -DrunningInEclipse

Than you can check from within your TestCase:

您可以从 TestCase 中检查:

   System.getProperty("runningInEclipse") != null

回答by Markus Lausberg

When i want to set some properties entries for my junit test i implement the following

当我想为我的 junit 测试设置一些属性条目时,我实现了以下内容

protected void setUp() throws Exception {
        super.setUp();

        System.setProperty("Property1", "value1");
        System.setProperty("Property2", "value2");
}

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

编辑:您还可以从文件中读取属性并在这些属性中读取系统属性

回答by chillitom

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.

我一直不明白为什么启动配置有一种定义环境变量的方法,但添加系统属性的唯一方法似乎是添加 vm 参数。

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.

我解决这个问题的方法是对所需属性的存在进行测试(或抽象测试基类),如果它们不存在,那么我从类路径上的 .properties 文件中加载它们。

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.

这是有效的,因为我仍然可以覆盖它们或从 ANT 或 Maven 指定它们,但也可以“右键单击”-> 运行方式-> Junit 测试各个测试文件。

edit: here is an example of getting Spring to optionally load a properties file in the same manner as described above:

编辑:这是让 Spring 以与上述相同的方式选择性加载属性文件的示例:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="database.properties"/>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="systemPropertiesMode">
            <util:constant static-field="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        </property>
</bean>

回答by Harsh Gupta

Agreed used method in this way in one of my junit tests and it worked

在我的一项 junit 测试中同意以这种方式使用方法并且它有效


     @BeforeClass
        public static void setupProperties() {
            System.setProperty("catalina.base", "C:\sam-tomcat-7.0.42");
        }