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

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

问题描述

我有一个具有以下目录结构的 Eclipse 项目:

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

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

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

当我右键单击文件并转到 Run As >> 时,我试图让 WidgetTest 的测试用例(全部用 JUnit 编写)在 Eclipse 中运行.JUnit 测试.我假设我必须将它作为自定义的运行配置实际运行,并使用自己配置的类路径,但我不确定这一点,因为我以前从未这样做过.

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.

有谁知道我如何获得自定义运行配置来运行 WidgetTest.java 作为 JUnit 测试,并成功放置 src/test/config/widget-test-config.xml 在类路径上?提前致谢!

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!

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

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!

推荐答案

我的印象是只要你有 src/test/config/widget-test-config.xml 里面什么Eclipse 认为是一个源文件夹,它应该已经在类路径上.

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.

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

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

如果你把widget-test-config.xml复制到src根目录,Widget类可以读取吗?

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

那么这是测试文件夹不在类路径上的问题,您可能想尝试像这样手动添加它.

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

右键单击 WidgetTest 并选择 Run As ->Junit 测试.这应该会自动创建一个可在 Run -> 处访问的 Junit 运行配置.运行配置.您修改它的 Classpath 条目以添加包含 .xml 文件的项目,如下所示:

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 :

如果,即使在将 .xml 文件移动到 src 根目录(即默认包)之后,您的小部件类也无法读取它,那么还有其他问题.在这种情况下,如果您能在 WidgetTest 中提供试图读取 .xml 文件的代码片段,那就太好了.

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.

这是一些工作代码:

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' 打印在控制台上)

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

我正在创建一个 GITHub 存储库供您轻松试用此代码.

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

您应该能够在this<中导入项目/strong> zip 并查看代码工作.右键单击测试类 A 并单击 Run As ->Junit Test,您应该在控制台中看到两个 true.

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 测试配置运行配置的类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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