且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java / Eclipse:如何配置运行配置的JUnit测试的Classpath?

更新时间:2023-09-18 11:42:04

我的印象是,只要你有$ $ 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.