简体   繁体   中英

Where on a Maven build should a test framework reside - src/test/java or src/main/java?

I understand from the previous answers on this site that, for Maven builds:

  1. src/main/java will be deployed to production whereas src/test/java will not be.
  2. src/main/java contains the main application whereas src/test/java will contain code to test the main app

Now my question is, when writing a test framework, which approach is better/worse:

  1. Test framework itself will be the main app - hence it will reside on src/main/java ?
  2. Test framework will only be used to test the main app - hence will reside on src/main/java ?

Somehow getting stuck on visualizing this properly -

  • Approach 1 seems to be correct as a test framework will be the main purpose of the build hence that will be the main app.

    But then somehow I cannot imagine a test framework being deployed to production.

  • Approach 2 seems to be correct, but then if the main purpose is to write a test framework then what will go inside src/main/java ? Will that be empty or non-existing for my test framework app?

Any suggestions on this would be helpful.

I think you're going astray with

  1. src/main/java will be deployed to production whereas src/test/java will not be.

I would rephrase it as

  1. src/main/java contains the code that consumers of the module will use (it is the purpose of your module)
  • If your module is about something that an end user works with it will go into production.
  • If your module is a testing framework then it will not go into production (in the sense of being deployed onto a server or on client machines), but other modules will use it, so the testing framework should be in src/main/java

(This assumes that you build the testing framework as a separate module)

If you need real life examples, you don't need to look any further than JUnit (the legendary testing framework)

  • src/main/java contains the JUnit testing framework
  • src/test/java contains code to test the testing framework

Prefer to work with TestNG ? The code for TestNG is in a submodule "core", but even then:

  • core/src/main/java contains the TestNG testing framework
  • core/src/test/java contains code to test the testing framework

An interesting question.

Personally, I would always ship the Test Framework with the production code if the circumstances don't explicitly forbid me from doing. Why?

  1. With Test Framework, you can also include "mock" or "fake" classes that would help simplify and minimize unit testing as the mocking of complex structures tend to bloat the unit test and its gist are not often visible at the first glance.

  2. The users of the production code if shipped as JAR and loaded as a dependency might use both the Test Framework with Mock classes for their tests as well.

  3. Test Framework is yet another implementation aside from the main application in order to increase testability of both the application and the components using it as a dependency.

Summary: Unless it is said different, by default, I'd ship it together with the production application (you might put it in a separate module if you use Maven) ie src/main/java

Here is a tree of package/directory best practice for maven projects, which I hope be helpful for you:

src  --- main  ----
    |             |
    |--- test     |--- config
    |             |
    \--- site     |--- java
                  |
                  |--- scripts
                  |
                  |--- resources

So if the app is a test framework (as I understood) I think you should put it in src/main/java , where all the main java codes are.

If you write a test framework my-test-framework , you put the code into src/main/java . When you use the test framework in some JAR my-jar , you declare it as test scoped dependency. Then the code will not go to production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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