Andy Cribbens
Andy Cribbens

Reputation: 1480

Is it considered bad practice to edit files in the src/test/resources folder

Source: Java 8
IDE: Eclipse
Testing: JUnit
Build Tool: Maven
Packaging: WAR

I need to JUnit test my code that manipulates Excel files. I was planning to keep some sample Excel files the src/test/resources folder. Using @Before I would create a new folder inside the src/test/resources (with a timestamp) and copy the sample Excel files in there. Then the tests would operate on those files and then remove the timestamp folder @After the tests.

My questions are:

  1. It is bad practice to modify files in this directory considering it is part of the codebase?

  2. Does this have implications when my code is running in CI using maven.

  3. If the answer to 1 is Yes then are there advisable alternative approaches.

Upvotes: 2

Views: 1037

Answers (2)

davidxxx
davidxxx

Reputation: 131336

It is bad practice to modify files in this directory considering it is part of the codebase?

Even if you change the content of "only" testing directory, it is not a good idea because you may produce side effects during test phase if your tests don't behave as they should (suppose they leave a test resource file not in the expected and original state).

Does this have implications when my code is running in CI using maven.

I don't see why it would be more or less a problem in this environnement. The worst is I think in the development env as you may commit undesirable things if the test execution has produced side-effects.

If the answer to 1 is Yes then are there advisable alternative approaches.

Actually, you copy data in a new folder inside the src/test/resources. What I propose you is simply copy data in a folder where you don't risk to create any side effect in the code : target/test-classes.

The test-classes dir is not used for the packaging of the artifact.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109547

Bad practice. Use the files in resources as template. That is copy them using

Path target = Files.createTemp...
Files.copy(MyClass.class.getResourceAsStream("/..."), target);

In a temp directory results will at some future moment be cleaned up, if you would like so.

Under some circumstances instead of a temporary directoy one may write to the target build directory. For instance for your own maven reporting plugin. However for tests the above should do.

Upvotes: 3

Related Questions