Java 多模块项目:在不同模块的测试类之间重用静态方法
我有一个多模块java maven项目。我向模块a的junit类添加了一个静态方法。现在我想重用模块B的junit测试中的这个静态方法 模块1:Java 多模块项目:在不同模块的测试类之间重用静态方法,java,junit,static,classpath,Java,Junit,Static,Classpath,我有一个多模块java maven项目。我向模块a的junit类添加了一个静态方法。现在我想重用模块B的junit测试中的这个静态方法 模块1: public class AccountDAOTest { private static Faker faker = new Faker(); public static Account getRandomAccount() { Account account = new Account(); acc
public class AccountDAOTest {
private static Faker faker = new Faker();
public static Account getRandomAccount() {
Account account = new Account();
account.set...(faker.idNumber().valid());
...
return account;
}
@Test
public void getByName() {
Account expected = getRandomAccount();
accountDAO.persist(expected);
assertNotEquals(expected.getId(), null);
Account actual = accountDAO.getByName(expected.getName());
assertNotNull(actual);
}
...
}
public class BusinessBeanTest {
@Test
public void testSomething() {
Account account = AccountDAOTest.getRandomAccount();
...
}
}
模块2(尝试重用静态方法):
public class AccountDAOTest {
private static Faker faker = new Faker();
public static Account getRandomAccount() {
Account account = new Account();
account.set...(faker.idNumber().valid());
...
return account;
}
@Test
public void getByName() {
Account expected = getRandomAccount();
accountDAO.persist(expected);
assertNotEquals(expected.getId(), null);
Account actual = accountDAO.getByName(expected.getName());
assertNotNull(actual);
}
...
}
public class BusinessBeanTest {
@Test
public void testSomething() {
Account account = AccountDAOTest.getRandomAccount();
...
}
}
问题是AccountDAOTest.java不在模块2测试的类路径中,或者我在模块2的pom.xml
中添加了模块依赖项,该模块具有测试范围
我只能看到两种解决方案:
- 复制此方法,并将其从模块1测试类复制到模块2测试类
- 从测试类中删除getRandomAccount()方法,并将其作为实际代码添加到公共模块中
有没有正确的方法来做这件事的想法?只通过测试创建jar,然后将其包含到其他模块中如何 对于模块1:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
org.apache.maven.plugins
maven jar插件
3.1.1
试验罐
模块2:
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<classifier>tests</classifier>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
groupId
人工的
测验
试验罐
版本
测试
如何仅通过测试创建jar,然后将其包含到其他模块中
对于模块1:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
org.apache.maven.plugins
maven jar插件
3.1.1
试验罐
模块2:
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<classifier>tests</classifier>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
groupId
人工的
测验
试验罐
版本
测试