我正在尝试 gt 我创建的特定 java 类对象的方法列表并尝试对其进行单元测试。
它失败说它找不到具有“java.lang.ClassNotFoundException:class com.jr.freedom.user.User”的类
这是测试代码:
@Test
public void testRegisterNewUser() throws InvalidDataException {
userService = new UserService();
try {
Class classUser = Class.forName(User.class.toString());
Method m[] =classUser.getDeclaredMethods();
for(int i = 0; i < m.length; i++){
System.out.println(m[i].toString());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
fail(e.getMessage());
}
assertNotNull(createTestUserJson());
}
我的项目包如下所示:
在项目文件夹内:src test
User 类在 src>com>jr?freedom>user 中(代表 com.jr.freedom.user.User),而测试类在 test>test>com>jr>freedom>user 中(代表 test.com.jr .freedom.user.UserServiceTest)
起初,/test 目录中的包名称与用户对象的 /src 目录中的包标签相同,但这并没有起作用,因为我认为 java relfection 试图检索 /test 文件夹中的用户对象,所以我重命名了包,使其独一无二,但仍然无法正常工作。有什么建议吗?谢谢
完整的错误详细信息:
java.lang.ClassNotFoundException: class com.jr.freedom.user.User
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at test.com.jr.freedom.user.UserServiceTest.testRegisterNewUser(UserServiceTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197
)