swa*_*ner 94 java eclipse maven-2 exception
当我试图从Eclipse开始我的JUnit-Test时,我得到一个"ClassNotFoundException".从控制台运行"mvn test"时 - 一切正常.此外,Eclipse中没有报告任何问题.
我的项目结构如下:
编辑:如何找不到课程?这是一个简单的HelloWorld-Application,没有特殊的库.
这是我的JUnit的运行配置: alt text http://www.walkner.biz/_temp/runconfig.png
Testclass(但正如我所说;它不适用于简单的HelloWorld ......):
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import biz.prognoserechnung.domain.User;
import biz.prognoserechnung.domain.UserRepository;
import biz.prognoserechnung.domain.hibernate.UserHibernateDao;
public class UserDaoTest {
/**
* the applicationcontext.
*/
private ApplicationContext ctx = null;
/**
* the user itself.
*/
private User record = null;
/**
* Interface for the user.
*/
private UserRepository dao = null;
@Before
public void setUp() throws Exception {
String[] paths = { "WEB-INF/applicationContext.xml" };
ctx = new ClassPathXmlApplicationContext(paths);
dao = (UserHibernateDao) ctx.getBean("userRepository");
}
@After
public void tearDown() throws Exception {
dao = null;
}
@Test
public final void testIsUser() throws Exception {
Assert.assertTrue(dao.isUser("John", "Doe"));
}
@Test
public final void testIsNoUser() throws Exception {
Assert.assertFalse(dao.isUser("not", "existing"));
Assert.assertFalse(dao.isUser(null, null));
Assert.assertFalse(dao.isUser("", ""));
}
}
Run Code Online (Sandbox Code Playgroud)
Car*_*los 193
我已经多次遇到过这种情况,经过多次尝试,我找到了解决方案.
检查项目构建路径并为每个文件夹启用特定输出文件夹.逐个浏览项目的每个源文件夹,并设置maven将使用的输出文件夹.
例如,您的Web项目src/main/java应该target/classes在Web项目下,测试类target/test-classes也应该在Web项目下也是如此.
使用此配置将允许您在eclipse中执行单元测试.
还有一个建议,如果您的Web项目的测试需要资源下的一些配置文件,请确保将该文件夹包含为源文件夹并进行正确的构建路径配置.
希望能帮助到你.
Cof*_*nky 46
卡洛斯的方法有帮助! Eclipse - java.lang.ClassNotFoundException
尝试检查junit运行配置的类路径:
适合我.
akf*_*akf 16
你的构建类路径是正确的,这就是你可以编译的原因.需要检查JUnit的类路径.转到"运行"菜单,然后选择"打开运行对话框".在那里你应该看到左边的一棵树,JUnit作为一个选项.打开该节点,找到并选择您的测试.在右侧窗格中,您将看到classpath的选项卡.看一下,确保找到测试试图实例化的类.
编辑:
这似乎是maven的一个问题,它在发布后的行为改变了默认的Eclipse输出文件夹.我见过解决方案描述的地方
mvn clean test诀窍或与前三个一样,有报道称这个问题反复发生.最后一个看起来对我来说最好,但如果它不起作用,请尝试其他人.
启用[x] 使用临时 JAR在运行配置的类路径选项卡内指定类路径(以避免类路径长度限制)对我来说很有效。
如果您的项目很大,并且您有很多来自其他兄弟项目和 maven 依赖项的依赖项,您可能会遇到类路径长度限制,这似乎是唯一的解决方案(除了缩短本地 maven repo 的目录(我们已经开始了)在 c:/m2)
| 归档时间: |
|
| 查看次数: |
303243 次 |
| 最近记录: |