2016-02-06 104 views
1

我遇到了一個奇怪的問題。我的測試案例有一個失敗的測試,welcometest。但是,如果我單獨運行相同的程序,它將完美運行。我是JUnit的新手,不知道爲什麼會發生這種情況。Mockito和Junit測試運行在隔離但一起運行時失敗

package com.twu.biblioteca; 

org.junit.After; 
import org.junit.Before; 
import org.mockito.Mockito; 
import org.mockito.Mockito.*; 
import org.junit.Test; 

import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 

import static org.junit.Assert.assertEquals; 
import static org.mockito.Mockito.*; 


public class ExampleTest { 
    @Test 
    public void welcometest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     test.welcome(asker); 
     verify(asker).printLine("**** Welcome Customer! We are glad to have you at Biblioteca! ****"); 
    } 

    @Test 
    public void addBooksTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     test.addBooks(); 

     assertEquals("Head First Java", test.booksList[1].name); 
     assertEquals("Dheeraj Malhotra", test.booksList[2].author); 
     assertEquals(2009, test.booksList[3].publication); 
    } 

    @Test 
    public void CustomersaddedTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     test.addCustomers(); 

     assertEquals("Ritabrata Moitra", test.customerList[1].name); 
     assertEquals("121-1523", test.customerList[2].libraryNumber); 
     assertEquals("0987654321", test.customerList[3].number); 
    } 

    @Test 
    public void addMoviesTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     test.addMovies(); 

     assertEquals("Taken", test.moviesList[1].name); 
     assertEquals("Steven Spielberg", test.moviesList[2].director); 
     assertEquals(2004, test.moviesList[3].year); 
    } 


    @Test 
    public void getBoundIntegerFromUserTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter your choice. ")).thenReturn(99); 
     when(asker.ask("Select a valid option! ")).thenReturn(1); 

     BibliotecaApp.getBoundIntegerFromUser(asker, "Enter your choice. ", 1, 2); 

     verify(asker).ask("Select a valid option! "); 
    } 

    @Test 
    public void mainMenuTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter your choice. ")).thenReturn(test.numberOfMainMenuOptions); 

     test.mainMenu(asker); 

     verify(test).mainMenuaction(3, asker); 
    } 

    @Test 
    public void checkoutTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter the serial number of the book that you want to checkout")).thenReturn(2); 

     test.addBooks(); 
     test.checkout(asker); 

     assertEquals(0, test.booksList[2].checkoutstatus); 
    } 

    @Test 
    public void returnTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter the serial number of the book that you want to return")).thenReturn(2); 

     test.addBooks(); 
     test.booksList[2].checkoutstatus = 0; 
     test.returnBook(asker); 

     assertEquals(1, test.booksList[2].checkoutstatus); 
    } 

    @Test 
    public void checkoutMovieTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter the serial number of the movie that you want to checkout")).thenReturn(2); 

     test.addMovies(); 
     test.checkoutMovie(asker); 

     assertEquals(0, test.moviesList[2].checkoutstatus); 
    } 

    @Test 
    public void returnMovieTest() { 
     BibliotecaApp test = mock(BibliotecaApp.class); 
     BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
     when(asker.ask("Enter the serial number of the movie that you want to return")).thenReturn(2); 

     test.addMovies(); 
     test.moviesList[2].checkoutstatus = 0; 
     test.returnMovie(asker); 

     assertEquals(1, test.moviesList[2].checkoutstatus); 
    } 

// @Test 
// public void checkoutWithoutLoginTest(){ 
//  BibliotecaApp test = mock(BibliotecaApp.class); 
//  BibliotecaApp.IntegerAsker asker =   mock(BibliotecaApp.IntegerAsker.class); 
//  when(asker.ask("Enter your choice. ")).thenReturn(8); 
//  test.loginStatus = false; 
// 
// 
//  test.mainMenuaction(3,asker); 
// 
//  verify(test,times(0)).checkout(asker); 
// } 
} 

如果我註釋掉最後一個測試(已經註釋掉了),我的所有測試都能成功運行!但是,如果我不評論它,一個測試失敗,但這不是這個測試!這是失敗的welcometest

+2

你爲什麼要嘲笑你的課堂? (BibliotecaApp) –

回答

0
BibliotecaApp test = mock(BibliotecaApp.class); 
BibliotecaApp.IntegerAsker asker = mock(BibliotecaApp.IntegerAsker.class); 
test.welcome(asker); 
verify(asker).printLine("**** Welcome Customer! We are glad to have you at Biblioteca! ****"); 
  1. 創建一個模擬BibliotecaApp
  2. 創建一個模擬BibliotecaApp.IntegerAsker
  3. 運行上與嘲笑嘲笑method歡迎作爲參數
  4. 期待您的MOCK沒有調用另一個方法。

問題:爲什麼要這樣做?你從來沒有告訴過它。您的test對象將返回null,並且不會調用任何printLine方法。

所以,我個人非常懷疑這個測試能否成功運行,無論是孤立的還是其他的。它也沒有任何意義,因爲你正在測試模擬對象的行爲。但我們可以假設Mockito本身已經測試得相當好。你(可能)需要使用真實的BibliotecaApp而不是模擬。

+0

使用真正的Biblioteca應用程序爲我做了訣竅!謝謝你! – Ritabrata

相關問題