Search code examples
springspring-bootmavenjunitmockito

Maven Module for common Unit Test with Cyclic Reference


A simple tree of the project.

project:
├── pom.xml
│ 
├── project-algo-common
│   ├── pom.xml
│   └── src.main.java
│                  └── com.company.project
│                                     ├──── providers
│                                     │          ├──── ProviderX.java
│                                     │          ├──── ProviderY.java
│                                     │          └──── ProviderZ.java
│                                     └──── entities
│                                                ├──── X.java
│                                                ├──── Y.java
│                                                └──── Z.java
│
├── project-algo-A1
│   ├── pom.xml
│   ├── src.main.java
│   │              └── com.company.project
│   │                                 ├──── helpers
│   │                                 │          ├──── HelperPPP.java
│   │                                 │          ├──── HelperOOO.java
│   │                                 │          └──── HelperKKK.java
│   │                                 └──── computator
│   │                                            └──── ComputatorA1.java
│   └── src.test.java
│                  └── com.company.project
│                                     └──── computator
│                                                └──── ComputatorA1Test.java
│
└── project-algo-B1
    ├── pom.xml
    ├── src.main.java
    │              └── com.company.project
    │                                 ├──── helpers
    │                                 │          ├──── HelperQQQ.java
    │                                 │          ├──── HelperWWW.java
    │                                 │          └──── HelperEEE.java
    │                                 └──── computator
    │                                            └──── ComputatorB1.java
    └── src.test.java
                   └── com.company.project
                                      └──── computator
                                                 └──── ComputatorB1Test.java

The helper uses providers to retrieve some data from MongoDB.
On Unit Test the providers must be mocked in order to retrieve data from a CSV file.

So for that reason, I created ProviderXMocked, ProviderYMocked, and ProviderZMocked.
Now I want to have these classes on a Maven Module called project-algo-unit-test to be used as a dependency of project-algo-common with scope test.

The problem is that project-algo-unit-test need project-algo-common as dependency as well.

In this case, I will have a cyclic reference,

[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.company.project:project-algo-unit-test:3.0.0-SNAPSHOT'}' and 'Vertex{label='com.company.project:project-algo-common:3.0.0-SNAPSHOT'}' introduces to cycle in the graph com.company.project:project-algo-common:3.0.0-SNAPSHOT --> com.company.project:project-algo-unit-test:3.0.0-SNAPSHOT --> com.company.project:project-algo-common:3.0.0-SNAPSHOT

How can I solve it?

I know that I can extract the common part into a new module, and then use it as a dependency for both (project-algo-common, project-algo-unit-test), but it looks ugly in my opinion, to extract methods of a class into a new module.


Solution

  • As you noticed you cannot have cyclic references.

    Two possibilities:

    • Extract the module as you said.
    • If this is just part of one class, you can also copy&paste the code. I know that if you are DRY extremist this is bad, but actually I am not.