Java / Eclipse:如何配置运行配置的JUnit测试的Classpath? [英] Java/Eclipse: How to configure Run Configuration's Classpath for JUnit test?

查看:225
本文介绍了Java / Eclipse:如何配置运行配置的JUnit测试的Classpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MyProj / 
src / main / java /
com.me.myproject.widgets
Widget.java
src / main / config
widget-config.xml
src / test / java
com .me.myproject.widgets
WidgetTest.java
src / test / config
widget-test-config.xml

Widget 类从类路径中的任何位置读取其配置(XML)文件,并使用它来配置其属性。 / p>

我试图获取 WidgetTest 的测试用例(全部用JUnit编写)在Eclipse中运行,当我右键单击该文件,然后转到运行方式>> JUnit测试。我假设我必须使用自己配置的类路径来实际运行它作为一个定制的运行配置,但是我不确定,因为我从来没有这样做。



有没有人知道如何获得一个自定义运行配置来运行 WidgetTest.java 作为JUnit测试,并成功地放置<$ c在类路径上$ c> src / test / config / widget-test-config.xml 感谢提前!



请注意,这个问题是不是关于如何从运行时类路径读取资源,它关于如何获取它在Eclipse的JUnit Run Config类路径中首先!

解决方案

我的印象是,只要你有$ $ c> src / test / config / widget-test-config.xml 在Eclipse认为是源文件夹的内部,它应该已经在类路径上了。



src / test是Eclipse的源文件夹吗?如果是,并且仍然遇到问题,请尝试以下实验:



如果将widget-test-config.xml复制到src根,则Widget类会读取它?



如果是



那么测试文件夹不在类路径上,您可能会想尝试手动添加它,如此。



右键单击WidgetTest,然后选择运行方式 - > Junit Test 。这应该会自动创建一个Junit运行配置,可以在 Run - >运行配置。您修改它的Classpath条目以添加包含.xml文件的项目,如下所示:





如果否



.xml文件到src根(即默认包),你的widget类无法读取,那么还有其他的错误。在这种情况下,如果您可以在WidgetTest中提供试图读取.xml文件的代码片段,那将是非常好的。



工作代码



这里有一些工作代码:

  public class A {

@Test
public void test(){
InputStream stream = A.class.getResourceAsStream(/ SomeTextFile.txt);
System.out.println(stream!= null);
stream = Test.class.getClassLoader()
.getResourceAsStream(SomeTextFile.txt);
System.out.println(stream!= null);
}

}

以上内容适用于我简单的JAVA项目运行正常。 (运行良好意味着在控制台上打印
'true')



我正在为您创建一个GITHub repo,无需试用此代码



GIT Hub Repo with Test project



您应该可以在一个href =https://github.com/ajorpheus/stack-overflow/zipball/master =noreferrer>这个 zip,看看代码的工作。右键单击测试类 A ,然后单击运行方式 - > Junit Test ,您应该在控制台中看到两个 true


I have an Eclipse project with the following directory structure:

MyProj/
    src/main/java/
        com.me.myproject.widgets
            Widget.java
    src/main/config
        widget-config.xml
    src/test/java
        com.me.myproject.widgets
            WidgetTest.java
    src/test/config
        widget-test-config.xml

The Widget class reads its config (XML) file in from anywhere on the classpath and uses it to configure its properties.

I am trying to just get WidgetTest's test cases (all written with JUnit) to run inside Eclipse when I right-click the file and go to Run As >> JUnit Test. I assume I'll have to actually run it as a customized Run Configuration with its own configured classpath, but I'm not sure about that as I've never done this before.

Does anybody know how I can get a custom Run Configuration to run WidgetTest.java as a JUnit test, and successfully place src/test/config/widget-test-config.xml on the classpath? Thanks in advance!

Please note, this question is not about how to read a resource from the runtime classpath, its about how to get it on Eclipse's JUnit Run Config classpath in the first place!

解决方案

I was under the impression that as long as you have src/test/config/widget-test-config.xml inside what Eclipse considers to be a source folder, it should already be on the classpath.

Is src/test a source folder for Eclipse ? If it is and you still get the problem, try out the following experiment :

If you copy widget-test-config.xml to the src root can Widget class read it ?

If Yes

then it's a problem of the test folder not being on the classpath and you may wanna try adding it manually like so.

Right click WidgetTest and select Run As -> Junit Test. This should automatically create a Junit Run Configuration accessible at Run -> Run Configurations. You modify it's Classpath entry to add the project containing the .xml file like so :

If No

If, even after moving the .xml file to the src root (i.e. default package), your widget class cannot read it then there is something else wrong. In that case, it would be great if you could furnish the snippet of code in WidgetTest which is trying to read the .xml file.

Working Code

Here is a bit of working code :

public class A {

    @Test
    public void test() {
        InputStream stream = A.class.getResourceAsStream("/SomeTextFile.txt");
        System.out.println(stream != null);
        stream = Test.class.getClassLoader()
            .getResourceAsStream("SomeTextFile.txt");
        System.out.println(stream != null);
    }

}

The above works for me in a simple JAVA project and runs fine. (Running fine means getting 'true' printed on the console)

I am in the process of creating a GITHub repo for you to try out this code painlessly.

GIT Hub Repo with Test project

You should be able to import the project in this zip and see the code working. Right click on the Test class A and click Run As -> Junit Test, and you should see two true in the Console.

这篇关于Java / Eclipse:如何配置运行配置的JUnit测试的Classpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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