简体   繁体   English

Junit使用正确的资源

[英]Junit to use correct resources

When writing unit tests for the application, usually I create copy of some configurations that are in original main/resources and modify them for test purpose, but i leave the names the same. 在为应用程序编写单元测试时,通常我会创建原始main/resources的某些配置的副本,并出于测试目的对其进行修改,但我将名称保持不变。 However Junit sometimes takes the one in src and sometimes the one in the test/resources . 但是Junit有时在src中使用一个,有时在test/resources

How do we manage which one it picks up without renaming files? 我们如何管理不重命名文件的文件?

For example i have some "config.json" which is in both test and main's resources, how does junit choose which one to pick when running a test... 例如,我在测试和main的资源中都有一些“ config.json”,junit在运行测试时如何选择要选择的那个...

Ok, just a quick answer: 好吧,一个简单的答案:

You cannot exclude resources from src/main/resources . 您不能从src/main/resources排除src/main/resources They are always inside your class path and thus they are part of your program/library. 它们始终在您的类路径中,因此它们是您的程序/库的一部分。

But you shouldn't have to do so. 但是您不必这样做。

Log4j2 for example allows to have a differently named configuration file in the class path (while running the tests). 例如,Log4j2允许在类路径中(运行测试时)使用其他名称的配置文件。 Just name it log4j2-test.xml and it will be loaded prior to a non-test file. 只需将其命名为log4j2-test.xml ,它将在非测试文件之前加载。

Spring behaves the same when annotating the test classes with @ContextConfiguration . 当使用@ContextConfiguration注释测试类时,Spring的行为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM