简体   繁体   English

是否可以从 src/main 中的 src/test 加载测试类?

[英]Is it possible to load test classes from src/test in src/main?

Im new in Java and JUnit technology and I cant understand why I cant create instance for class from src/test in src/main classes?我是 Java 和 JUnit 技术的新手,我不明白为什么我不能从 src/main 类的 src/test 中为 class 创建实例?

For example:例如:

My JUnit runner is in src/main in second module(I have dependecy for first module).我的 JUnit 跑步者在第二个模块的 src/main 中(我对第一个模块有依赖)。 JUnit runner for filtering tests required MethodSelector object but in constructor i must put Class javaClass, String methodName parameters. JUnit 运行器用于过滤测试所需的 MethodSelector object 但在构造函数中我必须放入 Class javaClass、String methodName 参数。 When I tried create instance for my test class in src/main from src/test I cant import this class...当我尝试从 src/test 在 src/main 中为我的测试 class 创建实例时,我无法导入此 class ...

I understand its not best practice when in src/main we create test instances but my runner must be console app so if I understand well it should be in src/main?我知道在 src/main 中创建测试实例时它不是最佳实践,但我的运行器必须是控制台应用程序,所以如果我理解得很好,它应该在 src/main 中? Over the past two weeks I have not seen another way to filter tests without class instance and method names parameters.在过去的两周里,我没有看到没有 class 实例和方法名称参数的另一种过滤测试的方法。

I try to do it like this我试着这样做

def class = Class.forName("package.from.src.test")

but I always get java.lang.ClassNotFoundException error.但我总是收到 java.lang.ClassNotFoundException 错误。

Can anyone help?任何人都可以帮忙吗? Thanks谢谢

I am posting this very late but hoping it would help someone.我很晚才发布这个,但希望它会对某人有所帮助。

I was able to access a class from src/test into src/main using following gradle entries.我能够使用以下 gradle 条目从 src/test 访问 class 到 src/main 。 I am not sure how to do it with non-gradle project.我不确定如何使用非 gradle 项目来做到这一点。

sourceSets {
    main {
        java {
            srcDirs 'src/main/java'
            srcDirs 'src/test/java'
        }
    }
}

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

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