Mockito and Junit tests run in isolation but fail when run together -
i have been encountering strange issue. test cases have 1 failing test, welcometest. however, if run same in isolation, runs perfectly. new junit , have no idea why happen.
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! glad have @ 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 choice. ")).thenreturn(99); when(asker.ask("select valid option! ")).thenreturn(1); bibliotecaapp.getboundintegerfromuser(asker, "enter choice. ", 1, 2); verify(asker).ask("select valid option! "); } @test public void mainmenutest() { bibliotecaapp test = mock(bibliotecaapp.class); bibliotecaapp.integerasker asker = mock(bibliotecaapp.integerasker.class); when(asker.ask("enter 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 serial number of book want 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 serial number of book want 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 serial number of movie want 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 serial number of movie want 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 choice. ")).thenreturn(8); // test.loginstatus = false; // // // test.mainmenuaction(3,asker); // // verify(test,times(0)).checkout(asker); // } } if comment out last test (that commented out) tests run successfully! if not comment out, 1 test fails not test! welcometest fails!
bibliotecaapp test = mock(bibliotecaapp.class); bibliotecaapp.integerasker asker = mock(bibliotecaapp.integerasker.class); test.welcome(asker); verify(asker).printline("**** welcome customer! glad have @ biblioteca! ****"); - create mock bibliotecaapp
- create mock bibliotecaapp.integerasker
- run
methodwelcome on mock mock parameter - expect mock did call method.
question: why should that? never told to. test object return null , not call printline method.
so, personally, highly doubt test ever run successfully, in isolation or otherwise. doesn't make sense, since testing behavior of mock object. can assume mockito tested well. (probably) need use real bibliotecaapp instead of mock.
Comments
Post a Comment