当前位置: 首页 > 知识库问答 >
问题:

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

林英锐
2023-03-14

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

共有1个答案

巴博耘
2023-03-14
 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. 创建一个模拟的图书目录CAApp

问:它为什么要这样做?你从来没有告诉过它。您的test对象将返回null,并且不调用任何printLine方法。

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

 类似资料:
  • 我有一堆JUnit测试,它们都单独运行。每一个都是一个真正的独立单元测试--被测试的单个类。不需要上下文。我可以在Eclipse中或通过maven/surefire-plugin单独或一起运行它们。 此后,我添加了一个新的集成测试,它利用了Spring上下文等,并使用了SpringJUnit4ClassRunner。一旦我将这个测试添加到我的套件中,任何测试用例都会在这个类失败后运行。 我不确定这

  • 我目前正在做一个学校的作业,我正在努力与测试部分。出于某种原因,单元测试单独运行时运行良好,但一起运行时就不行了。我知道这与我在他们之间共享对象有关,而我不应该基于我以前的搜索,但我一生都无法找出需要改变什么来解决这个问题。下面是ApplientService类和ApplientServiceTest类的代码。任何帮助都将非常感谢,因为我已经被困在这个问题上一段时间了,现在知道这可能是其他人会立即

  • 这是我的整个测试课程: 有3个单元测试,它们在单独运行时都通过了,但当我运行整个测试类时,我的第2个和第3个测试失败,错误如下: 我已经想尽一切办法来解决这个问题: 我将测试实例化下的类移动到@Before函数中 我尝试创建@After函数并调用Mockito。重置我的模拟 我应该提到的是,我正在使用nhaarman。mockitokotlin2库和argumentCaptor。 关于为什么这些测

  • 不要与之前提出的问题混淆“为什么我的测试在一起运行时失败,但单独通过?” 我有一个任务,我需要修改JUnit测试类来处理多个数据库测试。在实现之前,我需要确保所有测试都在运行,没有失败。令我困惑的是,现在当我一起运行所有的类时,它显示它运行时没有失败。当我运行一个特定的类时,它突然失败了,如果我重复它,结果仍然存在。 这可能是什么原因造成的? 我自己没有写测试,因此我对测试内容的了解是有限的。不过

  • 我为咖啡因CacheLoader实现编写的单元测试(JUnit,Mockito)在单独运行时都成功了,但在一起运行时其中一个失败了。我相信我在所有测试对象设置中使用了的最佳实践。 当与其他人一起运行时,测试testGet_WhenCalledASecondAndThirdTimeBeyondCacheDuration_LoadingMethodCalledASecondTime每次都会失败,并出现

  • 只启动jUnit测试和集成测试。从不执行。 下面是Maven输出的一部分: 我在StackOverflow上发现了几乎相同或相似的问题,但在我的项目中没有一个响应起作用。 null