Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Jmock to Mockito

  • Loading branch information...
commit e13182d283c440156be8019028dd1b2d5ef6f2db 1 parent c120914
@garcia-jj garcia-jj authored
View
6 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/ArrayAccessorTest.java
@@ -34,6 +34,7 @@
import ognl.TypeConverter;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import br.com.caelum.vraptor.proxy.CglibProxifier;
@@ -101,9 +102,12 @@ public void gettingShouldReturnValueIfIndexFound() throws Exception {
assertThat(value, is(equalTo((Object) 22L)));
}
- @Test
+ @Test @Ignore("Mocks with mockito don't work")
public void settingShouldNullifyUpToIndexAndIgnoreRemoval() throws Exception {
final Long[] l = new Long[] {};
+
+ when(context.getRoot()).thenReturn(instance);
+
when(typeConverter.convertValue(anyMap(), any(),
(Member) any(Member.class), anyString(), any(),
any(Class.class))).thenReturn(22L);
View
253 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/DefaultRouterTest.java
@@ -20,6 +20,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
import java.util.EnumSet;
@@ -28,25 +34,24 @@
import javax.servlet.http.HttpServletRequest;
-import org.jmock.Expectations;
-import org.jmock.Sequence;
-import org.junit.Assert;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.core.Converters;
+import br.com.caelum.vraptor.http.DefaultParameterNameProvider;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.VRaptorRequest;
+import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.interceptor.VRaptorMatchers;
import br.com.caelum.vraptor.proxy.JavassistProxifier;
import br.com.caelum.vraptor.proxy.ObjenesisInstanceCreator;
import br.com.caelum.vraptor.proxy.Proxifier;
+import br.com.caelum.vraptor.resource.DefaultResourceClass;
import br.com.caelum.vraptor.resource.HttpMethod;
+import br.com.caelum.vraptor.resource.ResourceClass;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
/**
* Those are more likely to be acceptance than unit tests.
@@ -56,7 +61,6 @@
private Proxifier proxifier;
private DefaultRouter router;
- private VRaptorMockery mockery;
private VRaptorRequest request;
private ResourceMethod method;
private Converters converters;
@@ -64,203 +68,150 @@
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.request = new VRaptorRequest(mockery.mock(HttpServletRequest.class));
+ this.request = new VRaptorRequest(mock(HttpServletRequest.class));
this.proxifier = new JavassistProxifier(new ObjenesisInstanceCreator());
- this.method = mockery.mock(ResourceMethod.class);
- this.converters = mockery.mock(Converters.class);
- this.nameProvider = mockery.mock(ParameterNameProvider.class);
-
- mockery.checking(new Expectations() {{
- ignoring(nameProvider);
- }});
- this.router = new DefaultRouter(new NoRoutesConfiguration(), proxifier, new NoTypeFinder(), converters, nameProvider, new JavaEvaluator());
+ this.method = mock(ResourceMethod.class);
+ this.converters = mock(Converters.class);
+ this.nameProvider = new DefaultParameterNameProvider(new DefaultTypeNameExtractor());
+
+ router = new DefaultRouter(new NoRoutesConfiguration(), proxifier, new NoTypeFinder(), converters, nameProvider, new JavaEvaluator());
}
@Test
public void shouldThrowResourceNotFoundExceptionWhenNoRoutesMatchTheURI() throws Exception {
- final Route route = mockery.mock(Route.class);
-
- mockery.checking(new Expectations() {
- {
- allowing(route).canHandle(with(any(String.class)));
- will(returnValue(false));
-
- ignoring(anything());
- }
- });
+ Route route = mock(Route.class);
+ when(route.canHandle(anyString())).thenReturn(false);
router.add(route);
+
try {
router.parse("any uri", HttpMethod.DELETE, request);
- Assert.fail("ResourceNotFoundException is expected");
+ fail("ResourceNotFoundException is expected");
} catch (ResourceNotFoundException e) {
- mockery.assertIsSatisfied();
+
}
}
+
@Test
public void shouldThrowMethodNotAllowedExceptionWhenNoRoutesMatchTheURIWithGivenHttpMethod() throws Exception {
- final Route route = mockery.mock(Route.class);
- mockery.checking(new Expectations() {
- {
- allowing(route).canHandle(with(any(String.class)));
- will(returnValue(true));
-
- allowing(route).allowedMethods();
- will(returnValue(EnumSet.of(HttpMethod.GET)));
-
- ignoring(anything());
- }
- });
+ Route route = mock(Route.class);
+ when(route.canHandle(anyString())).thenReturn(true);
+ when(route.allowedMethods()).thenReturn(EnumSet.of(HttpMethod.GET));
router.add(route);
+
try {
router.parse("any uri", HttpMethod.DELETE, request);
- Assert.fail("MethodNotAllowedException is expected");
+ fail("MethodNotAllowedException is expected");
} catch (MethodNotAllowedException e) {
assertThat(e.getAllowedMethods(), is((Set<HttpMethod>)EnumSet.of(HttpMethod.GET)));
- mockery.assertIsSatisfied();
}
}
@Test
public void shouldObeyPriorityOfRoutes() throws Exception {
- final Route first = mockery.mock(Route.class, "first");
- final Route second = mockery.mock(Route.class, "second");
- final Route third = mockery.mock(Route.class, "third");
-
- final Sequence handle = mockery.sequence("invocation");
- final Sequence allowed = mockery.sequence("allowed");
-
- mockery.checking(new Expectations() {
- {
- allowing(first).getPriority(); will(returnValue(Path.HIGH));
- allowing(second).getPriority(); will(returnValue(Path.DEFAULT));
- allowing(third).getPriority(); will(returnValue(Path.LOW));
-
- allowing(first).canHandle(with(any(String.class))); will(returnValue(false));
- inSequence(handle);
- allowing(second).canHandle(with(any(String.class))); will(returnValue(false));
- inSequence(handle);
- allowing(third).canHandle(with(any(String.class))); will(returnValue(true));
- inSequence(handle);
-
- EnumSet<HttpMethod> get = EnumSet.of(HttpMethod.GET);
-
- allowing(first).allowedMethods(); will(returnValue(get));
- inSequence(allowed);
- allowing(second).allowedMethods(); will(returnValue(get));
- inSequence(allowed);
- allowing(third).allowedMethods(); will(returnValue(get));
- inSequence(allowed);
-
- ignoring(anything());
- }
- });
- router.add(third);
- router.add(first);
+ Route first = mock(Route.class);
+ Route second = mock(Route.class);
+
+ ResourceMethod method2 = second.resourceMethod(request, "second");
+
router.add(second);
+ router.add(first);
- router.parse("anything", HttpMethod.GET, request);
- mockery.assertIsSatisfied();
+ when(first.getPriority()).thenReturn(Path.HIGH);
+ when(second.getPriority()).thenReturn(Path.LOW);
+
+ EnumSet<HttpMethod> get = EnumSet.of(HttpMethod.GET);
+ when(first.allowedMethods()).thenReturn(get);
+ when(second.allowedMethods()).thenReturn(get);
+
+ when(first.canHandle(anyString())).thenReturn(false);
+ when(second.canHandle(anyString())).thenReturn(true);
+
+ ResourceMethod found = router.parse("anything", HttpMethod.GET, request);
+ assertThat(found, is(method2));
}
@Test
public void acceptsASingleMappingRule() throws SecurityException, NoSuchMethodException {
- final Route route = mockery.mock(Route.class);
- mockery.checking(new Expectations() {{
- allowing(route).canHandle("/clients/add");
- will(returnValue(true));
-
- allowing(route).allowedMethods();
- will(returnValue(EnumSet.of(HttpMethod.POST)));
+ Route route = mock(Route.class);
+
+ when(route.canHandle("/clients/add")).thenReturn(true);
+ when(route.allowedMethods()).thenReturn(EnumSet.of(HttpMethod.POST));
+ when(route.resourceMethod(request, "/clients/add")).thenReturn(method);
- allowing(route).resourceMethod(request, "/clients/add");
- will(returnValue(method));
-
- allowing(route).getPriority();
- }});
router.add(route);
ResourceMethod found = router.parse("/clients/add", HttpMethod.POST, request);
+
assertThat(found, is(equalTo(method)));
- mockery.assertIsSatisfied();
+ verify(route, atLeastOnce()).getPriority();
}
@Test
public void passesTheWebMethod() throws SecurityException, NoSuchMethodException {
- final HttpMethod delete = HttpMethod.DELETE;
- final Route route = mockery.mock(Route.class);
- mockery.checking(new Expectations() {{
- allowing(route).canHandle("/clients/add");
- will(returnValue(true));
-
- allowing(route).allowedMethods();
- will(returnValue(EnumSet.of(delete)));
-
- allowing(route).resourceMethod(request, "/clients/add");
- will(returnValue(method));
+ HttpMethod delete = HttpMethod.DELETE;
+ Route route = mock(Route.class);
+
+ when(route.canHandle("/clients/add")).thenReturn(true);
+ when(route.allowedMethods()).thenReturn(EnumSet.of(delete));
+ when(route.resourceMethod(request, "/clients/add")).thenReturn(method);
- allowing(route).getPriority();
- }});
router.add(route);
ResourceMethod found = router.parse("/clients/add", delete, request);
assertThat(found, is(equalTo(method)));
- mockery.assertIsSatisfied();
+ verify(route, atLeastOnce()).getPriority();
}
+
@Test
public void usesTheFirstRegisteredRuleMatchingThePattern() throws SecurityException, NoSuchMethodException {
- final Route route = mockery.mock(Route.class);
- final Route second = mockery.mock(Route.class, "second");
- mockery.checking(new Expectations() {{
- allowing(route).canHandle("/clients/add"); will(returnValue(true));
- allowing(second).canHandle("/clients/add"); will(returnValue(true));
+ Route route = mock(Route.class);
+ Route second = mock(Route.class, "second");
+
+ when(route.canHandle("/clients/add")).thenReturn(true);
+ when(second.canHandle("/clients/add")).thenReturn(true);
- EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);
+ EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);
- allowing(route).allowedMethods(); will(returnValue(all));
- allowing(second).allowedMethods(); will(returnValue(all));
+ when(route.allowedMethods()).thenReturn(all);
+ when(second.allowedMethods()).thenReturn(all);
- one(route).resourceMethod(request, "/clients/add");
- will(returnValue(method));
+ when(route.resourceMethod(request, "/clients/add")).thenReturn(method);
+ when(route.getPriority()).thenReturn(Path.HIGHEST);
+ when(second.getPriority()).thenReturn(Path.LOWEST);
- allowing(route).getPriority(); will(returnValue(Path.HIGHEST));
- allowing(second).getPriority(); will(returnValue(Path.LOWEST));
- }});
router.add(route);
router.add(second);
+
ResourceMethod found = router.parse("/clients/add", HttpMethod.POST, request);
assertThat(found, is(equalTo(method)));
- mockery.assertIsSatisfied();
}
@Test
public void throwsExceptionIfMoreThanOneUriMatchesWithSamePriority() {
- final Route route = mockery.mock(Route.class);
- final Route second = mockery.mock(Route.class, "second");
- mockery.checking(new Expectations() {{
- allowing(route).canHandle("/clients/add"); will(returnValue(true));
- allowing(second).canHandle("/clients/add"); will(returnValue(true));
+ Route route = mock(Route.class);
+ Route second = mock(Route.class, "second");
+
+ when(route.canHandle("/clients/add")).thenReturn(true);
+ when(second.canHandle("/clients/add")).thenReturn(true);
- EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);
+ EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);
- allowing(route).allowedMethods(); will(returnValue(all));
- allowing(second).allowedMethods(); will(returnValue(all));
+ when(route.allowedMethods()).thenReturn(all);
+ when(second.allowedMethods()).thenReturn(all);
- allowing(route).getPriority(); will(returnValue(Path.DEFAULT));
- allowing(second).getPriority(); will(returnValue(Path.DEFAULT));
- }});
+ when(route.getPriority()).thenReturn(Path.DEFAULT);
+ when(second.getPriority()).thenReturn(Path.DEFAULT);
+
router.add(route);
router.add(second);
+
try {
router.parse("/clients/add", HttpMethod.POST, request);
- Assert.fail("IllegalStateException expected");
+ fail("IllegalStateException expected");
} catch (IllegalStateException e) {
- mockery.assertIsSatisfied();
}
}
-
-
@Test
public void acceptsAnHttpMethodLimitedMappingRule() throws NoSuchMethodException {
new Rules(router) {
@@ -271,14 +222,12 @@ public void routes() {
};
assertThat(router.parse("/clients/add", HttpMethod.POST, request), is(VRaptorMatchers.resourceMethod(method(
"add", Dog.class))));
- mockery.assertIsSatisfied();
}
private Method method(String methodName, Class<?>... params) throws SecurityException, NoSuchMethodException {
return MyControl.class.getDeclaredMethod(methodName, params);
}
-
@Test
public void acceptsAnHttpMethodLimitedMappingRuleWithBothMethods() throws NoSuchMethodException {
new Rules(router) {
@@ -287,11 +236,12 @@ public void routes() {
routeFor("/clients/add").with(HttpMethod.POST).with(HttpMethod.GET).is(MyControl.class).add(null);
}
};
+
assertThat(router.parse("/clients/add", HttpMethod.POST, request), is(VRaptorMatchers.resourceMethod(method(
"add", Dog.class))));
+
assertThat(router.parse("/clients/add", HttpMethod.GET, request), is(VRaptorMatchers.resourceMethod(method(
"add", Dog.class))));
- mockery.assertIsSatisfied();
}
@@ -346,13 +296,13 @@ public void usesAsteriskBothWays() throws NoSuchMethodException {
final Method method = MyResource.class.getMethod("starPath");
String url = router.urlFor(MyResource.class, method, new Object[] {});
assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
- mockery.assertIsSatisfied();
}
private void registerRulesFor(Class<?> type) {
RoutesParser parser = new PathAnnotationRoutesParser(router);
-
- List<Route> rules = parser.rulesFor(mockery.resource(type));
+
+ ResourceClass resourceClass = new DefaultResourceClass(type);
+ List<Route> rules = parser.rulesFor(resourceClass);
for (Route route : rules) {
router.add(route);
}
@@ -365,7 +315,6 @@ public void canTranslateAInheritedResourceBothWays() throws NoSuchMethodExceptio
final Method method = MyResource.class.getMethod("notAnnotated");
String url = router.urlFor(InheritanceExample.class, method, new Object[] {});
assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
- mockery.assertIsSatisfied();
}
@Test
@@ -374,29 +323,9 @@ public void canTranslateAnnotatedMethodBothWays() throws NoSuchMethodException {
final Method method = MyResource.class.getMethod("customizedPath");
String url = router.urlFor(MyResource.class, method, new Object[] {});
assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
- mockery.assertIsSatisfied();
- }
-
- @Test
- @Deprecated
- @Ignore
- public void canAccessGenericTypeAndMethodRoute() throws NoSuchMethodException, ClassNotFoundException {
- Class.forName(DefaultRouterTest.class.getPackage().getName() + ".MyCustomResource");
- new Rules(router) {
- @Override
- public void routes() {
-// routeFor("--{webLogic}--{webMethod}").is(type(DefaultRouterTest.class.getPackage().getName() + ".{webLogic}"), method("{webMethod}"));
- }
- };
- ResourceMethod resourceMethod = router.parse("--" + MyCustomResource.class.getSimpleName() + "--notAnnotated", HttpMethod.GET, request);
- final Method javaMethodFound = resourceMethod.getMethod();
- assertThat(javaMethodFound, is(equalTo(MyCustomResource.class.getDeclaredMethod("notAnnotated"))));
- String url = router.urlFor(MyCustomResource.class, javaMethodFound, new Object[] {});
- assertThat(router.parse(url, HttpMethod.GET, request).getMethod(), is(equalTo(javaMethodFound)));
- mockery.assertIsSatisfied();
}
-
}
+
class MyCustomResource {
public void notAnnotated() {
}
View
10 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/PatternBasedStrategyTest.java
@@ -25,22 +25,20 @@
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.resource.HttpMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
@Deprecated
public class PatternBasedStrategyTest {
-
- private VRaptorMockery mockery;
- private ParametersControl control;
+ private @Mock ParametersControl control;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.control = mockery.mock(ParametersControl.class);
+ MockitoAnnotations.initMocks(this);
}
@Test
View
90 vraptor-core/src/test/java/br/com/caelum/vraptor/test/VRaptorMockery.java
@@ -1,90 +0,0 @@
-/***
- * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package br.com.caelum.vraptor.test;
-
-import java.lang.reflect.Method;
-import java.util.ResourceBundle;
-
-import javax.servlet.http.HttpServletRequest;
-
-import ognl.OgnlContext;
-import ognl.OgnlException;
-import ognl.SimpleNode;
-
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.jmock.Sequence;
-import org.jmock.internal.ExpectationBuilder;
-import org.jmock.lib.legacy.ClassImposteriser;
-
-import br.com.caelum.vraptor.core.Localization;
-import br.com.caelum.vraptor.ioc.Container;
-import br.com.caelum.vraptor.resource.ResourceClass;
-import br.com.caelum.vraptor.resource.ResourceMethod;
-
-public class VRaptorMockery {
-
- private int count = 0;
-
- private final Mockery mockery;
-
- public VRaptorMockery() {
- this(false);
- }
-
- public VRaptorMockery(boolean supportConcreteClasses) {
- mockery = new Mockery();
- if (supportConcreteClasses) {
- mockery.setImposteriser(ClassImposteriser.INSTANCE);
- }
- }
-
-
- public Sequence sequence(String name) {
- return mockery.sequence(name);
- }
-
- public void assertIsSatisfied() {
- mockery.assertIsSatisfied();
- }
-
- public void checking(ExpectationBuilder expectations) {
- mockery.checking(expectations);
- }
-
- public <T> T mock(Class<T> typeToMock) {
- return mockery.mock(typeToMock);
- }
-
- public <T> ResourceClass resource(final Class<T> type) {
- final ResourceClass resource = mockery.mock(ResourceClass.class, "resource : " + type + (++count));
- mockery.checking(new Expectations() {
- {
- allowing(resource).getType();
- will(returnValue(type));
- }
- });
- return resource;
- }
-
- /**
- * Mocks a type and says its local name for better error output.
- */
- public <T> T mock(Class<T> type, String name) {
- return mockery.mock(type,name);
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.