2015-12-20 60 views
-1

我想測試一個名爲getProductById的方法的Spring控制器。我有一些我不理解的例外。我將複製堆棧跟蹤。我仍然在學習,我不知道是否會讓事情順利。斷言異常的春天單元測試Java

@Controller 
    @RequestMapping("books") 
    public class BookController { 

     @Autowired 
     BookService bookService; 
    @RequestMapping("/product") 
     public String getProductById(@RequestParam("id") String productId, Model model) { 
      model.addAttribute("allBooks", bookService.getBookById(productId)); 
      return "book"; 
     } 

    } 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/applicationContext.xml" }) 
@WebAppConfiguration 
@RequestMapping("books") 
public class BookControllerTest { 

    @Mock 
    private BookService bookService; 

    @InjectMocks 
    private BookController bookController; 

    private MockMvc mockMvc; 

    @Before 
    public void before() { 
     MockitoAnnotations.initMocks(this); 
     this.mockMvc = MockMvcBuilders.standaloneSetup(bookController).build(); 
    } 
@Test 
    public void testGetProductById() { 
     Book book = new Book("3", "book", "book", 5253); 
     String id = book.getId(); 
     assertNotNull("Object can not have null id ", id); 

     Book searchedBook = bookService.getBookById(id); 
     Assertions.assertThat(searchedBook).isNotNull(); 

     assertTrue("Found book id should be equal to id being searched", searchedBook.getId().compareTo(id) == 3); 
    } 

java.lang.AssertionError: expecting actual value not to be null 
    at org.fest.assertions.Fail.failure(Fail.java:228) 
    at org.fest.assertions.Fail.fail(Fail.java:167) 
    at org.fest.assertions.Fail.failIfActualIsNull(Fail.java:100) 
    at org.fest.assertions.GenericAssert.isNotNull(GenericAssert.java:238) 
    at com.book.controller.test.BookControllerTest.testGetProductById(BookControllerTest.java:61) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    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) 

回答

2
Book searchedBook = bookService.getBookById(id); 
Assertions.assertThat(searchedBook).isNotNull(); 

你聲稱的,你會從你的bookService的一本書,但這種情況並非如此,因爲你的異常顯示:

java.lang.AssertionError: expecting actual value not to be null

顯然,searchedBook爲空。原因很簡單:爲什麼不應該這樣呢?你的bookService是模擬對象,你永遠不會告訴模擬返回任何特定的東西,所以它可能總是返回null

換句話說,你在做什麼是這樣的:

  1. 「嘿,創造了我模仿的對象 - 僞裝成一個bookService的對象」
  2. 「嘿,模仿的對象,給我該書的編號爲X「
  3. 模擬對象:呃?什麼?當你問我這件事時,你從未告訴過我該怎麼做。對不起,我唯一得到的是null。玩得開心。

無論如何,測試模擬的行爲是沒有意義的。通常,你告訴模擬器做一些特定的事情,以便你能夠測試其他的東西。測試假裝成另一個對象的對象......用於什麼?

對於您的情況,您可能需要測試您的bookController,因此您需要模擬bookservice以某種方式運行(因爲真正的書籍服務可能取決於數據庫或類似的東西)。