简体   繁体   English

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?

[英]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/javasrc/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/javasrc/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.

相关问题 JAVA - 无法将 src/test/java 导入 src/main/java - JAVA - can't import src/test/java to src/main/java 无法在 eclipse 中将 class src/test/java 导入 src/main/java - can't import class src/test/java into src/main/java in eclipse 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java 春季引导。 该类仅在src / java / main中运行,在src / java / test中具有异常 - Spring boot. Class run only in src/java/main, in src/java/test it has exceptions Maven项目中src / main / java和src / test / java之间的区别 - Difference between src/main/java and src/test/java in a Maven project IntelliJ IDEA:库在src \\ test \\ java上可用,但在src \\ main \\ java上不可用 - IntelliJ IDEA: library available on src\test\java but not on src\main\java eclipse中无法将src/main/java下的java类导入到src/test/java中 - Cannot import java classes under src/main/java into src/test/java in eclipse 如何从 Java 的主类访问 src.test 包或如何从主类运行测试类? - How to make acces to src.test package from main class in Java or how to run test class from main class? 我们可以将 /src/main/scala 中的 class 导入 /src/main/java - Can we import a class present in /src/main/scala to /src/main/java Java - 使用 java 1.4 编译 src/main/java 并使用 1.5 编译 src/test/java - Java - Make src/main/java compile with java 1.4 and src/test/java compile with 1.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM