Mockito和Junit测试是单独运行的,但在一起运行时失败

Mockito和Junit测试是单独运行的,但在一起运行时失败,junit,mockito,Junit,Mockito,我一直遇到一个奇怪的问题。我的测试用例有一个失败的测试,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

我一直遇到一个奇怪的问题。我的测试用例有一个失败的测试,
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
失败了

 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! ****");
  • 创建一个模拟的图书馆
  • 创建一个模拟的BibliotecaApp.IntegerAsker
  • 使用MOCK作为参数,在MOCK上运行
    方法
    welcome
  • 假设您的模拟调用了另一个方法 问:为什么要这样做?你从来没有告诉过它。您的
    test
    对象将返回
    null
    ,并且不会调用任何
    printLine
    方法

    所以,就我个人而言,我非常怀疑这个测试是否会成功运行,不管是单独运行还是以其他方式运行。它也没有任何意义,因为您正在测试模拟对象的行为。但我们可以假设Mockito本身已经得到了相当好的测试。您(可能)需要使用真正的
    BibliotecaApp
    而不是模拟

  • 创建一个模拟的图书馆
  • 创建一个模拟的BibliotecaApp.IntegerAsker
  • 使用MOCK作为参数,在MOCK上运行
    方法
    welcome
  • 假设您的模拟调用了另一个方法 问:为什么要这样做?你从来没有告诉过它。您的
    test
    对象将返回
    null
    ,并且不会调用任何
    printLine
    方法


    所以,就我个人而言,我非常怀疑这个测试是否会成功运行,不管是单独运行还是以其他方式运行。它也没有任何意义,因为您正在测试模拟对象的行为。但我们可以假设Mockito本身已经得到了相当好的测试。您(可能)需要使用真正的
    BibliotecaApp
    而不是模拟。

    为什么要模拟您的测试类?(卡普)你为什么要嘲笑你的受测班级?(BibliotecaApp)使用一个真正的Biblioteca应用程序为我做到了!谢谢你!使用一个真正的Biblioteca应用程序帮了我一把!谢谢你!