简体   繁体   English

Maven-如何对依赖于另一个项目的项目进行单元测试?

[英]Maven - How to unit test a project dependent on another project?

I know this question has been asked numerous times but the methods I've read just doesn't work for me. 我知道这个问题已被问过无数次,但是我所读的方法对我不起作用。 I tried this but it still won't work. 我试过了,但还是行不通。

I have a subproject(A) that depends on another subproject(B). 我有一个子项目(A),它依赖于另一个子项目(B)。

Both subprojects are contained in a parent directory with a parent pom.xml (declares A and B as modules) within their respective subdirectories. 这两个子项目都包含在一个父目录中,在其各自的子目录中带有一个父pom.xml(将A和B声明为模块)。

Compiling and installing the project works fine with the maven-assembly-plugin but when I try to test A, it doesn't recognize the classes from B. I have tried installing it first and then testing but it still won't find the classes. 使用maven-assembly-plugin编译和安装项目效果很好,但是当我尝试测试A时,它无法识别来自B的类。我尝试先安装它然后进行测试,但仍然找不到类。 What am I missing? 我想念什么?

Error: 错误:

 [ERROR] Failed to execute goal
 org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
 (default-testCompile) on project (A)

[ERROR] TestService.java:[8,17] cannot find symbol 

[ERROR]   symbol:   class **Model** (the class I'm referencing in B)

[ERROR]   location: class **TestService** 
(test in A that tests the class in ../service/src/main/java/Service.java)

Edit: 编辑:
/project /项目
--/service (this depends on model;this is also what I want to test) -/服务(这取决于型号;这也是我要测试的内容)
---/src --- / src
----/main - - /主要
-----/java ----- / java
------/Service.java ------ / Service.java
----/test - - /测试
-----/java ----- / java
------/TestService.java ------ / TestService.java

--/model (independent) -/型号(独立)
---/src --- / src
----/main - - /主要
-----/java ----- / java
------/Model.java ------ / Model.java

--/entry (this depends on service; entry point of the whole project) -/入口(取决于服务;整个项目的入口)
--pom.xml (parent pom) --pom.xml(父pom)

Each of the three projects have their own pom.xml inside. 这三个项目中的每一个都有自己的pom.xml。

/model/pom.xml contains no dependencies and no plugins /model/pom.xml不包含依赖项,也不包含插件

here's parent: parent/pom.xml 这是父母:parent / pom.xml

  ...
  <modules>
    <module>entry</module>
    <module>service</module>
    <module>model</module>
  </modules>

here's entry: 这里的条目:

 /service/pom.xml
        ...
    <parent>
        <groupId>com.some.project</groupId>
        <artifactId>project</artifactId>
        <version>xx</version>
    </parent>
    <artifactId>entry</artifactId>
    <packaging>jar</packaging>
    <version>xx</version>
    <name>entry</name>
    <build>
     ...
     <!--assembly plugin is declared here-->
    </build>
    <dependencies>
      <dependency>
      <groupId>com.some.project</groupId>
      <artifactId>service</artifactId>
      <version>xx</version>
      </dependency>
    </dependencies>

here's service: 这里的服务:

/service/pom.xml
    ...
<parent>
    <groupId>com.some.project</groupId>
    <artifactId>project</artifactId>
    <version>xx</version>
</parent>
<artifactId>service</artifactId>
<packaging>jar</packaging>
<version>xx</version>
<name>service</name>
<dependencies>
  <dependency>
  <groupId>com.some.project</groupId>
  <artifactId>model</artifactId>
  <version>xx</version>
  </dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
</dependencies>

Maven assembly plugin is used usually when you want to package the already compiled sources, so its kind of not related here. 当您要打包已编译的源代码时,通常使用Maven程序集插件,因此此处与之无关。

If you have two projects, A and B and B has to depend on A, you have to define in B's pom.xml a dependency (trivial stuff): 如果您有两个项目,A和B,而B必须依赖A,则必须在B的pom.xml中定义一个依赖项(琐碎的事情):

<dependency>
  <groupId>YOUR_GROUP_ID</groupId>
  <artifactId>A</artifactId>
  <version>YOUR_VERSION</version>
</dependency>

This will instruct maven to setup classpath during the build. 这将指示Maven在构建过程中设置类路径。

Now, Depending on artifact type of A and B maven can decide on some steps after the compilation, for example, if B is a WAR, A will be included in WEB-INF/lib folder of B because of such a dependency. 现在,根据A和B的工件类型,maven可以决定编译后的某些步骤,例如,如果B是WAR,则由于这种依赖性,A将包含在B的WEB-INF / lib文件夹中。

But in general, if A & B are jars, maven won't use this information only for the compilation/tests and not for packaging. 但是总的来说,如果A和B是罐子,那么maven不会仅将此信息用于编译/测试,而不会用于包装。

Now, maven has different classpaths for different phases: in particuler, one for compilation, and one for unit tests. 现在,maven在不同阶段具有不同的类路径:在分词器中,一个用于编译,一个用于单元测试。

So if you want to specify that the dependency is required only for tests but should not be considered as a "compile" dependency, then define the scope: 因此,如果您要指定仅测试需要依赖项,而不应将其视为“编译”依赖项,请定义范围:

<dependency>
  <groupId>YOUR_GROUP_ID</groupId>
  <artifactId>A</artifactId>
  <version>YOUR_VERSION</version>
  <scope>test</scope>
</dependency>

If you don't specify any scope, maven will conclude that the dependency is required both for compilation and for tests and maybe even for packaging as I've explained earlier. 如果您不指定任何范围,则maven会得出结论,无论是编译还是测试,甚至包装,都需要依赖,正如我之前解释的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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