2016-02-06 75 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

相关问题