Skip to content

Commit

Permalink
Code created by the November 2nd Coding Dojo session
Browse files Browse the repository at this point in the history
  • Loading branch information
GenevaCodingDojo committed Nov 2, 2011
1 parent 4947b52 commit 45409cf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
Expand Up @@ -27,10 +27,7 @@

/**
* A simple implementation of the providers service. This is the class that's
* going to be tested using PowerMock. The main reason for the test is to
* demonstrate how to use PowerMock to set internal state (i.e. setting the
* <code>providerDao</code> field without setters), partial mocking and
* expectations of private methods.
* going to be tested using PowerMock.
*/
public class ProviderServiceImpl implements ProviderService {

Expand Down
Expand Up @@ -57,7 +57,7 @@ public long registerService(String name, Object serviceImplementation) {
return id;
}

/**
/** Mock for ServiceRegistratio
* {@inheritDoc}
*/
public void unregisterService(long id) {
Expand Down
Expand Up @@ -86,6 +86,5 @@ public void shouldThrowSampleServiceExceptionWhenPersonInstantiationFailed() thr
verifyZeroInteractions(personService);
verifyZeroInteractions(eventService);
}

}
}
@@ -0,0 +1,89 @@
package ch.genevajug.crappy.serviceregistrator;

import ch.genevajug.crappy.hello.SimpleConfig;
import ch.genevajug.crappy.serviceregistor.impl.IdGenerator;
import ch.genevajug.crappy.serviceregistor.impl.ServiceRegistrator;
import ch.genevajug.crappy.serviceregistor.osgi.BundleContext;
import ch.genevajug.crappy.serviceregistor.osgi.ServiceRegistration;
import org.fest.assertions.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.util.HashMap;
import java.util.Map;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.powermock.reflect.Whitebox.getInternalState;
import static org.powermock.reflect.Whitebox.setInternalState;

/**
* Created by IntelliJ IDEA.
* User: codingdojo
* Date: 11/2/11
* Time: 12:26 PM
* To change this template use File | Settings | File Templates.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdGenerator.class)
public class ServiceRegistratorTest {

@Mock
BundleContext bundleContext;

@InjectMocks
ServiceRegistrator serviceRegistrator = new ServiceRegistrator();

@Test
public void shouldAddServiceRegistrationToTheMap() {
Object serviceImplementation = mock(Object.class);
ServiceRegistration serviceRegistration = mock(ServiceRegistration.class);
PowerMockito.mockStatic(IdGenerator.class);

when(bundleContext.registerService(eq("Test"), eq(serviceImplementation), anyString())).thenReturn(serviceRegistration);
when(IdGenerator.generateNewId()).thenReturn(100L);

long id = serviceRegistrator.registerService("Test", serviceImplementation);

Map serviceRegistrationsMap = getInternalState(serviceRegistrator, Map.class);

assertThat(id).isEqualTo(100L);
assertThat(serviceRegistrationsMap).hasSize(1);
assertThat(serviceRegistrationsMap.get(100L)).isEqualTo(serviceRegistration);
}

@Test
public void mapShouldBeEmptyAfterUnregistration() {
ServiceRegistration serviceRegistration = mock(ServiceRegistration.class);
Map<Long, ServiceRegistration> serviceRegistrationsMap = new HashMap<Long, ServiceRegistration>();
serviceRegistrationsMap.put(100L, serviceRegistration);

setInternalState(serviceRegistrator, serviceRegistrationsMap);

serviceRegistrator.unregisterService(100L);

assertThat(serviceRegistrationsMap).isEmpty();
verify(serviceRegistration, times(1)).unregister();
}


@Test(expected = IllegalStateException.class)
public void shouldThrowIllegalExceptionWhenIdNotFound() {
Map serviceRegistrationsMap = mock(Map.class);
when(serviceRegistrationsMap.get(100L)).thenReturn(null);

setInternalState(serviceRegistrator, serviceRegistrationsMap);

serviceRegistrator.unregisterService(100L);
}
}

0 comments on commit 45409cf

Please sign in to comment.