1

In a TDD (Test-Driven Development)-based Java project built by maven, lots of classes needs to be tested with text-based input files such as .csv. At the beginning, I put them into the src/test/resources directory. But along with the increase of the amount of test input .csv files, to know which input files used for which classes has become more and more difficult and messy.

A senior Javascript developer suggested to put the test files directly into the corresponding Java package probably on base of the unit test practice in Javascript. For instance, there is a class in src/main/java/com/AutoRobot.java, correspondingly there is a test class in src/test/java/com/AutoRobotTest.java and the test input csv file is also in the src/test/java/com directory.

Question: are there disadvantages to put the text-based files into the Java package, esp. when many text files are stored in the Java class package? Is it a good practice to do so? If not are there any similar alternative solutions?

11
  • tangent: I hate how certain build systems force you to put source files and resource files in different folders from each other.
    – user253751
    Apr 7 at 14:39
  • What do you think is the advantage of not putting them in the package?
    – user253751
    Apr 7 at 14:39
  • advantage of not putting them in the package: resource files are different from source files after all, so store them differenty woud make the project structure clean. This is what comes into my mind
    – Rui
    Apr 7 at 18:20
  • What does "make the project structure clean" mean?
    – user253751
    Apr 7 at 18:34
  • source files are in source directory, resource files are in resource directory. But isn't it weird that you come to ask me? I write questions here to ask people
    – Rui
    Apr 7 at 18:39

3 Answers 3

0

There are no disadvantages other than a little bit more configuration in maven maybe.

If a test file is specifically for a given test, then yes, just put it right beside it. It makes sense.

I actually put production resource files that are specific to a class or package into the src/main/java structure directly too.

This sense of "cleanliness" that sort-of forces us to separate things that otherwise clearly belong together are misguided.

2
  • how many resource files have you put into src/main/java? If so what is the sense to have the src/resources directory?
    – Rui
    Apr 7 at 19:06
  • Lots of times you have global resources. Images, default settings, i18n files, etc. I don't mind keeping those in the global src/main/resources/ hierarchy. I've put lots of things in src/main/java too though. I remember a wicket-based project, where I put the html, js, even images with the java source where the component / page was actually located. Was much easier that way. Apr 7 at 21:24
0

Question: are there disadvantages to put the text-based files into the Java package, esp. when many text files are stored in the Java class package?

It's trade offs all the way down.

The motivation for accepting the constraints of the standard layout is that general-purpose plugins just work.

Here, src/main/resources is the default value of the project.build.resources property, which in turn means that the they will be filtered and copied to project.build.outputDirectory during the process-resources lifecycle phase. See, for example, Mincong Huang's description of the resources:resources goal.

In other words, the "out-of-the-box" behavior of the copy from src/main/resources is a little bit different from the copy from src/main/java.


My litmus test is this: are you willing to do the work to justify and document your deviation from orthodoxy? I would reject a PR that had resource files under the "wrong" directory, but I would accept that same PR if it also included a record of research done into the trade offs that were considered.

0

There is no reason to put test files in the /src/main/Java/hierarchy. They can go into ../src/test/resources/com/mycompany/myproject/myclass directory. That way they will be available to your tests but will not be built into your output artifacts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.