我有两个Java项目,"A"和"B",B对A有Maven依赖:
<dependency>
<!-- Back end stuff -->
<groupId>com.myapp</groupId>
<artifactId>ProjectA</artifactId>
<version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这两个项目在我的工作站上并排放置在一个公共父文件夹中:
/Myproject
/ProjectA
/ProjectB
Run Code Online (Sandbox Code Playgroud)
我想在项目B中对项目A的所有单元测试使用项目A的单元测试上下文"test-context.xml".有没有办法直接引用外部上下文进行测试?这些是使用Surefire和Junit进行测试的Maven项目,但我担心Surefire和Junit不是我的优势.我很确定有办法做到这一点,但我不知道在哪里寻找答案 - Spring,Junit,Maven,Surefire ......?
我的项目"A"单元测试类配置如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml"})
Run Code Online (Sandbox Code Playgroud)
文件"test-context.xml"位于/src/test/resources/test-context.xml中的项目A. 理想情况下,我只需配置我的Project"B"单元测试类,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"ProjectA-reference:test-context.xml"})
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何配置ContextConfiguration元素以指向其他项目.有没有人这样做过?
在ProjectA的pom中,执行此操作以生成测试jar依赖项:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
然后,在ProjectB的pom.xml中,执行以下操作:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ProjectA</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
最后,在ProjectB的测试类中,您应该能够使用上面尝试的类路径方法从ProjectA中的src/test/resources引用任何xml文件.假设您的文件被调用projectA-test-context.xml并驻留在/ src/test/resources/META-INF/spring中.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/spring/projectA-test-context.xml")
Run Code Online (Sandbox Code Playgroud)
编辑编辑我的答案,将/ src/main/resources更正为/ src/test/resources.
| 归档时间: |
|
| 查看次数: |
2021 次 |
| 最近记录: |