我可以避免一个边缘作为测试依赖项的依赖循环吗?

发布于 2024-11-08 00:24:39 字数 1539 浏览 1 评论 0原文

考虑一个带有模块 DummyCoreTestFrameworktestCycle 父级。

TestFramework 依赖于 DummyCore,并且 DummyCoreTestFramework 具有测试依赖性。

独立maven构建和测试各个模块没有问题。但是 mvn test 父级 testCycle 结果是:

    The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.mysimpatico:TestFramework:1.0-SNAPSHOT'}' and 'Vertex{label='org.apache:DummyCore:1.0-SNAPSHOT'}' introduces to cycle in the graph org.apache:DummyCore:1.0-SNAPSHOT --> com.mysimpatico:TestFramework:1.0-SNAPSHOT --> org.apache:DummyCore:1.0-SNAPSHOT -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

重现:

wget http://dp4j.sf.net/debug/testCycle.zip
unzip testCycle.zip
cd testCycle; mvn test 

我的期望是 maven 会构建 DummyCore src,然后编译测试编译TestFramework src,它不依赖于DummyCore。在此阶段,它将编译 DummyCore src + 测试和 TestFramework src。最后它也会编译 DummyCore 测试。有没有办法告诉maven这样做? 如果没有,您将如何解决这个问题?

DummyCore 中的 tests 移动到依赖于 DummyCoreTestFramework 的自己的模块中?我这样做只是为了让专家满意。

Consider a testCycle parent with modules DummyCore and TestFramework.

TestFramework depends on DummyCore, and DummyCore has a test dedepency on TestFramework.

Building and testing each module independently maven has no problems. But mvn test the parents testCycle results in:

    The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.mysimpatico:TestFramework:1.0-SNAPSHOT'}' and 'Vertex{label='org.apache:DummyCore:1.0-SNAPSHOT'}' introduces to cycle in the graph org.apache:DummyCore:1.0-SNAPSHOT --> com.mysimpatico:TestFramework:1.0-SNAPSHOT --> org.apache:DummyCore:1.0-SNAPSHOT -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

To reproduce:

wget http://dp4j.sf.net/debug/testCycle.zip
unzip testCycle.zip
cd testCycle; mvn test 

My expectation was that maven would build DummyCore src and then coming to compile the tests will compile TestFramework src, which doesn't depend on DummyCore. At this stage it would have compiled DummyCore src + tests, and TestFramework src. Finally it will compile DummyCore tests too. Is there a way to tell maven to do this?
If not, how would you work around this?

Move the tests in DummyCore into a module of its own that depends on DummyCore and TestFramework? I'd be doing that just to satisfy maven.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜访吸血鬼 2024-11-15 00:24:39

您无法使用 Maven 或任何其他构建工具解决此冲突。这不是构建工具问题,而是架构缺陷,只能通过重构来解决。

我立即想到两个选项:

1)创建一个名为“test_common”的新模块,其中包含 TestFramework 和 DummyCore 都需要的内容。使 test_common 成为这两个模块的依赖项。

2)将TestFramework需要的东西从DummyCore移到TestFramework中。那么 TestFramework 不依赖任何东西,而 DummyCore 依赖于 TestFramework。

有很多方法可以解决这个问题,但是无论语言或构建工具如何,循环模块间依赖都是一个很重要的问题。

You can't resolve this conflict with Maven or with any other build tool. It's not a build tool issue, it is an architectural flaw and can only be addressed through refactoring.

Two options come immediately to mind:

1) Create a new module called "test_common" that contains the stuff that both TestFramework need and DummyCore need. The make test_common a dependency of both of those modules.

2) Move the stuff that TestFramework needs from DummyCore into TestFramework. Then TestFramework depends on nothing and DummyCore depends on TestFramework.

There are many ways to solve this, but circular inter-module dependencies are a big time NO-NO regardless of language or build tool.

违心° 2024-11-15 00:24:39

然而我同意循环依赖是一种糟糕的设计,有一种情况我仍然尝试做一个,它正是正如OP所要求的:测试。
我有基于我的模型的 DSL 测试类,这使我能够编写类似的内容

Book book = new Book()
assertThat(book).hasNoAuthor(); // "hasNoAuthor()" is AssertJ base assertions
verify(book, never()).addAuthor(anyAuthor()); // "anyAuthor()" is mockito based matchers

。 DSL 对象对于测试我的 Book 对象所在的 domain 模块以及使用我的 domain 的其他模块都很有用模块。

因此,我尝试创建一个 domain-test 模块以及现有的 domain 模块,但

  • domain 中的单元测试取决于 domain-test< /code> DSL 类
  • domain-test 中的 DSL 类依赖于 domain

... 循环来了。

打破这个循环的唯一方法是将单元测试从 domain 模块中提取出来:

domain-unit-tests >取决于> + 域测试

域测试 >取决于 domain

这对我来说在单独的 Maven 模块中分离代码和测试听起来非常糟糕。也许我应该成长,或者也许我应该改变我的工具...

解决方案

将所有内容保留在同一个 Maven 模块中,但创建一个仅包含 DSL 类的“测试 jar”。

需要的 2 件事

  1. 将您需要的所有类放在独立的包中

我将使用 maven-jar-plugin 的“过滤器”功能来仅导出我需要的内容。最简单的方法是将所需的类隔离在仅包含这些类的包中。

  1. 创建一个测试 jar :
<plugin>
  <!-- Build a test jar with test classes that other module might use -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
      <configuration>
        <includes>
          <include>com/myapp/testdsl/**</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

要使用测试 jar,您必须在依赖项声明中添加 type 标记:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>domain</artifactId>
  <version>2.1</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

IntelliJ 警告

在使用 IntelliJ 时,我对该解决方案有一个警告:

IDE 不采用 过滤器考虑在内,所有类都在类路径中。
这是一个已知的 IntelliJ 错误 (https://youtrack.jetbrains.com/issue/IDEA-134943 )目前尚未解决

Yet I would agree that cyle dependency is bad design, there is one case I still tried to make one and it is precisely as OP asked: testing.
I have DSL test classes based on my model which makes me able to write things like

Book book = new Book()
assertThat(book).hasNoAuthor(); // "hasNoAuthor()" is AssertJ base assertions
verify(book, never()).addAuthor(anyAuthor()); // "anyAuthor()" is mockito based matchers

There DSL objects are usefull both to test my domain module where my Book object is and other modules that consumes my domain module.

So I tried creating a domain-test module along with the existing domain module but

  • unit tests in domain depends on domain-test DSL classes
  • DSL classes in domain-test depends on domain

... Here comes the cycle.

The only way to break this cycle is to extract the unit tests out of the domain module:

domain-unit-tests > depends on > domain + domain-test

domain-test > depends on domain

This sounds terribly bad to me to separate code and tests in separated maven module. Maybe I should grow or maybe I should change my tools ...

Solution

Keep everything in the same maven module but create a 'test-jar' with only the DSL classes.

2 things needed

  1. put all the classes you need in independent packages

I will use the "filter" feature of the maven-jar-plugin to export only what I need. The easiest way to do so is to isolate the needed class in packages that only contains only these classes.

  1. create a test-jar :
<plugin>
  <!-- Build a test jar with test classes that other module might use -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
      <configuration>
        <includes>
          <include>com/myapp/testdsl/**</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

To use a test-jar, you must add the type tag in the dependency declaration :

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>domain</artifactId>
  <version>2.1</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

IntelliJ caveat

I have one caveat with that solution, when using IntelliJ:

The IDE does not take the <include> filter into account and all the classes are in the classpath.
This is a known IntelliJ bug (https://youtrack.jetbrains.com/issue/IDEA-134943) unresolved so far

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文