Java/Eclipse: How to configure Run Configuration&#

2019-01-23 10:20发布

问题:

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!

回答1:

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.



回答2:

If your WidgetTest class is written as a JUnit test, eclipse will try to run it as a Junit test automatically. If it doesn't, you should right click on the class in the package explorer, choose Run As >> Run Configuration >> choose Junit

To run a Junit test:

  • in JUnit3, the class should implement TestCase and all the method names should start "test"

  • in JUnit4, all the methods should be preceded by a @Test annotation

To place that config file in the classpath: when setting the Run Configuration as above, go to the Arguments tab in the upper right pane and in the VM arguments specify the classpath:

   -cp .:/path/to/the/config/file 

However, if that file is in a package in the source directory, it should automatically be included in the classpath.