在单元测试期间访问webapp文件夹中的文件 [英] Accessing Files In webapp Folder During Unit Testing

查看:65
本文介绍了在单元测试期间访问webapp文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上已经看到了这个问题的几种变体,但没有一个能准确描述我的情况,也没有提供适合我的解决方案.

I've seen several variations of this question on StackOverflow, but none describe my scenario exactly, or offer a solution that works for me.

我正在构建Java EE网络游戏.我在构建过程中使用JUnit进行单元测试.在我的webapp文件夹中,我拥有用户可以通过HTTP GET请求访问的资产.在我的源代码中,我具有实现这些资产逻辑的相应Java类.

I'm building a Java EE web game. I'm using JUnit to do unit testing during the build process. In my webapp folder, I have assets that users access via HTTP GET requests. In my source code, I have corresponding Java classes that implement the logic of those assets.

例如,我有一个名为CookingPot的类,该类在我的游戏中实现了烹饪锅的逻辑.在webapp文件夹中,我有一个文件,向用户提供烹饪锅的媒体和内容,例如:

For example, I have a class called CookingPot, which implements the logic for a cooking pot in my game. In the webapp folder, I have a files that provide media and content for cooking pots to the user, for example:

webapp/assets/items/CookingPot.svg (an image of the cooking pot)
webapp/assets/items/CookingPot.html (an HTML description of the cooking pot)

在单元测试期间,我想确保对于每个游戏项目(在本示例中为CookingPot项目),在webapp文件夹中的正确位置都有一个SVG和HTML文件.

During unit testing, I'd like to make sure that for every game item (in this example, the CookingPot item), there is an SVG and HTML file in the correct place in the webapp folder.

如何实施此测试?JUnit测试是在Maven构建环境中执行的,该环境不提供要测试的Tomcat服务器,因此无法通过HTTP访问它们.我尝试过:

How can I implement this test? The JUnit tests are executed in the Maven build environment, which does not provide a Tomcat server to test against, so accessing them via HTTP won't work. I've tried:

URL svg = getClass().getResource("/webapp/assets/items/CookingPot.svg");
URL svg = getClass().getResource("webapp/assets/items/CookingPot.svg");

但是那些返回null.

But those return null.

我感觉这里缺少一些简单的东西,但是我不知道它是什么.当然,必须有一种方法可以测试在构建过程中这些文件是否存在?

I have a feeling that there is something simple I'm missing here, but I don't know what it is. Surely there's got to be a way to test that these files exist during the build process?

在此先感谢您的帮助!

推荐答案

src/main/webapp 文件夹不属于类路径(默认情况下).因此,您无法使用问题中提供的代码访问它们.如果文件是 src/main/resources 的一部分,则可以通过单元测试中的 .getResource(")访问它们,而无需进一步配置.

The src/main/webapp folder is not part of the classpath (per default). Hence you can't access them with the code you provide in your question. If your files would be part of src/main/resources you could access them with .getResource("") from your Unit test without further configuration.

修改Surefire插件并包含 webapp 文件夹可以使用当前设置解决您的问题(如此处所述:

Modifying the Surefire plugin and including the webapp folder can solve your issue using your current setup (like mentioned here: Unit Test in Maven requires access to the src/main/webapp Directory):

  <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
      <additionalClasspathElements>
        <additionalClasspathElement>src/main/webapp</additionalClasspathElement>
      </additionalClasspathElements>
    </configuration>
  </plugin>

这篇关于在单元测试期间访问webapp文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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