关于完全不相关的代码行的AssertionError - 学校项目的Junit测试

时间:2014-11-25 06:25:32

标签: java junit rmi assertions

我正在研究Java RMI项目,我正在尝试开始测试我编写的代码,当我运行以下代码时,我在一条甚至没有断言语句的行上得到AssertionError。我很困惑如何解决这个问题。

public void basicTest() throws UnknownHostException, RemoteException, AlreadyBoundException, NotBoundException, InterruptedException {
        int numBooks = 20;
        int copiesPerBook = 5;
        int booksPerMember = 4;

        // Simulate the server
        LibraryServerImpl library = new LibraryServerImpl(numBooks, copiesPerBook, booksPerMember);
        LibraryServer stub = (LibraryServer) java.rmi.server.UnicastRemoteObject.exportObject(library, 0);
        Registry registry = LocateRegistry.createRegistry(port);
        registry.bind(libraryName, stub);

        // Simulate the client
        Member member = new MemberImpl();
        assertNotNull(member.getName()); // Will fail until you implement MemberImpl
        Thread t = new Thread(new BasicClient(member));
        t.start();
        t.join();
    }

这是复制的痕迹:

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at org.junit.Assert.assertNotNull(Assert.java:712)
    at org.junit.Assert.assertNotNull(Assert.java:722)
    at PublicTests.basicTest(PublicTests.java:56)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

第56行对应于registry.bind(libraryName,stub);

1 个答案:

答案 0 :(得分:3)

显然,这没有意义,所以其中一个事实是错误的。

事实:你得到了上面的堆栈跟踪。 - 没有理由怀疑这一事实的真实性。

事实:PublicTests.java是由您编写的。 - 没有理由怀疑这一事实的真实性。

事实:PublicTests.java的第56行对应registry.bind(libraryName, stub)。嗯,这不可能是真的,因为根据堆栈跟踪,通过调用org.junit.Assert.assertNotNull()检测到失败,但行registry.bind(libraryName, stub) 调用{{ 1}}。

但是,在代码中进一步调用org.junit.Assert.assertNotNull(),这意味着必须将其报告为第56行。

因此,您所看到的是堆栈跟踪中报告的行号不匹配。正如用户assertNotNull(member.getName())建议的那样,请刷新项目,清理输出文件夹,重建所有项目,然后重试。