Link to home
Start Free TrialLog in
Avatar of Steve Sperber
Steve SperberFlag for United States of America

asked on

junit test for void methods using mockito

i have mocked required object for testing void method.
i am using

verify() method and times to check how many times  a method is called for a void method.
i have called 2 levels to test that number of times called gives some positive value but i am unable to get anything other then zero times called.

is there a solution for this
Avatar of mccarl
mccarl
Flag of Australia image

Can you post your code?
Avatar of Steve Sperber

ASKER

Below is the code.
@Test
	public void testHidePlanArtifacts() {
		List<Long> objectKeys = new ArrayList<Long>();
		objectKeys.add(new Long(1L));
		objectServiceImpl objectServiceImplMockObj = Mockito.mock(objectServiceImpl.class);
		service.hidePlanArtifacts(objectKeys);
		verify(objectServiceImplMockObj, times(0)).hidePlanArtifacts(Mockito.anyListOf(Long.class));//not sure why it is only 0 time not 1 time
		verify(objectServiceImplMockObj, times(0)).hideArtifacts(Mockito.anyLong());//not sure why it is only 0 time not 1 time
	}

Open in new window


My guess is objectServiceImplMockObj is not being passed as parameter that is why it gives zero times invoked.
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works