我遇到了一个奇怪的问题。我的测试用例有一个失败的测试,welcometest
。但是,如果我单独运行它,它运行完美。我是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
失败了!
答案 0 :(得分: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! ****");
method
欢迎问题:为什么要这样做?你从来没有告诉过它。您的test
对象将返回null
,而不会调用任何printLine
方法。
所以,就个人而言,我非常怀疑这项测试是否会成功运行,无论是孤立还是其他方式。它也没有任何意义,因为您正在测试模拟对象的行为。但我们可以假设Mockito本身已经测试得相当好。您(可能)需要使用真实的BibliotecaApp
而不是模拟。