expectation PowerMockito vs Mockito

1,121 views
Skip to first unread message

mantat

unread,
Jan 11, 2011, 6:24:11 PM1/11/11
to PowerMock
Hello,

I am writing all the examples in the tutorial in the
demo.org.powermock.examples.tutorial package in mockito format (all of
them are toward using easymock and I perfer mockito).

While doing that, I am seeing myself using 2 difference expectation
setup.
------------
// use PowerMockito to set up your expectation
PowerMockito.doReturn(value).when(classUnderTest, "methodToMock",
"parameter1");

// use Mockito to set up your expectation
Mockito.when(classUnderTest.methodToMock()).thenReturn(value);
------------


- What are the difference between them and when should them be use?
- Example that I am writing right now.

package
demo.org.powermock.examples.tutorial.partialmocking.service.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.powermock.reflect.Whitebox.invokeMethod;
import static org.powermock.reflect.Whitebox.setInternalState;

import java.util.HashSet;
import java.util.Set;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import
demo.org.powermock.examples.tutorial.partialmocking.dao.ProviderDao;
import
demo.org.powermock.examples.tutorial.partialmocking.dao.domain.impl.ServiceArtifact;
import
demo.org.powermock.examples.tutorial.partialmocking.domain.ServiceProducer;


@RunWith(PowerMockRunner.class)
@PrepareForTest(ProviderServiceImpl.class)
public class ProviderServiceImplMockitoTest {

private ProviderServiceImpl providerserviceImpl;
private ProviderDao providerDaoMock;

@Before
public void setUp() {
providerserviceImpl = new ProviderServiceImpl();
providerDaoMock = mock(ProviderDao.class);
setInternalState(providerserviceImpl, providerDaoMock);
}

@After
public void tearDown() {
providerserviceImpl = null;
providerDaoMock = null;
}

@Test
public void testGetAllServiceProviders() throws Exception {
final String methodNameToMock = "getAllServiceProducers";
final Set<ServiceProducer> expectedServiceProducers = new
HashSet<ServiceProducer>();
expectedServiceProducers.add(new ServiceProducer(1, "mock name"));
ProviderServiceImpl providerserviceImpl = PowerMockito.spy(new
ProviderServiceImpl());

PowerMockito.doReturn(expectedServiceProducers).when(providerserviceImpl,
methodNameToMock);
Set<ServiceProducer> actualServiceProviders =
providerserviceImpl.getAllServiceProviders();
assertSame(expectedServiceProducers, actualServiceProviders);
}

@Test
public void testGetAllServiceProviders_noServiceProvidersFound()
throws Exception {
final String methodNameToMock = "getAllServiceProducers";
final Set<ServiceProducer> expectedServiceProducers = new
HashSet<ServiceProducer>();
ProviderServiceImpl providerserviceImpl = PowerMockito.spy(new
ProviderServiceImpl());
PowerMockito.doReturn(null).when(providerserviceImpl,
methodNameToMock);
Set<ServiceProducer> actualServiceProviders =
providerserviceImpl.getAllServiceProviders();
assertNotSame(expectedServiceProducers, actualServiceProviders);
assertEquals(expectedServiceProducers, actualServiceProviders);
}

@Test
public void testServiceProvider_found() throws Exception {
final String methodNameToMock = "getAllServiceProducers";
final int expectedServiceProducerId = 1;
final ServiceProducer expected = new
ServiceProducer(expectedServiceProducerId, "mock name");
final Set<ServiceProducer> serviceProducers = new
HashSet<ServiceProducer>();
serviceProducers.add(expected);
ProviderServiceImpl providerserviceImpl = PowerMockito.spy(new
ProviderServiceImpl());

PowerMockito.doReturn(serviceProducers).when(providerserviceImpl,
methodNameToMock);
ServiceProducer actual =
providerserviceImpl.getServiceProvider(expectedServiceProducerId);
assertSame(expected, actual);
}

@Test
public void testServiceProvider_notFound() throws Exception {
final String methodNameToMock = "getAllServiceProducers";
final int expectedServiceProducerId = 1;
ProviderServiceImpl providerserviceImpl = PowerMockito.spy(new
ProviderServiceImpl());
PowerMockito.doReturn(new
HashSet<ServiceProducer>()).when(providerserviceImpl,
methodNameToMock);

assertNull(providerserviceImpl.getServiceProvider(expectedServiceProducerId));
}

@Test
@SuppressWarnings("unchecked")
public void testGetAllServiceProducers() throws Exception {

final String expectedName = "mock name";
final int expectedId = 1;
final Set<ServiceArtifact> serviceArtifacts = new
HashSet<ServiceArtifact>();
serviceArtifacts.add(new ServiceArtifact(expectedId, expectedName));

Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(serviceArtifacts);
Set<ServiceProducer> serviceProducers = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertEquals(1, serviceProducers.size());
assertTrue(serviceProducers.contains(new ServiceProducer(expectedId,
expectedName)));

}

@Test
@SuppressWarnings("unchecked")
public void getAllServiceProducers_empty() throws Exception {


Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(new
HashSet<ServiceArtifact>());
Set<ServiceProducer> actual = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertTrue(actual.isEmpty());
}
}






Johan Haleby

unread,
Jan 12, 2011, 2:08:44 AM1/12/11
to powe...@googlegroups.com
Hi, 

You can use Mockito's when in combination with PowerMock (however I don't think you should explicitly write Mockito.when(..) or PowerMockito.when(..), use static imports instead). The difference between the two is that when using "when" you call the original method if you're using "spy" whereas usng doReturn never calls the real method. So for example if you need partial mocking and cannot allow the method to be called then you should use doReturn. 

Do you have any problem with using the "when" method in PowerMockito? If I remember it correctly it should simply delegate to Mockito.when so there should be no difference.

It would also be really cool if you could share the tutorial with us when you're done.

Regards,
/Johan







--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


mantat

unread,
Jan 12, 2011, 2:47:40 PM1/12/11
to PowerMock
Johan,

Thanks. I was trying to use just PowerMock.when(). And when I am
testing an interface class, I cannot create the spy object. And the
PowerMock.when() won't take a mock object.
Please look at the testGetAllServiceProducers().
// PowerMockito.when have problem taking the mockito mock object -
only take spy real object
// PowerMockito.doReturn(serviceArtifacts).when(providerDaoMock,
"getAllServiceProducers");

Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(serviceArtifacts);
Set<ServiceProducer> serviceProducers = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertEquals(1, serviceProducers.size());
assertTrue(serviceProducers.contains(new ServiceProducer(expectedId,
expectedName)));

}

@Test
@SuppressWarnings("unchecked")
public void getAllServiceProducers_empty() throws Exception {


Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(new
HashSet<ServiceArtifact>());
Set<ServiceProducer> actual = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertTrue(actual.isEmpty());
}
}







I wrote SampleServiceImplMockitoTest, HelloWorldMockitoTest,
ProviderServiceImplMockitoTest, ServiceRegistratorMockitoTest in
mockito style. You can update that anyway you want and put that in
your repository. I can send you in email, did not see the attachment
option here.

thanks
mantat











On Jan 11, 11:08 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> You can use Mockito's when in combination with PowerMock (however I don't
> think you should explicitly write Mockito.when(..) or PowerMockito.when(..),
> use static imports instead). The difference between the two is that when
> using "when" you call the original method if you're using "spy" whereas usng
> doReturn never calls the real method. So for example if you need partial
> mocking and cannot allow the method to be called then you should use
> doReturn.
>
> Do you have any problem with using the "when" method in PowerMockito? If I
> remember it correctly it should simply delegate to Mockito.when so there
> should be no difference.
>
> It would also be really cool if you could share the tutorial with us when
> you're done.
>
> Regards,
> /Johan
>
> > powermock+...@googlegroups.com<powermock%2Bunsu...@googlegroups.com>
> > .

Johan Haleby

unread,
Jan 13, 2011, 2:01:30 AM1/13/11
to powe...@googlegroups.com
Hi, 

Not sure if I understand you correctly but you cannot spy on interfaces, only on concrete classes. Perhaps it will be more clear when I see the entire code. So if you don't mind please attach the files to me in an e-mail.

Regards,
/Johan

To unsubscribe from this group, send email to powermock+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages