我遇到了一個奇怪的問題。我的測試案例有一個失敗的測試,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
!
你爲什麼要嘲笑你的課堂? (BibliotecaApp) –