[英]want to execute a java test class which is in src/test/java from src/main/java but I am unable to import that java class in src/main/java?
In my requirement I need to execute the Junit test class in src/test/java
from a java class in src/main/java
, but I am unable to import the java class present in src/test/java
from src/main/java
. In my requirement I need to execute the Junit test class in src/test/java
from a java class in src/main/java
, but I am unable to import the java class present in src/test/java
from src/main/java
.
Ideally Maven structure also will look forward that main code will be src/main/java
and test code will be in src/test/java
and src/main/java
should not have dependency on src/test/java
.理想情况下 Maven 结构也将期待主代码将是src/main/java
和测试代码将在src/test/java
和src/main/java
不应该依赖于src/test/java
。
but in my case I want to execute Junit class from main program in src/main/java
but I am unable to import that class from src/test/java
.但就我而言,我想从src/main/java
的主程序执行 Junit class 但我无法从src/test/java
导入该 class 。
Please provide me any suggestions on how do I can achieve this or is there any way to mention in POM file so that maven allows me to import the class from src/test/java
in src/main/java
.请向我提供有关如何实现此目标的任何建议,或者是否有任何方法可以在 POM 文件中提及,以便 maven 允许我从src/test/java
中的src/main/java
导入 class 。
This is not an allowed operation.这是不允许的操作。 Maven allows you in your Test code to reference all the code in src/main/java
but not vice versa. Maven 允许您在测试代码中引用src/main/java
中的所有代码,反之亦然。
It is semantically incorrect in your production code to rely on something that is available only when testing.在生产代码中依赖仅在测试时可用的东西在语义上是不正确的。 In production imagine your test code does not even exist (anything under src/test/java
would not be available, but src/main/java
should still work).在生产中想象你的测试代码甚至不存在( src/test/java
下的任何东西都不可用,但src/main/java
应该仍然有效)。
If you do need the functionality in your main code, it is advisable to refactor the code somewhere under src/main/java
and then use it both in the class where you originally needed it, as well as in your test code.如果您确实需要主代码中的功能,建议在src/main/java
下的某处重构代码,然后在您最初需要它的 class 以及测试代码中使用它。
For instance:例如:
// src/main/java
class Production {
void foo() {
someTestClass.bar();
}
}
// src/test/java
class SomeTestClass {
void testSomething() {
bar();
}
void bar() {
}
}
The above code would not compile, but you could refactor it like so:上面的代码不会编译,但你可以像这样重构它:
// src/main/java
class Production {
void foo() {
utility.bar();
}
}
class Utility {
void bar() {
}
}
// src/test/java
class SomeTestClass {
void testSomething() {
utility.bar();
}
}
Because the dependency is unidirectional, the code now is available to both src/main/java
and src/test/java
.因为依赖是单向的,所以代码现在对src/main/java
和src/test/java
都可用。
If you want one Maven project's test code to depend on another Maven project's test code, then you should declare the dependecy with scope test
like this:如果您希望一个 Maven 项目的测试代码依赖于另一个 Maven 项目的测试代码,那么您应该使用 scope test
声明依赖关系,如下所示:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>cool-testing-dependency</artifactId>
<scope>test</scope>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.