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
jgdvishnuSoftware ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Can you post your code?
jgdvishnuSoftware ArchitectAuthor Commented:
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.
Jim CakalicSenior Developer/ArchitectCommented:
I'm guessing you might be a little confused about mocking. The reason I say that is this code
		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

Open in new window

appears to create a mock of objectServiceImpl. class (oddly named) but then never actually use it. I can't tell but I'd also guess that the mock and the service are both of type objectServiceImpl. In essence, you appear to be trying to test the mock object. In short, you created a mock but you aren't calling any methods on it, The only method invoked in your test is hidePlanArtifacts and that is called on service which I assume is a real instance of objectServiceImpl.

Typically, mocks are created to break dependencies and to isolate the class-under-test from other classes. For example, if your service class had a dependency on a repository class that did database  access, you could mock the repository class, set some expectations on it (what to do when certain methods are called) and then inject the mock on the service object so that when you called service methods that used the repository, it would be using your mock instead of the real thing.

As an aside, I'm not a big fan of the style of testing/mocking that attempts to verify that specific methods were called as a result of a test method invokation. This unnecessarily ties your test to a specific implementation. Future refactoring should be able to change the internal call structure of the implementation without breaking the test but it won't because the test requires specific methods to be called. I prefer a behavioral approach which verifies that a method accomplished what it was supposed to by virtue of what it returns or other residue that it leaves behind.

See Mockists are dead. Long live classicists.

Regards,
Jim

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgdvishnuSoftware ArchitectAuthor Commented:
works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.