2012-11-18 127 views
0

有人可以告訴我爲什麼我的單元測試不起作用。我對單元測試非常陌生。這是查詢不運行,我不知道爲什麼?單元測試不起作用

public class ContactsServiceTest { 

@Autowired 
private ContactsService contactsService; 

@Test 
public void testGetContactsById() { 
    Contacts con = contactsService.getContactsById(59); 
    assertTrue(con.getFirstName().equals("Jon")); 
} 

}

這是我的錯誤;

java.lang.NullPointerException 
at com.logicalideas.myapp.ContactsServiceTest.testGetContactsById(ContactsServiceTest.java:45) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
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.runChild(BlockJUnit4ClassRunner.java:76) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
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

您的服務或聯繫人爲null。不知道是哪一個,因爲你不告訴我們NPE在哪一行。雖然我不明白這個問題;你不知道什麼是空的? –

+0

感謝您的回覆。這是在這裏執行的查詢(在測試方法中),否則它工作正常。我不明白爲什麼不呢?聯繫人con = contactsService.getContactsById(59); – Zed420

+0

再次 - 你能弄清楚什麼是空?!如果這是你說的這條線,顯然沒有服務。 –

回答

0

顯示java.lang.NullPointerException

私人ContactsService contactsService =新ContactsService();

+0

當使用DI框架時,這並不是特別好的建議,也不適用於簡單的可測試性。 –

1

根據你的錯誤堆棧跟蹤只要45行是

Contacts con = contactsService.getContactsById(59); 

然後ID 59您試圖檢索不持有任何信息或有與連接的問題。

除非您發佈系統的更多信息,否則很難找出導致此問題的確切問題。

+0

仍然不確定爲什麼它不執行查詢? – Zed420

相關問題