我正在尝试在我创建的特定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 测试
User类位于src&gt; com&gt; jr?freedom&gt; user(代表com.jr.freedom.user.User) 并且测试类在测试&gt;测试&gt; com&gt; jr&gt;自由&gt;用户(代表test.com.jr.freedom.user.UserServiceTest)
起初/ test director中的包名与User对象的/ src目录中的包标签相同但是没有用,因为我认为java relfection试图检索/ test文件夹中的User对象所以我重命名了这个包,它是独一无二的,但仍然没有用。任何建议?感谢
完整的错误详情:
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
)
答案 0 :(得分:3)
User.class。toString()的结果是:
class com.jr.freedom.user.User
在真正的类名开始之前,请参阅类?这使得该字符串成为非法的类名。
您需要使用getName()。
答案 1 :(得分:2)
toString
类的Class
返回:"class com.foo.NameOfClass"
因此它正在尝试加载:class com.jr.freedom.user.User
(正如您的错误消息所述)
但更重要的是,你已经拥有了这个类:User.class,为什么要再次加载?只需拿出那条线就可以了:
User.class.getDeclaredMethods...