【问题标题】:AssertionError on totally unrelated line of code - Junit test for school project完全不相关的代码行上的 AssertionError - 学校项目的 Junit 测试
【发布时间】:2014-11-25 06:25:31
【问题描述】:

我正在开发一个 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);

【问题讨论】:

  • 你确定它不指向assertNotNull(member.getName());吗?刷新您的项目,重建并重新运行。如果您编辑它们周围的代码,断点可以保持固定在行上,它们有时不会随之移动。除此之外,请尝试使用assert registry != null 并确保参数也不为空,并使用更多断言或if (blah != null) {..} 块来确保您想要使用的任何内容都不应该为空。
  • 有一个LibraryServerImpl 类是LibraryServer 远程类的实现,MemberImpl 是Member 远程类的一个实现。这两个都有主要的方法,里面什么都没有。这是否会导致问题,因为主要方法(服务器或客户端)在其主要方法中都没有任何内容。尽管如此,我不觉得它应该说 assertionError 当它仍然应该创建一个 LIbraryServer 对象时
  • 这些库对象的构造函数或方法(它们是第 3 方还是您有权修改它们?)可能会抛出 AssertionErrors。您可以调试该部分代码并单步执行所有涉及的方法,以准确确定抛出异常的位置和原因。

标签: java junit rmi assertions


【解决方案1】:

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

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

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

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

但是,在代码的更下方,您确实调用了 assertNotNull(member.getName()),这意味着这必须是报告为第 56 行的内容。

因此,您正在查看的是堆栈跟踪中报告的行号不匹配。按照用户indivisible 的建议,请刷新您的项目,清理您的输出文件夹,重建所有项目,然后重试。

【讨论】:

  • 我打赌他正在使用 Eclipse
  • 是的,它有 Eclipse 的味道。不,它很臭。我真的不知道当存在诸如 IntelliJ IDEA 社区版这样令人敬畏(且同样免费)的替代品时,为什么有人会为它烦恼。 Eclipse 杀死了我内心的孩子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多