简体   繁体   中英

Mockito not resetting

I have a tests, that when they run individually work fine. However when i run them together one always fail expecting total invocations accross both tests rather than one.

I have added Mockito.reset in the before and after method, but to no avail.

    private Logic mockTest = Mockito.mock(Logic.class);         

    @Before
    public void createMocks() {
        Mockito.reset(mockTest);
    }

    @Test
    public void TestGameList() {


        Mockito.when(mockTest.getGame()).thenReturn(null);

            Mockito.verify(mockTest, Mockito.times(1)).getGame();

    }

    @Test
    public void TestGame2List() {


        Mockito.when(mockTest.getGame()).thenReturn(null);

            Mockito.verify(mockTest, Mockito.times(1)).getGame();

    }

Why doesnt reset work?

I have tried VerificationModeFactory to count it, but that doesnt work either

Use one of the following:

@Mock
private Logic mockTest;         

@Before
public void createMocks() {
    MockitoAnnotiation.initMocks(this);
}

or

private Logic mockTest;      

@Before
public void createMocks() {
    mockTest = Mockito.mock(Logic.class); 
}

Either way you will create an entirely new mock for each test thereby ensuring that no state is maintained across tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM