将测试文件添加到 Netbeans 6.7.1 中的 Junit 测试 - getResource 问题,构建目录中缺少文件

发布于 2024-08-10 13:16:04 字数 784 浏览 2 评论 0原文

我正在使用 NetBeans 6.7.1 将 Junit 测试添加到我的一个类中,我需要能够加载特定于测试的 xml 文件,因此我将其包含在“测试包”文件夹中(以及我的实际测试)。我遇到了 2 个问题,

(1) getResource 在错误的目录中查找

(2) 当我运行测试时,我的 xml 测试文件没有被复制(注意,此功能适用于我将文件添加到“源包”目录)。

在我的测试课中:

this.getClass().getResource("/")

返回:

D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\classes

我也需要它返回:

D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample \build\test\classes

(因为这是编译测试类的地方)

调用 getResource,获取父级,然后在 test\classes 中查找似乎相当老套。这是获取测试文件路径的正确方法吗?也许这是 netbeans 中的一个错误?

另外,当我右键单击我的 testFile 并“运行测试”时,只有我的测试类文件被复制到 test/classes 目录,而不是我的 xml 测试文件。如何告诉 Netbeans 确保将常规 xml 文件与类文件一起复制到构建目录。

我还想避免破解 ant 构建来复制我的测试文件。

I'm adding a Junit test to one of my classes using NetBeans 6.7.1, I need to be able to load a an xml file specific to testing, so I've included it in the "Test Packages" folder (along with my actual test). I'm running into 2 issues,

(1) getResource is looking in the wrong directory

(2) my xml test file doesn't get copied when I run tests (note, this functionality works with I add files to the "Sources Packages" directory).

In my test class:

this.getClass().getResource("/")

returns:

D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\classes

I need it too return:

D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\test\classes

(As that is where the test classes are being compiled)

It seems rather hacky calling getResource, getting the parent, and then looking in test\classes. Is this the proper way of getting the path to my test file ? maybe this is a bug in netbeans ?

Also, when I right click on my testFile and "run tests" , only my test class files get copied to the test/classes directory and not my xml test file. How do I tell Netbeans to make sure to copy a regular xml file along with class files to the build directory.

I would also like to avoid hacking the ant build to copy my test files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2024-08-17 13:16:04

我最近使用 NetBeans 6.9.1 遇到了这个问题。我是这样解决的。

  1. 打开项目 -> 属性 -> 库
  2. 选择运行测试选项卡
  3. 单击添加 Jar/文件夹
  4. 导航到存储资源文件的位置
  5. 添加文件夹

现在可以使用这些资源运行测试。我使用 NetBeans 6.9.1 和一个简单的 Java 应用程序对此进行了测试。

NetBeans默认在src目录下创建资源文件(默认包),所以我在上面的第5步中添加了src文件夹。一旦您执行了在类路径中查找资源文件的测试,就会找到它。

由于 NetBeans 默认情况下会打包 src 文件夹中的资源,因此您无需复制文件并保持它们同步。

如果您希望测试资源与生产资源不同,可以将测试资源添加到test文件夹下的默认包中。然后,不要添加上面步骤 4/5 中的 src 文件夹,而是添加 test 文件夹。

I recently ran into this problem using NetBeans 6.9.1. Here's how I solved it.

  1. Open Project->Properties->Libraries
  2. Select the Run Tests Tab
  3. Click on Add Jar/Folder
  4. Navigate to where you've stored the resource files
  5. Add the folder

Now running tests using those resources will work. I tested this with NetBeans 6.9.1 and a simple Java Application.

NetBeans creates resource files in the src directory by default (default package), so I added the src folder in step 5 above. Once you do that tests looking for a resource file in the classpath will find it.

Since NetBeans packages the resources found in src folder by default, you don't need to copy the files around and keep them in sync.

If you want test resources different from production resources, you can add the test resources in the default package under the test folder. Then instead of adding the src folder in step 4/5 above, add the test folder.

揽清风入怀 2024-08-17 13:16:04

只需将您的文件与测试放在同一个包中,例如,将 data.xml 放在包 foo.bar 中。无需复制或构建脚本黑客,只需像这样引用该文件:

getClass().getResource("data.xml");

或者,您可以执行以下操作:

getClass().getResource("/foo/bar/data.xml");

Just put your file in the same package as your test, say, data.xml in package foo.bar. No copying or build script hacking is necessary, just refer to the file like this:

getClass().getResource("data.xml");

Alternatively, you can do this:

getClass().getResource("/foo/bar/data.xml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文