我正在開發一個Java RMI項目,並且試圖開始測試我編寫的代碼,並且在運行以下代碼時,我得到一個AssertionError在一條甚至沒有斷言聲明的線上。我很困惑如何解決這個問題。完全不相關的代碼行上的AssertionError - 學校項目的Junit測試
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(庫名稱,存根);
你確定它沒有指向'assertNotNull(member.getName());'?刷新您的項目,重建並重新運行。如果你編輯它們周圍的代碼,它們有時不隨它移動,斷點可以保持固定。除此之外,嘗試將'assert registry!= null'放在一起,並確保參數也不爲null,其中包含更多斷言或'if(blah!= null){..}'塊以確保您要使用的任何內容爲null它不應該。 – indivisible 2014-11-25 08:13:55
有一個LibraryServerImpl類,它是LibraryServer遠程類的實現,MemberImpl是成員遠程類的實現。這兩種方法都沒有任何主要方法。這可能會導致問題,因爲主要方法(服務器或客戶端)都沒有任何主要方法。儘管如此,我不覺得它應該說assertionError當它仍然應該創建一個LIbraryServer對象 – 2014-11-25 08:16:46
這些庫對象的構造函數或方法(他們是第三方還是您有權修改它們?)可能會拋出AssertionErrors。您可以調試該代碼部分,並進入所有涉及的方法,以精確確定拋出異常的位置和原因。 – indivisible 2014-11-25 11:00:57