Java/Eclipse: How to configure Run Configuration's Classpath for JUnit test?

前端 未结 2 1448
轮回少年
轮回少年 2020-12-15 14:34

I have an Eclipse project with the following directory structure:

MyProj/
    src/main/java/
        com.me.myproject.widgets
            Widget.java
    src         


        
相关标签:
2条回答
  • 2020-12-15 15:20

    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 :

    enter image description here

    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.

    0 讨论(0)
  • 2020-12-15 15:25

    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.

    0 讨论(0)
提交回复
热议问题