From: Daniel B. <dan...@gm...> - 2006-11-22 21:36:51
|
Hi Mick! How did it go? Cheers Daniel On 11/16/06, Daniel Brolund <dan...@gm...> wrote: > Hi again! > We strongly recommend you to split those iterations i separate tests > (test methods) instead. In your case maybe: > > testGetOneStateCode() {...} > testGetTwoStateCodes() {...} > ... > testGetManyStateCodes() {...} > ... > testWhenGetStateCodesReturnsNullMyClassDoesntGoNuts() {...} > > etc for any border condition of your class. > > The reasons to split the tests in separate methods are many, and from > the top of my mind I can come up with the following: > * When a test fails you want to know what went wrong as fast as possible. > * You want to communicate your intent with the test as clearly as possible > * You want your test code to be simple to read and understand, i.e. > short and concise. > > The same principle holds for when you want to test for when the mocked > object throws an exception: > > testWhenGetStateCodesThrowsExceptionTheExceptionIsHandled() {...} > > To make RMock throw exception, do like this example: > > public void testExceptionThrowing() > { > // Create a mocked object > Runnable runnable = (Runnable) mock(Runnable.class, "runnable"); > > // Set it up to throw an exception when called > RuntimeException runtimeException = new RuntimeException( "My > faked exception" ); > runnable.run(); > modify().throwException( runtimeException ); > > startVerification(); > > // Setup this test to expect an exception from the execution > expectThatExceptionThrown( is.same(runtimeException) ); > // If your class catches the exception and rethrows another > // exception, alter the above to reflect that using an appropriate > // "is" expression. > > // Call the mock that throws an exception (this mock should really be > // passed to the class under test in a more realistic situation) > runnable.run(); > } > > > Did that make any sense? > > Cheers > Daniel > > On 11/16/06, Mick Knutson <mic...@gm...> wrote: > > I have the following test: > > > > public void testGetStateCodes() throws Exception { > > // JUnitDoclet begin method getStateCodes > > > > // create mock service object > > ProviderDAOServiceImpl providerDAOServiceImpl = > > (ProviderDAOServiceImpl) mock( ProviderDAOServiceImpl.class, > > "providerDAOServiceImpl" ); > > > > ArrayList list = new ArrayList(); > > list.add("CA"); > > > > // Iteration 1 > > ------------------------------------------------------// > > // Also need to check for a null, and an exception returned... > > // setup the call that the providerbackingbean will be using > > providerDAOServiceImpl.getStateCodes(); > > // Modify argument expectation > > modify().returnValue(list); > > // Change state to verification, IMPORTANT!!! > > startVerification(); > > providerserviceimpl.setDao(providerDAOServiceImpl); > > // Check that the class returns the value from the mocked interface > > (defaults to null) > > assertThat( > > providerserviceimpl.getStateCodes().size(), is.eq(1) ); > > > > // Iteration 2 > > ------------------------------------------------------// > > list.add("AK"); > > /*//// setup the call that the providerbackingbean will be using > > //providerDAOServiceImpl.getStateCodes(); > > // Modify argument expectation > > modify().returnValue(list); > > // Change state to verification, IMPORTANT!!! > > startVerification(); > > providerserviceimpl.setDao(providerDAOServiceImpl); > > // Check that the class returns the value from the mocked interface > > (defaults to null) > > assertThat( > > providerserviceimpl.getStateCodes().size(), is.eq(2) );*/ > > > > // JUnitDoclet end method getStateCodes > > } > > > > > > > > I am trying to find the best practice for altering the return values during > > a test for multiple iterations like this. > > > > I also need to understand how to have my rMock return/throw an Exception > > > > > > > > -- > > > > Thanks > > > > DJ MICK > > http://www.djmick.com > > http://www.myspace.com/mickknutson > > ------------------------------------------------------------------------- > > Take Surveys. Earn Cash. Influence the Future of IT > > Join SourceForge.net's Techsay panel and you'll get the chance to share your > > opinions on IT & business topics through brief surveys - and earn cash > > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > > > > _______________________________________________ > > Rmock-users mailing list > > Rmo...@li... > > https://lists.sourceforge.net/lists/listinfo/rmock-users > > > > > > > > > -- > __________________________ > Dan...@Gm... > -- __________________________ Dan...@Gm... |