Permalink
Browse files

Migrations to Mockito: ioc package

  • Loading branch information...
1 parent 92c45d4 commit 9268b66891faf3ba955b7a0a7810c382ebeb87cb @garcia-jj garcia-jj committed Sep 12, 2012
@@ -16,6 +16,8 @@
*/
package br.com.caelum.vraptor.ioc;
+import static org.mockito.Mockito.verify;
+
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractList;
@@ -24,30 +26,28 @@
import java.util.List;
import java.util.RandomAccess;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.ComponentRegistry;
+
/**
*
* @author Lucas Cavalcanti
*
*/
public class AbstractComponentRegistryTest {
-
- private Mockery mockery;
- private ComponentRegistry registry;
+ private @Mock ComponentRegistry registry;
private AbstractComponentRegistry abstractRegistry;
@Before
public void setUp() throws Exception {
- mockery = new Mockery();
- registry = mockery.mock(ComponentRegistry.class);
-
+ MockitoAnnotations.initMocks(this);
+
abstractRegistry = new AbstractComponentRegistry() {
public void register(Class<?> requiredType, Class<?> componentType) {
registry.register(requiredType, componentType);
@@ -56,34 +56,26 @@ public void register(Class<?> requiredType, Class<?> componentType) {
}
@Test
public void shouldRegisterComponentAndImplementedInterfaces() {
- mockery.checking(new Expectations() {{
-
- one(registry).register(Runnable.class, RunnableComponent.class);
- one(registry).register(RunnableComponent.class, RunnableComponent.class);
-
- }});
abstractRegistry.deepRegister(RunnableComponent.class);
- mockery.assertIsSatisfied();
+
+ verify(registry).register(Runnable.class, RunnableComponent.class);
+ verify(registry).register(RunnableComponent.class, RunnableComponent.class);
}
@Test
public void shouldRegisterComponentUsingAllPossibleSupertypes() {
- mockery.checking(new Expectations() {{
-
- one(registry).register(ArrayListSubclass.class, ArrayListSubclass.class);
- one(registry).register(ArrayList.class, ArrayListSubclass.class);
- one(registry).register(List.class, ArrayListSubclass.class);
- one(registry).register(Collection.class, ArrayListSubclass.class);
- one(registry).register(Iterable.class, ArrayListSubclass.class);
- one(registry).register(Cloneable.class, ArrayListSubclass.class);
- one(registry).register(Serializable.class, ArrayListSubclass.class);
- one(registry).register(RandomAccess.class, ArrayListSubclass.class);
- one(registry).register(AbstractList.class, ArrayListSubclass.class);
- one(registry).register(AbstractCollection.class, ArrayListSubclass.class);
-
- }});
abstractRegistry.deepRegister(ArrayListSubclass.class);
- mockery.assertIsSatisfied();
+
+ verify(registry).register(ArrayListSubclass.class, ArrayListSubclass.class);
+ verify(registry).register(ArrayList.class, ArrayListSubclass.class);
+ verify(registry).register(List.class, ArrayListSubclass.class);
+ verify(registry).register(Collection.class, ArrayListSubclass.class);
+ verify(registry).register(Iterable.class, ArrayListSubclass.class);
+ verify(registry).register(Cloneable.class, ArrayListSubclass.class);
+ verify(registry).register(Serializable.class, ArrayListSubclass.class);
+ verify(registry).register(RandomAccess.class, ArrayListSubclass.class);
+ verify(registry).register(AbstractList.class, ArrayListSubclass.class);
+ verify(registry).register(AbstractCollection.class, ArrayListSubclass.class);
}
@@ -97,6 +89,4 @@ public void run() {
@Component
public static class ArrayListSubclass extends ArrayList<Object> {
}
-
-
}
@@ -17,7 +17,6 @@
package br.com.caelum.vraptor.ioc;
import static org.junit.Assert.assertEquals;
-import junit.framework.Assert;
import org.junit.Test;
@@ -61,13 +60,13 @@ public String getInstance() {
@Test
public void shoudWorkWithSubclassesOfComponenetFactoryImplementations() {
Class<?> c = new ComponentFactoryIntrospector().targetTypeForComponentFactory((ClassThatImplementsCFExtending.class));
- Assert.assertEquals(String.class, c);
+ assertEquals(String.class, c);
}
@Test
public void shoudWorkWithImplementationsOfComponenetFactorySubinterfacesImplementations() {
Class<?> c = new ComponentFactoryIntrospector().targetTypeForComponentFactory((ClassThatImplementsCFIndirectly.class));
- Assert.assertEquals(String.class, c);
+ assertEquals(String.class, c);
}
interface InterfaceThatExtendCF extends ComponentFactory<String> {
@@ -19,6 +19,9 @@
import static br.com.caelum.vraptor.VRaptorMatchers.canHandle;
import static br.com.caelum.vraptor.VRaptorMatchers.hasOneCopyOf;
+import static br.com.caelum.vraptor.config.BasicConfiguration.BASE_PACKAGES_PARAMETER_NAME;
+import static br.com.caelum.vraptor.config.BasicConfiguration.SCANNING_PARAM;
+import static java.lang.Thread.currentThread;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
@@ -30,6 +33,8 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.net.URL;
import java.net.URLClassLoader;
@@ -44,8 +49,6 @@
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.junit.After;
@@ -55,7 +58,6 @@
import br.com.caelum.vraptor.ComponentRegistry;
import br.com.caelum.vraptor.Converter;
import br.com.caelum.vraptor.Result;
-import br.com.caelum.vraptor.config.BasicConfiguration;
import br.com.caelum.vraptor.converter.jodatime.LocalDateConverter;
import br.com.caelum.vraptor.converter.jodatime.LocalTimeConverter;
import br.com.caelum.vraptor.core.BaseComponents;
@@ -87,34 +89,25 @@
*/
public abstract class GenericContainerTest {
- protected Mockery mockery;
-
protected ContainerProvider provider;
-
protected ServletContext context;
-
protected abstract ContainerProvider getProvider();
-
protected abstract <T> T executeInsideRequest(WhatToDo<T> execution);
-
protected abstract void configureExpectations();
@Test
public void canProvideAllApplicationScopedComponents() {
checkAvailabilityFor(true, BaseComponents.getApplicationScoped().keySet());
- mockery.assertIsSatisfied();
}
@Test
public void canProvideAllPrototypeScopedComponents() {
checkAvailabilityFor(false, BaseComponents.getPrototypeScoped().keySet());
- mockery.assertIsSatisfied();
}
@Test
public void canProvideAllRequestScopedComponents() {
checkAvailabilityFor(false, BaseComponents.getRequestScoped().keySet());
- mockery.assertIsSatisfied();
}
@ApplicationScoped
@@ -125,7 +118,6 @@ public void canProvideAllRequestScopedComponents() {
@Test
public void processesCorrectlyAppBasedComponents() {
checkAvailabilityFor(true, MyAppComponent.class, MyAppComponent.class);
- mockery.assertIsSatisfied();
}
@Test
public void canProvideJodaTimeConverters() {
@@ -142,7 +134,6 @@ public String execute(RequestInfo request, int counter) {
}
});
- mockery.assertIsSatisfied();
}
@ApplicationScoped
@@ -210,8 +201,8 @@ public Void insideRequest(Container container) {
@Test
public void processesCorrectlyRequestBasedComponents() {
checkAvailabilityFor(false, MyRequestComponent.class, MyRequestComponent.class);
- mockery.assertIsSatisfied();
}
+
@Component
@PrototypeScoped
public static class MyPrototypeComponent {
@@ -236,7 +227,6 @@ public Object insideRequest(Container container) {
});
}
});
- mockery.assertIsSatisfied();
}
@Test
@@ -262,29 +252,21 @@ public void supportsComponentFactoriesForCustomInstantiation() {
@Before
public void setup() throws Exception {
- this.mockery = new Mockery();
- this.context = mockery.mock(ServletContext.class, "servlet context");
-
- mockery.checking(new Expectations() {{
- allowing(context).getMajorVersion();
- will(returnValue(3));
-
- allowing(context).getInitParameter(BasicConfiguration.BASE_PACKAGES_PARAMETER_NAME);
- will(returnValue("br.com.caelum.vraptor.ioc.fixture"));
-
- allowing(context).getRealPath("/WEB-INF/classes");
- will(returnValue(getClassDir()));
+ context = mock(ServletContext.class, "servlet context");
- allowing(context).getClassLoader();
- will(returnValue(new URLClassLoader(new URL[] {ScannotationComponentScannerTest.class.getResource("/test-fixture.jar")}, Thread.currentThread().getContextClassLoader())));
+ when(context.getMajorVersion()).thenReturn(3);
+ when(context.getInitParameter(BASE_PACKAGES_PARAMETER_NAME)).thenReturn("br.com.caelum.vraptor.ioc.fixture");
+ when(context.getRealPath("/WEB-INF/classes")).thenReturn(getClassDir());
- allowing(context).getInitParameter(BasicConfiguration.ENCODING);
- allowing(context).getInitParameter(BasicConfiguration.SCANNING_PARAM);
- will(returnValue("enabled"));
+ when(context.getClassLoader()).thenReturn(
+ new URLClassLoader(new URL[] {ScannotationComponentScannerTest.class.getResource("/test-fixture.jar")},
+ currentThread().getContextClassLoader()));
- allowing(context).setAttribute(with(any(String.class)), with(any(Object.class)));
+ //allowing(context).getInitParameter(ENCODING);
+ //allowing(context).setAttribute(with(any(String.class)), with(any(Object.class)));
+
+ when(context.getInitParameter(SCANNING_PARAM)).thenReturn("enabled");
- }});
configureExpectations();
provider = getProvider();
provider.start(context);
@@ -309,7 +291,7 @@ public T insideRequest(Container secondContainer) {
registry.register(componentToRegister, componentToRegister);
}
- ResourceMethod secondMethod = mockery.mock(ResourceMethod.class, "rm" + counter);
+ ResourceMethod secondMethod = mock(ResourceMethod.class, "rm" + counter);
secondContainer.instanceFor(MethodInfo.class).setResourceMethod(secondMethod);
return secondContainer.instanceFor(component);
}
@@ -332,7 +314,7 @@ public T insideRequest(Container firstContainer) {
ComponentRegistry registry = firstContainer.instanceFor(ComponentRegistry.class);
registry.register(componentToRegister, componentToRegister);
}
- ResourceMethod firstMethod = mockery.mock(ResourceMethod.class, "rm" + counter);
+ ResourceMethod firstMethod = mock(ResourceMethod.class, "rm" + counter);
firstContainer.instanceFor(MethodInfo.class).setResourceMethod(firstMethod);
return firstContainer.instanceFor(componentToBeRetrieved);
}
@@ -8,11 +8,8 @@
import java.util.Collection;
-import javax.servlet.http.HttpSessionListener;
-
import net.vidageek.mirror.dsl.Mirror;
-import org.jmock.Expectations;
import org.junit.Test;
import br.com.caelum.vraptor.ioc.ContainerProvider;
@@ -30,7 +27,6 @@ protected ContainerProvider getProvider() {
return new GuiceProvider();
}
-
@Test
public void shouldBeAbleToOverrideVRaptorsDefaultImplementation() throws Exception {
MethodNotAllowedHandler handler = registerAndGetFromContainer(MethodNotAllowedHandler.class, CustomMethodNotAllowedHandler.class);
@@ -53,12 +49,4 @@ public void shouldBeAbleToReceiveListsOfSerializationsAsDependency() throws Exce
Collection serializations = (Collection) new Mirror().on(instance).get().field("serializations");
assertFalse(serializations.isEmpty());
}
- @Override
- protected void configureExpectations() {
- mockery.checking(new Expectations() {{
- allowing(context).addListener(with(any(HttpSessionListener.class)));
- }});
- super.configureExpectations();
- }
-
}
@@ -15,15 +15,12 @@
public class SessionCustomScopeTest {
-
private static final String SESSION_ID = "abc";
private SessionCustomScope scope;
private @Mock HttpSession session;
private @Mock LifecycleListener listener;
-
-
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
@@ -58,6 +55,5 @@ public void shouldRemoveListenersOnStop() throws Exception {
scope.stop(session);
verify(listener).onEvent();
-
}
}
Oops, something went wrong. Retry.

0 comments on commit 9268b66

Please sign in to comment.