无法从主包(Eclipse)访问测试包中的类

问题描述:

我在Eclipse中有一个Maven项目,其文件夹结构如下-

I have a maven project in eclipse with a folder structure are follows -

   - Project

    > src/main/java ..

    > src/test/java ..

我发现当我尝试初始化测试包中的任何类时从
主程序包中的代码中,我得到classNotFoundException。

I found that when I try to initialize any class in the test package from my code in the main package I get a classNotFoundException.

我想知道为什么会这样,以及不允许访问测试类的原因是什么?从主包中获取?

I want to know why this happens and what is the reason for not allowing access to test classes from the main package ?

如果您的主代码需要测试文件夹中的代码,则测试文件夹中的代码不是一个测试,它是主代码。测试的重点是它独立于您的主代码,而不是不可分割的部分。

If your main code needs code in the test folder, then the code in the test folder is not a "test", it is main code. The point of test is that it is independent from your main code and not an integral part.

否则,如果您确实要执行此操作,请查看运行配置。用于启动程序并设置类路径以包含测试内容。

Otherwise, if you really want to do it, look at the Run Configuration you are using to launch your program and set the classpath to include the test stuff.