Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Jmock to Mockito

  • Loading branch information...
commit c120914e50a13225d888a27e9bd9c23b8deefcff 1 parent f2d32f9
Otávio Garcia garcia-jj authored
Showing with 445 additions and 879 deletions.
  1. +56 −81 vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java
  2. +24 −30 vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java
  3. +7 −12 vraptor-core/src/test/java/br/com/caelum/vraptor/http/iogi/MiscIogiSupportTest.java
  4. +22 −36 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/ArrayAccessorTest.java
  5. +13 −34 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/GenericNullHandlerTest.java
  6. +25 −36 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/ListNullHandlerTest.java
  7. +18 −38 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/VRaptorConvertersAdapterTest.java
  8. +3 −4 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/DefaultRouterTest.java
  9. +15 −19 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/DefaultTypeFinderTest.java
  10. +14 −20 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/FixedMethodStrategyTest.java
  11. +5 −6 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RouteBuilderTest.java
  12. +11 −16 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java
  13. +75 −109 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExecuteMethodInterceptorTest.java
  14. +14 −37 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/ByteArrayDownloadTest.java
  15. +12 −10 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/FileDownloadTest.java
  16. +15 −38 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/InputStreamDownloadTest.java
  17. +14 −34 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/JFreeChartDownloadTest.java
  18. +14 −23 vraptor-core/src/test/java/br/com/caelum/vraptor/ioc/InterceptorStereotypeHandlerTest.java
  19. +24 −63 vraptor-core/src/test/java/br/com/caelum/vraptor/resource/HttpMethodTest.java
  20. +0 −104 vraptor-core/src/test/java/br/com/caelum/vraptor/test/VRaptorMockery.java
  21. +64 −129 vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java
137 vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java
View
@@ -16,9 +16,14 @@
*/
package br.com.caelum.vraptor;
+ import static br.com.caelum.vraptor.config.BasicConfiguration.CONTAINER_PROVIDER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.io.IOException;
@@ -31,9 +36,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.config.BasicConfiguration;
import br.com.caelum.vraptor.core.DefaultStaticContentHandler;
@@ -45,94 +51,69 @@
import br.com.caelum.vraptor.http.NullEncodingHandler;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.ioc.ContainerProvider;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class VRaptorTest {
- private VRaptorMockery mockery;
- private FilterConfig config;
- private ServletContext context;
- private static Container container;
- private RequestExecution execution;
+ private @Mock FilterConfig config;
+ private @Mock ServletContext context;
+ private @Mock static Container container;
+ private @Mock RequestExecution execution;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.config = mockery.mock(FilterConfig.class);
- this.context = mockery.mock(ServletContext.class);
- container = mockery.mock(Container.class);
- this.execution = mockery.mock(RequestExecution.class);
- started =stoped= false;
+ MockitoAnnotations.initMocks(this);
+ started = stoped = false;
}
@Test(expected = ServletException.class)
public void shoudlComplainIfNotInAServletEnviroment() throws IOException, ServletException {
- ServletRequest request = mockery.mock(ServletRequest.class);
- ServletResponse response = mockery.mock(ServletResponse.class);
+ ServletRequest request = mock(ServletRequest.class);
+ ServletResponse response = mock(ServletResponse.class);
+
new VRaptor().doFilter(request, response, null);
- mockery.assertIsSatisfied();
}
@Test
public void shouldExecuteARequestUsingTheSpecifiedContainer() throws ServletException, IOException,
VRaptorException {
- final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
- final ServletResponse response = mockery.mock(HttpServletResponse.class);
- mockery.checking(new Expectations() {
- {
- one(request).getRequestURI();
- will(returnValue("/unknown_file"));
- one(request).getContextPath();
- will(returnValue(""));
- one(context).getResource("/unknown_file");
- will(returnValue(null));
- one(config).getServletContext();
- will(returnValue(context));
- one(context).getInitParameter(BasicConfiguration.CONTAINER_PROVIDER);
- will(returnValue(MyProvider.class.getName()));
-
- one(context).getAttribute("container");
- will(returnValue(container));
- one(container).instanceFor(RequestExecution.class);
- will(returnValue(execution));
-
- one(container).instanceFor(StaticContentHandler.class);
- will(returnValue(new DefaultStaticContentHandler(context)));
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletResponse response = mock(HttpServletResponse.class);
+
+ when(request.getRequestURI()).thenReturn("/unknown_file");
+ when(request.getContextPath()).thenReturn("");
+ when(context.getResource("/unknown_file")).thenReturn(null);
+ when(config.getServletContext()).thenReturn(context);
+ when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER)).thenReturn(MyProvider.class.getName());
+
+ when(context.getAttribute("container")).thenReturn(container);
+ when(container.instanceFor(RequestExecution.class)).thenReturn(execution);
+
+ when(container.instanceFor(StaticContentHandler.class)).thenReturn(new DefaultStaticContentHandler(context));
+
+ when(container.instanceFor(EncodingHandler.class)).thenReturn(new NullEncodingHandler());
- one(execution).execute();
-
- one(container).instanceFor(EncodingHandler.class);
- will(returnValue(new NullEncodingHandler()));
- }
- });
- VRaptor raptor = new VRaptor();
- raptor.init(this.config);
- raptor.doFilter(request, response, null);
- mockery.assertIsSatisfied();
+ VRaptor vraptor = new VRaptor();
+ vraptor.init(this.config);
+ vraptor.doFilter(request, response, null);
+
+ verify(execution, times(1)).execute();
}
@Test
public void shouldStopContainer() throws ServletException, IOException,
VRaptorException {
- mockery.checking(new Expectations() {
- {
- one(config).getServletContext();
- will(returnValue(context));
- one(context).getInitParameter(BasicConfiguration.CONTAINER_PROVIDER);
- will(returnValue(MyProvider.class.getName()));
-
- one(container).instanceFor(StaticContentHandler.class);
- will(returnValue(new DefaultStaticContentHandler(context)));
- }
- });
- VRaptor raptor = new VRaptor();
- raptor.init(this.config);
+ when(config.getServletContext()).thenReturn(context);
+ when(context.getInitParameter(CONTAINER_PROVIDER)).thenReturn(MyProvider.class.getName());
+ when(container.instanceFor(StaticContentHandler.class)).thenReturn(new DefaultStaticContentHandler(context));
+
+ VRaptor vraptor = new VRaptor();
+ vraptor.init(this.config);
assertThat(started, is(equalTo(true)));
assertThat(stoped, is(equalTo(false)));
- raptor.destroy();
+
+ vraptor.destroy();
assertThat(started, is(equalTo(false)));
assertThat(stoped, is(equalTo(true)));
- mockery.assertIsSatisfied();
}
private static boolean started;
@@ -190,23 +171,17 @@ public Container getContainer() {
@Test
public void shouldDeferToContainerIfStaticFile() throws IOException, ServletException {
- VRaptor raptor = new VRaptor();
- final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
- final HttpServletResponse response = mockery.mock(HttpServletResponse.class);
- final StaticContentHandler handler = mockery.mock(StaticContentHandler.class);
- final FilterChain chain = mockery.mock(FilterChain.class);
- mockery.checking(new Expectations() {
- {
- one(handler).requestingStaticFile(request);
- will(returnValue(true));
- one(handler).deferProcessingToContainer(chain, request, response);
- allowing(request).setCharacterEncoding("UTF-8");
- allowing(response).setCharacterEncoding("UTF-8");
- }
- });
- raptor.init(new DoNothingProvider(handler));
- raptor.doFilter(request, response, chain);
- mockery.assertIsSatisfied();
+ VRaptor vraptor = new VRaptor();
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ StaticContentHandler handler = mock(StaticContentHandler.class);
+ FilterChain chain = mock(FilterChain.class);
+
+ when(handler.requestingStaticFile(request)).thenReturn(true);
+
+ vraptor.init(new DoNothingProvider(handler));
+ vraptor.doFilter(request, response, chain);
+
+ verify(handler, times(1)).deferProcessingToContainer(chain, request, response);
}
-
}
54 vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java
View
@@ -16,25 +16,32 @@
*/
package br.com.caelum.vraptor.core;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.IOException;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class ToInstantiateInterceptorHandlerTest {
- private VRaptorMockery mockery;
+ private @Mock Container container;
+ private @Mock Interceptor interceptor;
+ private @Mock InterceptorStack stack;
+ private @Mock ResourceMethod method;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
+ MockitoAnnotations.initMocks(this);
}
public static class MyWeirdInterceptor implements Interceptor {
@@ -56,7 +63,8 @@ public boolean accepts(ResourceMethod method) {
@Test(expected = InterceptionException.class)
public void shouldComplainWhenUnableToInstantiateAnInterceptor() throws InterceptionException, IOException {
- Container container = mockery.container(MyWeirdInterceptor.class, null);
+ when(container.instanceFor(MyWeirdInterceptor.class)).thenReturn(null);
+
ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container,
MyWeirdInterceptor.class);
handler.execute(null, null, null);
@@ -64,40 +72,26 @@ public void shouldComplainWhenUnableToInstantiateAnInterceptor() throws Intercep
@Test
public void shouldInvokeInterceptorsMethodIfAbleToInstantiateIt() throws InterceptionException, IOException {
- final Interceptor interceptor = mockery.mock(Interceptor.class);
- final InterceptorStack stack = mockery.mock(InterceptorStack.class);
- final ResourceMethod method = mockery.mock(ResourceMethod.class);
final Object instance = new Object();
- Container container = mockery.container(Interceptor.class, interceptor);
- mockery.checking(new Expectations() {
- {
- one(interceptor).accepts(method); will(returnValue(true));
- one(interceptor).intercept(stack, method, instance);
- }
- });
+
+ when(container.instanceFor(Interceptor.class)).thenReturn(interceptor);
+ when(interceptor.accepts(method)).thenReturn(true);
+
ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class);
handler.execute(stack, method, instance);
- mockery.assertIsSatisfied();
+
+ verify(interceptor).intercept(stack, method, instance);
}
@Test
public void shouldNotInvokeInterceptorsMethodIfInterceptorDoesntAcceptsResource() throws InterceptionException, IOException {
- final Interceptor interceptor = mockery.mock(Interceptor.class);
- final InterceptorStack stack = mockery.mock(InterceptorStack.class);
- final ResourceMethod method = mockery.mock(ResourceMethod.class);
final Object instance = new Object();
- Container container = mockery.container(Interceptor.class, interceptor);
- mockery.checking(new Expectations() {
- {
- one(interceptor).accepts(method); will(returnValue(false));
+ when(container.instanceFor(Interceptor.class)).thenReturn(interceptor);
+ when(interceptor.accepts(method)).thenReturn(false);
- never(interceptor).intercept(stack, method, instance);
-
- one(stack).next(method, instance);
- }
- });
ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class);
handler.execute(stack, method, instance);
- mockery.assertIsSatisfied();
+
+ verify(interceptor, never()).intercept(stack, method, instance);
+ verify(stack).next(method, instance);
}
-
}
19 vraptor-core/src/test/java/br/com/caelum/vraptor/http/iogi/MiscIogiSupportTest.java
View
@@ -30,6 +30,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.when;
import java.util.Calendar;
import java.util.GregorianCalendar;
@@ -37,10 +38,10 @@
import ognl.OgnlException;
-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.iogi.Iogi;
import br.com.caelum.iogi.parameters.Parameter;
@@ -60,16 +61,13 @@
*/
public class MiscIogiSupportTest {
- private Mockery mockery;
-
private Iogi iogi;
- private LocaleProvider mockLocaleProvider;
+ private @Mock LocaleProvider mockLocaleProvider;
@Before
public void setup() {
- this.mockery = new Mockery();
- this.mockLocaleProvider = mockery.mock(LocaleProvider.class);
- this.iogi = new Iogi(new NullDependencyProvider(), mockLocaleProvider);
+ MockitoAnnotations.initMocks(this);
+ iogi = new Iogi(new NullDependencyProvider(), mockLocaleProvider);
}
public static class Cat {
@@ -130,10 +128,7 @@ public void isCapableOfDealingWithEmptyParameterForInternalWrapperValue() {
public void isCapableOfDealingWithEmptyParameterForInternalValueWhichNeedsAConverter() throws OgnlException {
final Target<House> target = Target.create(House.class, "house");
final Parameter parameter = new Parameter("house.cat.firstLeg.birthDay", "10/5/2010");
- mockery.checking(new Expectations() {{
- allowing(mockLocaleProvider).getLocale();
- will(returnValue(new Locale("pt", "BR")));
- }});
+ when(mockLocaleProvider.getLocale()).thenReturn(new Locale("pt", "BR"));
final House house = iogi.instantiate(target, parameter);
assertThat(house.cat.firstLeg.birthDay, is(equalTo((Calendar)new GregorianCalendar(2010, 4, 10))));
}
58 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/ArrayAccessorTest.java
View
@@ -20,28 +20,29 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyMap;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.lang.reflect.Member;
-import java.util.Map;
import ognl.Evaluation;
import ognl.OgnlContext;
import ognl.SimpleNode;
import ognl.TypeConverter;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import br.com.caelum.vraptor.proxy.CglibProxifier;
import br.com.caelum.vraptor.proxy.Proxifier;
import br.com.caelum.vraptor.proxy.ReflectionInstanceCreator;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class ArrayAccessorTest {
private ArrayAccessor accessor;
- private VRaptorMockery mockery;
private OgnlContext context;
private Evaluation evaluation;
private SimpleNode node;
@@ -62,12 +63,11 @@ public void setSimpleNode(Long[] simpleNode) {
@Before
public void setup() {
- this.mockery = new VRaptorMockery(true);
- this.context = mockery.mock(OgnlContext.class);
- this.evaluation = mockery.mock(Evaluation.class);
- this.node = mockery.mock(SimpleNode.class);
+ this.context = mock(OgnlContext.class);
+ this.evaluation = mock(Evaluation.class);
+ this.node = mock(SimpleNode.class);
this.instance = new Data();
- this.typeConverter = mockery.mock(TypeConverter.class);
+ this.typeConverter = mock(TypeConverter.class);
final EmptyElementsRemoval removal = new EmptyElementsRemoval() {
@Override
public void removeExtraElements() {
@@ -76,24 +76,15 @@ public void removeExtraElements() {
};
final Proxifier proxifier = new CglibProxifier(new ReflectionInstanceCreator());
accessor = new ArrayAccessor();
- mockery.checking(new Expectations() {
- {
- allowing(context).get("removal"); will(returnValue(removal));
- allowing(context).get("proxifier"); will(returnValue(proxifier));
- allowing(context).getCurrentEvaluation();
- will(returnValue(evaluation));
- allowing(evaluation).getPrevious();
- will(returnValue(evaluation));
- allowing(evaluation).getNode();
- will(returnValue(node));
- one(node).toString();
- will(returnValue("values"));
- allowing(evaluation).getSource();
- will(returnValue(instance));
- one(context).getTypeConverter();
- will(returnValue(typeConverter));
- }
- });
+
+ when(context.get("removal")).thenReturn(removal);
+ when(context.get("proxifier")).thenReturn(proxifier);
+ when(context.getCurrentEvaluation()).thenReturn(evaluation);
+ when(evaluation.getPrevious()).thenReturn(evaluation);
+ when(evaluation.getNode()).thenReturn(node);
+ when(node.toString()).thenReturn("values");
+ when(evaluation.getSource()).thenReturn(instance);
+ when(context.getTypeConverter()).thenReturn(typeConverter);
}
@Test
@@ -113,15 +104,10 @@ public void gettingShouldReturnValueIfIndexFound() throws Exception {
@Test
public void settingShouldNullifyUpToIndexAndIgnoreRemoval() throws Exception {
final Long[] l = new Long[] {};
- mockery.checking(new Expectations() {
- {
- allowing(context).getRoot();
- one(typeConverter).convertValue((Map) with(anything()), with(anything()),
- (Member) with(anything()), (String) with(an(String.class)), with(anything()),
- (Class) with(an(Class.class)));
- will(returnValue(22L));
- }
- });
+ when(typeConverter.convertValue(anyMap(), any(),
+ (Member) any(Member.class), anyString(), any(),
+ any(Class.class))).thenReturn(22L);
+
accessor.setProperty(context, l, 1, 22L);
assertThat(instance.simpleNode[0], is(nullValue()));
assertThat(instance.simpleNode[1], is(equalTo(22L)));
47 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/GenericNullHandlerTest.java
View
@@ -21,6 +21,8 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Calendar;
@@ -34,26 +36,23 @@
import java.util.SortedSet;
import java.util.TreeSet;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.ioc.Container;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class GenericNullHandlerTest {
- private VRaptorMockery mockery;
- private EmptyElementsRemoval removal;
- private Container container;
+ private @Mock EmptyElementsRemoval removal;
+ private @Mock Container container;
private GenericNullHandler handler;
@Before
public void setup() {
- this.mockery = new VRaptorMockery(true);
- this.removal = mockery.mock(EmptyElementsRemoval.class);
- this.container = mockery.mock(Container.class);
- this.handler = new GenericNullHandler(removal);
+ MockitoAnnotations.initMocks(this);
+ handler = new GenericNullHandler(removal);
}
@Test(expected = IllegalArgumentException.class)
@@ -83,63 +82,43 @@ public void shouldInstantiateGregorianCalendarForAbstractCalendarType()
@Test
public void shouldInstantiateArrayListForCollectionInterface()
throws Exception {
- mockery.checking(new Expectations() {
- {
- one(removal).add(new ArrayList<Object>());
- }
- });
Collection<?> collection = handler.instantiate(Collection.class);
assertThat(collection, is(notNullValue()));
assertThat(collection, is(instanceOf(ArrayList.class)));
+ verify(removal, times(1)).add(new ArrayList<Object>());
}
@Test
public void shouldInstantiateArrayListForListInterface() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(removal).add(new ArrayList<Object>());
- }
- });
List<?> list = handler.instantiate(List.class);
assertThat(list, is(notNullValue()));
assertThat(list, is(instanceOf(ArrayList.class)));
+ verify(removal, times(1)).add(new ArrayList<Object>());
}
@Test
public void shouldInstantiateLinkedListForQueueInterface() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(removal).add(new LinkedList<Object>());
- }
- });
Queue<?> queue = handler.instantiate(Queue.class);
assertThat(queue, is(notNullValue()));
assertThat(queue, is(instanceOf(LinkedList.class)));
+ verify(removal, times(1)).add(new LinkedList<Object>());
}
@Test
public void shouldInstantiateHashSetListForSetInterface() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(removal).add(new HashSet<Object>());
- }
- });
Set<?> set = handler.instantiate(Set.class);
assertThat(set, is(notNullValue()));
assertThat(set, is(instanceOf(HashSet.class)));
+ verify(removal, times(1)).add(new HashSet<Object>());
}
@Test
public void shouldInstantiateTreeSetListForSortedSetInterface()
throws Exception {
- mockery.checking(new Expectations() {
- {
- one(removal).add(new TreeSet<Object>());
- }
- });
Set<?> set = handler.instantiate(SortedSet.class);
assertThat(set, is(notNullValue()));
assertThat(set, is(instanceOf(TreeSet.class)));
+ verify(removal, times(1)).add(new TreeSet<Object>());
}
}
61 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/ListNullHandlerTest.java
View
@@ -21,38 +21,35 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import ognl.Evaluation;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.SimpleNode;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.VRaptorException;
-import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.proxy.CglibProxifier;
-import br.com.caelum.vraptor.proxy.Proxifier;
import br.com.caelum.vraptor.proxy.ReflectionInstanceCreator;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class ListNullHandlerTest {
- private VRaptorMockery mockery;
private ListNullHandler handler;
- private Container container;
private Client client;
- private Evaluation evaluation;
- private EmptyElementsRemoval removal;
private OgnlContext context;
+ private @Mock Evaluation evaluation;
+ private @Mock EmptyElementsRemoval removal;
public static class Client {
private List nonGeneric = new ArrayList();
@@ -74,11 +71,10 @@ public void setNames(List<Client> names) {
@Before
public void setup() {
- this.mockery = new VRaptorMockery(true);
- this.client = new Client();
- this.evaluation = mockery.mock(Evaluation.class);
- this.removal = mockery.mock(EmptyElementsRemoval.class);
- this.handler = new ListNullHandler(removal);
+ MockitoAnnotations.initMocks(this);
+
+ client = new Client();
+ handler = new ListNullHandler(removal);
context = (OgnlContext) Ognl.createDefaultContext(null);
context.put("proxifier", new CglibProxifier(new ReflectionInstanceCreator()));
@@ -87,35 +83,28 @@ public void setup() {
@Test(expected = VRaptorException.class)
public void shouldNotSupportNonGenericLists() throws InstantiationException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
- final SimpleNode node = mockery.ognlNode("nonGeneric");
- mockery.checking(new Expectations() {
- {
- one(evaluation).getNode();
- will(returnValue(node));
- one(evaluation).getSource();
- will(returnValue(client));
- }
- });
+ SimpleNode node = mock(SimpleNode.class);
+ when(node.toString()).thenReturn("nonGeneric");
+
+ when(evaluation.getNode()).thenReturn(node);
+ when(evaluation.getSource()).thenReturn(client);
+
handler.instantiate(client.nonGeneric, "2", handler.getListType(client.nonGeneric, evaluation, context));
- mockery.assertIsSatisfied();
}
@Test
public void shouldInsertItemsUntilTheRequiredPosition() throws InstantiationException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
- final SimpleNode node = mockery.ognlNode("names");
- mockery.checking(new Expectations() {
- {
- one(evaluation).getNode();
- will(returnValue(node));
- one(evaluation).getSource();
- will(returnValue(client));
- one(removal).add(client.names);
- }
- });
+ SimpleNode node = mock(SimpleNode.class);
+ when(node.toString()).thenReturn("names");
+
+ when(evaluation.getNode()).thenReturn(node);
+ when(evaluation.getSource()).thenReturn(client);
+
handler.instantiate(client.names, 2, handler.getListType(client.names, evaluation, context));
assertThat(client.names.size(), is(equalTo(3)));
assertThat(client.names.get(2), is(notNullValue()));
- mockery.assertIsSatisfied();
+
+ verify(removal).add(client.names);
}
}
56 vraptor-core/src/test/java/br/com/caelum/vraptor/http/ognl/VRaptorConvertersAdapterTest.java
View
@@ -20,6 +20,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
@@ -29,31 +30,28 @@
import ognl.Ognl;
import ognl.OgnlException;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.Converter;
import br.com.caelum.vraptor.core.Converters;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class VRaptorConvertersAdapterTest {
- private VRaptorMockery mockery;
- private Converters converters;
+ private @Mock Converters converters;
+ private @Mock Converter converter;
private VRaptorConvertersAdapter adapter;
private Cat myCat;
- private Converter converter;
private ResourceBundle bundle;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.converters = mockery.mock(Converters.class);
- this.bundle = ResourceBundle.getBundle("messages");
- this.adapter = new VRaptorConvertersAdapter(converters, bundle);
- this.converter = mockery.mock(Converter.class);
- this.myCat = new Cat();
+ MockitoAnnotations.initMocks(this);
+ bundle = ResourceBundle.getBundle("messages");
+ adapter = new VRaptorConvertersAdapter(converters, bundle);
+ myCat = new Cat();
}
public static class Cat {
@@ -107,55 +105,37 @@ public Tail(int l) {
@Test
@SuppressWarnings("unchecked")
public void shouldInvokePrimitiveConverter() throws OgnlException {
- mockery.checking(new Expectations() {
- {
- one(converters).to(int.class);
- will(returnValue(converter));
- one(converter).convert("2", int.class, bundle);
- will(returnValue(2));
- }
- });
+ when(converters.to(int.class)).thenReturn(converter);
+ when(converter.convert("2", int.class, bundle)).thenReturn(2);
+
Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
Ognl.setValue("length", context, myCat, "2");
assertThat(myCat.length, is(equalTo(2)));
- mockery.assertIsSatisfied();
}
@SuppressWarnings("unchecked")
@Test
public void shouldInvokeCustomTypeConverter() throws OgnlException {
- mockery.checking(new Expectations() {
- {
- one(converters).to(Tail.class);
- will(returnValue(converter));
- one(converter).convert("15", Tail.class, bundle);
- will(returnValue(new Tail(15)));
- }
- });
+ when(converters.to(Tail.class)).thenReturn(converter);
+ when(converter.convert("15", Tail.class, bundle)).thenReturn(new Tail(15));
+
Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
Ognl.setValue("tail", context, myCat, "15");
assertThat(myCat.tail.length, is(equalTo(15)));
- mockery.assertIsSatisfied();
}
@Test(expected=IllegalArgumentException.class)
public void shouldThrowExceptionIfNoConverterIsFound() throws Throwable {
- mockery.checking(new Expectations() {
- {
- one(converters).to(Tail.class);
- will(returnValue(null));
- }
- });
- Map context = Ognl.createDefaultContext(myCat);
+ when(converters.to(Tail.class)).thenReturn(null);
+
+ Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
try {
Ognl.setValue("tail", context, myCat, "15");
} catch (OgnlException e) {
- mockery.assertIsSatisfied();
throw e.getCause();
}
}
-
}
7 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/DefaultRouterTest.java
View
@@ -343,8 +343,7 @@ public void starPath() {
@Test
public void usesAsteriskBothWays() throws NoSuchMethodException {
registerRulesFor(MyResource.class);
- final ResourceMethod resourceMethod = mockery.methodFor(MyResource.class, "starPath");
- final Method method = resourceMethod.getMethod();
+ 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();
@@ -363,7 +362,7 @@ private void registerRulesFor(Class<?> type) {
public void canTranslateAInheritedResourceBothWays() throws NoSuchMethodException {
registerRulesFor(MyResource.class);
registerRulesFor(InheritanceExample.class);
- final Method method = mockery.methodFor(MyResource.class, "notAnnotated").getMethod();
+ 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();
@@ -372,7 +371,7 @@ public void canTranslateAInheritedResourceBothWays() throws NoSuchMethodExceptio
@Test
public void canTranslateAnnotatedMethodBothWays() throws NoSuchMethodException {
registerRulesFor(MyResource.class);
- final Method method = mockery.methodFor(MyResource.class, "customizedPath").getMethod();
+ 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();
34 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/DefaultTypeFinderTest.java
View
@@ -17,20 +17,28 @@
package br.com.caelum.vraptor.http.route;
import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
import java.util.Map;
import net.vidageek.mirror.dsl.Mirror;
-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.http.ParameterNameProvider;
public class DefaultTypeFinderTest {
+ private @Mock ParameterNameProvider provider;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ }
public static class AController {
public void aMethod(Bean bean, String path) {
@@ -58,16 +66,10 @@ public Integer getId() {
}
@Test
public void shouldGetTypesCorrectly() throws Exception {
- Mockery mockery = new Mockery();
- final ParameterNameProvider provider = mockery.mock(ParameterNameProvider.class);
final Method method = new Mirror().on(AController.class).reflect().method("aMethod").withArgs(Bean.class, String.class);
- mockery.checking(new Expectations() {
- {
- one(provider).parameterNamesFor(method);
- will(returnValue(new String[] {"bean", "path"}));
- }
- });
+ when(provider.parameterNamesFor(method)).thenReturn(new String[] {"bean", "path"});
+
DefaultTypeFinder finder = new DefaultTypeFinder(provider);
Map<String, Class<?>> types = finder.getParameterTypes(method, new String[] {"bean.bean2.id", "path"});
@@ -76,16 +78,10 @@ public void shouldGetTypesCorrectly() throws Exception {
}
@Test
public void shouldGetTypesCorrectlyOnInheritance() throws Exception {
- Mockery mockery = new Mockery();
- final ParameterNameProvider provider = mockery.mock(ParameterNameProvider.class);
-
final Method method = new Mirror().on(AController.class).reflect().method("otherMethod").withArgs(BeanExtended.class);
- mockery.checking(new Expectations() {
- {
- one(provider).parameterNamesFor(method);
- will(returnValue(new String[] {"extended"}));
- }
- });
+
+ when(provider.parameterNamesFor(method)).thenReturn(new String[] {"extended"});
+
DefaultTypeFinder finder = new DefaultTypeFinder(provider);
Map<String, Class<?>> types = finder.getParameterTypes(method, new String[] {"extended.id"});
34 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/FixedMethodStrategyTest.java
View
@@ -21,51 +21,47 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
+import static org.mockito.Mockito.only;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
-import java.util.Arrays;
import java.util.EnumSet;
-import java.util.HashSet;
import java.util.Set;
-import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.interceptor.VRaptorMatchers;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.HttpMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
+
+import com.google.common.collect.Sets;
public class FixedMethodStrategyTest {
- private VRaptorMockery mockery;
- private MutableRequest request;
- private ParametersControl control;
+ private @Mock MutableRequest request;
+ private @Mock ParametersControl control;
private ResourceMethod list;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.request = mockery.mock(MutableRequest.class);
- this.control = mockery.mock(ParametersControl.class);
- this.list = DefaultResourceMethod.instanceFor(MyControl.class, method("list"));
+ MockitoAnnotations.initMocks(this);
+ list = DefaultResourceMethod.instanceFor(MyControl.class, method("list"));
}
@Test
public void canTranslate() {
FixedMethodStrategy strategy = new FixedMethodStrategy("abc", list,
methods(HttpMethod.POST), control, 0, new String[] {});
- mockery.checking(new Expectations() {
- {
- one(control).matches("/clients/add"); will(returnValue(true));
- one(control).fillIntoRequest("/clients/add", request);
- }
- });
+ when(control.matches("/clients/add")).thenReturn(true);
ResourceMethod match = strategy.resourceMethod(request, "/clients/add");
assertThat(match, is(VRaptorMatchers.resourceMethod(method("list"))));
+ verify(control, only()).fillIntoRequest("/clients/add", request);
}
@Test
@@ -80,9 +76,8 @@ public void areEquals() throws Exception {
assertThat(first, not(equalTo(forth)));
}
- @SuppressWarnings("unchecked")
private Set<HttpMethod> methods(HttpMethod method) {
- return new HashSet(Arrays.asList(method));
+ return Sets.newHashSet(method);
}
private Method method(String name, Class... types) {
@@ -121,5 +116,4 @@ public void list() {
public void show(Dog dog) {
}
}
-
}
11 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RouteBuilderTest.java
View
@@ -21,7 +21,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
@@ -30,6 +29,8 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.http.ParameterNameProvider;
@@ -42,12 +43,12 @@
public class RouteBuilderTest {
- private ParameterNameProvider provider;
+ private @Mock ParameterNameProvider provider;
+ private @Mock Converters converters;
private RouteBuilder builder;
private ResourceMethod method;
private Proxifier proxifier;
private TypeFinder typeFinder;
- private Converters converters;
public static class MyResource {
@@ -58,7 +59,7 @@ public void method(String abc, Integer def, BigDecimal ghi) {
@Before
public void setUp() throws Exception {
- provider = mock(ParameterNameProvider.class);
+ MockitoAnnotations.initMocks(this);
when(provider.parameterNamesFor(any(Method.class))).thenReturn(new String[] { "abc", "def", "ghi" });
@@ -68,8 +69,6 @@ public void setUp() throws Exception {
proxifier = new JavassistProxifier(new ObjenesisInstanceCreator());
typeFinder = new DefaultTypeFinder(provider);
- converters = mock(Converters.class);
-
}
@Test
27 vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java
View
@@ -17,30 +17,27 @@
package br.com.caelum.vraptor.http.route;
-import org.jmock.Expectations;
+import static org.mockito.Mockito.when;
+
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.proxy.Proxifier;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class RulesTest {
- private VRaptorMockery mockery;
- private Router router;
- private Proxifier proxifier;
+ private @Mock Router router;
+ private @Mock Proxifier proxifier;
+ private DefaultRouteBuilder routeBuilder;
@Before
public void setup() {
- this.mockery = new VRaptorMockery();
- this.router = mockery.mock(Router.class);
- this.proxifier = mockery.mock(Proxifier.class);
- mockery.checking(new Expectations() {
- {
- one(router).builderFor("");
- will(returnValue(new DefaultRouteBuilder(proxifier, null, null, null, new JavaEvaluator(), "")));
- }
- });
+ MockitoAnnotations.initMocks(this);
+
+ routeBuilder = new DefaultRouteBuilder(proxifier, null, null, null, new JavaEvaluator(), "");
+ when(router.builderFor("")).thenReturn(routeBuilder);
}
@Test(expected=IllegalRouteException.class)
@@ -51,7 +48,5 @@ public void routes() {
routeFor("");
}
};
- mockery.assertIsSatisfied();
}
-
}
184 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/ExecuteMethodInterceptorTest.java
View
@@ -18,17 +18,24 @@
package br.com.caelum.vraptor.interceptor;
import static br.com.caelum.vraptor.view.Results.nothing;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.util.Collections;
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.jmock.Expectations;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Validator;
@@ -36,25 +43,20 @@
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.ValidationException;
public class ExecuteMethodInterceptorTest {
- private VRaptorMockery mockery;
- private MethodInfo info;
- private InterceptorStack stack;
- private Validator validator;
+ private @Mock MethodInfo info;
+ private @Mock InterceptorStack stack;
+ private @Mock Validator validator;
private ExecuteMethodInterceptor interceptor;
@Before
public void setup() throws NoSuchMethodException {
- this.mockery = new VRaptorMockery();
- this.info = mockery.mock(MethodInfo.class);
- this.stack = mockery.mock(InterceptorStack.class);
- this.validator = mockery.mock(Validator.class);
- this.interceptor = new ExecuteMethodInterceptor(info, validator);
+ MockitoAnnotations.initMocks(this);
+ interceptor = new ExecuteMethodInterceptor(info, validator);
}
@Test
@@ -65,62 +67,48 @@ public void shouldAcceptAlways() {
@Test
public void shouldInvokeTheMethodAndNotProceedWithInterceptorStack() throws SecurityException,
NoSuchMethodException, IOException, InterceptionException {
- final ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark"));
- final DogAlike auau = mockery.mock(DogAlike.class);
- mockery.checking(new Expectations() {
- {
- one(auau).bark();
- one(info).getParameters();
- will(returnValue(new Object[] {}));
- one(stack).next(method, auau);
- one(info).setResult("ok");
- allowing(validator);
- }
- });
+ ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark"));
+ DogAlike auau = mock(DogAlike.class);
+ when(info.getParameters()).thenReturn(new Object[0]);
+
interceptor.intercept(stack, method, auau);
- mockery.assertIsSatisfied();
+
+ verify(auau).bark();
+ verify(stack).next(method, auau);
+ verify(info).setResult("ok");
}
@Test
public void shouldThrowMethodExceptionIfThereIsAnInvocationException() throws IOException, SecurityException,
NoSuchMethodException {
ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark"));
- final DogAlike auau = mockery.mock(DogAlike.class);
+ final DogAlike auau = mock(DogAlike.class);
final RuntimeException exception = new RuntimeException();
- mockery.checking(new Expectations() {
- {
- one(auau).bark();
- will(throwException(exception));
- one(info).getParameters();
- will(returnValue(new Object[] {}));
- }
- });
+
+ doThrow(exception).when(auau).bark();
+ when(info.getParameters()).thenReturn(new Object[0]);
+
try {
interceptor.intercept(stack, method, auau);
Assert.fail();
} catch (InterceptionException e) {
- MatcherAssert.assertThat((RuntimeException) e.getCause(), Matchers.is(Matchers.equalTo(exception)));
- mockery.assertIsSatisfied();
+ assertThat((RuntimeException) e.getCause(), is(equalTo(exception)));
}
}
@Test
public void shouldUseTheProvidedArguments() throws SecurityException, NoSuchMethodException, InterceptionException,
IOException {
- final ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark", int.class));
- final DogAlike auau = mockery.mock(DogAlike.class);
- mockery.checking(new Expectations() {
- {
- one(auau).bark(3);
- one(info).getParameters();
- will(returnValue(new Object[] { 3 }));
- one(stack).next(method, auau);
- one(info).setResult("ok");
- allowing(validator);
- }
- });
+ ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark", int.class));
+ DogAlike auau = mock(DogAlike.class);
+
+ when(info.getParameters()).thenReturn(new Object[] { 3 });
+
interceptor.intercept(stack, method, auau);
- mockery.assertIsSatisfied();
+
+ verify(auau).bark(3);
+ verify(stack).next(method, auau);
+ verify(info).setResult("ok");
}
public static class XController {
@@ -136,96 +124,74 @@ public void method() {
@Test
public void shouldSetResultReturnedValueFromInvokedMethod() throws SecurityException, NoSuchMethodException,
InterceptionException, IOException {
- final ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method",
- Object.class));
+ ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method", Object.class));
final XController x = new XController();
- mockery.checking(new Expectations() {
- {
- one(info).getParameters();
- will(returnValue(new Object[] { "string" }));
- one(stack).next(method, x);
- one(info).setResult("string");
- allowing(validator);
- }
- });
+
+ when(info.getParameters()).thenReturn(new Object[] { "string" });
+
interceptor.intercept(stack, method, x);
- mockery.assertIsSatisfied();
+
+ verify(stack).next(method, x);
+ verify(info).setResult("string");
}
@Test
public void shouldSetNullWhenNullReturnedFromInvokedMethod() throws SecurityException, NoSuchMethodException,
InterceptionException, IOException {
- final ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method",
- Object.class));
+ ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method", Object.class));
final XController x = new XController();
- mockery.checking(new Expectations() {
- {
- one(info).getParameters();
- will(returnValue(new Object[] { null }));
- one(stack).next(method, x);
- one(info).setResult(null);
- allowing(validator);
- }
- });
+
+ when(info.getParameters()).thenReturn(new Object[] { null });
+
interceptor.intercept(stack, method, x);
- mockery.assertIsSatisfied();
+
+ verify(stack).next(method, x);
+ verify(info).setResult(null);
}
@Test
public void shouldSetOkWhenVoidReturnedFromInvokedMethod() throws SecurityException, NoSuchMethodException,
InterceptionException, IOException {
- final ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method"));
- final XController x = new XController();
- mockery.checking(new Expectations() {
- {
- one(info).getParameters();
- will(returnValue(new Object[] {}));
- one(stack).next(method, x);
- one(info).setResult("ok");
- allowing(validator);
- }
- });
+ ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method"));
+ XController x = new XController();
+
+ when(info.getParameters()).thenReturn(new Object[] {});
+
interceptor.intercept(stack, method, x);
- mockery.assertIsSatisfied();
+
+ verify(stack).next(method, x);
+ verify(info).setResult("ok");
}
@Test
public void shouldBeOkIfThereIsValidationErrorsAndYouSpecifiedWhereToGo() throws SecurityException,
NoSuchMethodException, InterceptionException, IOException {
- final ResourceMethod method = mockery.methodFor(AnyController.class, "specifiedWhereToGo");
- final AnyController controller = new AnyController(validator);
- mockery.checking(new Expectations() {
- {
- one(info).getParameters();
- will(returnValue(new Object[0]));
- one(validator).onErrorUse(nothing());
- will(throwException(new ValidationException(Collections.<Message> emptyList())));
- allowing(validator).hasErrors();
- will(returnValue(true));
- }
- });
+ Method specifiedWhereToGo = AnyController.class.getMethod("specifiedWhereToGo");
+ ResourceMethod method = DefaultResourceMethod.instanceFor(AnyController.class, specifiedWhereToGo);
+ AnyController controller = new AnyController(validator);
+
+ when(info.getParameters()).thenReturn(new Object[0]);
+ doThrow(new ValidationException(Collections.<Message> emptyList())).when(validator).onErrorUse(nothing());
+ when(validator.hasErrors()).thenReturn(true);
+
interceptor.intercept(stack, method, controller);
- mockery.assertIsSatisfied();
}
@Test
public void shouldThrowExceptionIfYouHaventSpecifiedWhereToGoOnValidationError() throws SecurityException,
NoSuchMethodException, InterceptionException, IOException {
- final ResourceMethod method = mockery.methodFor(AnyController.class, "didntSpecifyWhereToGo");
+ Method didntSpecifyWhereToGo = AnyController.class.getMethod("didntSpecifyWhereToGo");
+ final ResourceMethod method = DefaultResourceMethod.instanceFor(AnyController.class, didntSpecifyWhereToGo);
final AnyController controller = new AnyController(validator);
- mockery.checking(new Expectations() {
- {
- one(info).getParameters();
- will(returnValue(new Object[0]));
- one(validator).hasErrors();
- will(returnValue(true));
- }
- });
+
+ when(info.getParameters()).thenReturn(new Object[0]);
+ when(validator.hasErrors()).thenReturn(true);
+
try {
interceptor.intercept(stack, method, controller);
Assert.fail();
} catch (InterceptionException e) {
- mockery.assertIsSatisfied();
+
}
}
51 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/ByteArrayDownloadTest.java
View
@@ -1,35 +1,33 @@
package br.com.caelum.vraptor.interceptor.download;
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
public class ByteArrayDownloadTest {
private byte[] bytes;
- private Mockery mockery;
- private HttpServletResponse response;
+ private @Mock HttpServletResponse response;
private ServletOutputStream socketStream;
private ByteArrayOutputStream outputStream;
@Before
public void setUp() throws Exception {
- this.mockery = new Mockery();
- this.response = mockery.mock(HttpServletResponse.class);
+ MockitoAnnotations.initMocks(this);
- this.bytes = new byte[10000];
- for (int i = 0; i < 10000; i++) {
- // just to have something not trivial
- bytes[i] = (byte) (i % 100);
- }
+ bytes = new byte[] { (byte) 0 };
this.outputStream = new ByteArrayOutputStream();
this.socketStream = new ServletOutputStream() {
@@ -38,44 +36,23 @@ public void write(int b) throws IOException {
}
};
+ when(response.getOutputStream()).thenReturn(socketStream);
}
@Test
public void shouldFlushWholeStreamToHttpResponse() throws IOException {
ByteArrayDownload fd = new ByteArrayDownload(bytes, "type", "x.txt");
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- ignoring(anything());
- }
- });
-
fd.write(response);
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+ assertArrayEquals(bytes, outputStream.toByteArray());
}
@Test
public void shouldUseHeadersToHttpResponse() throws IOException {
ByteArrayDownload fd = new ByteArrayDownload(bytes, "type", "x.txt");
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- one(response).setHeader("Content-type", "type");
-
- ignoring(anything());
- }
- });
-
fd.write(response);
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+ verify(response, times(1)).setHeader("Content-type", "type");
+ assertArrayEquals(bytes, outputStream.toByteArray());
}
-
}
22 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/FileDownloadTest.java
View
@@ -30,11 +30,7 @@
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- this.bytes = new byte[10000];
- for (int i = 0; i < 10000; i++) {
- // just to have something not trivial
- bytes[i] = (byte) (i % 100);
- }
+ bytes = new byte[] { (byte) 0 };
this.outputStream = new ByteArrayOutputStream();
this.file = File.createTempFile("test", "vraptor");
@@ -48,13 +44,12 @@ public void write(int b) throws IOException {
}
};
+ when(response.getOutputStream()).thenReturn(socketStream);
}
@Test
public void shouldFlushWholeFileToHttpResponse() throws IOException {
FileDownload fd = new FileDownload(file, "type");
-
- when(response.getOutputStream()).thenReturn(socketStream);
fd.write(response);
assertArrayEquals(bytes, outputStream.toByteArray());
@@ -63,11 +58,18 @@ public void shouldFlushWholeFileToHttpResponse() throws IOException {
@Test
public void shouldUseHeadersToHttpResponse() throws IOException {
FileDownload fd = new FileDownload(file, "type", "x.txt", false);
-
- when(response.getOutputStream()).thenReturn(socketStream);
fd.write(response);
+
verify(response, times(1)).setHeader("Content-type", "type");
-
+ verify(response, times(1)).setHeader("Content-disposition", "inline; filename=x.txt");
assertArrayEquals(bytes, outputStream.toByteArray());
}
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldThrowIllegalArgumentExceptionIfFileDoesntExists() throws Exception {
+ File file0 = new File("a.path.that.doesnot.exists");
+ FileDownload fd = new FileDownload(file0, "type", "x.txt", false);
+ fd.write(response);
+
+ }
}
53 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/InputStreamDownloadTest.java
View
@@ -1,5 +1,9 @@
package br.com.caelum.vraptor.interceptor.download;
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -8,31 +12,24 @@
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
public class InputStreamDownloadTest {
private InputStream inputStream;
private byte[] bytes;
- private Mockery mockery;
- private HttpServletResponse response;
+ private @Mock HttpServletResponse response;
private ServletOutputStream socketStream;
private ByteArrayOutputStream outputStream;
@Before
public void setUp() throws Exception {
- this.mockery = new Mockery();
- this.response = mockery.mock(HttpServletResponse.class);
-
- this.bytes = new byte[10000];
- for (int i = 0; i < 10000; i++) {
- // just to have something not trivial
- bytes[i] = (byte) (i % 100);
- }
+ MockitoAnnotations.initMocks(this);
+
+ bytes = new byte[] { (byte) 0 };
this.inputStream = new ByteArrayInputStream(bytes);
this.outputStream = new ByteArrayOutputStream();
@@ -42,44 +39,24 @@ public void write(int b) throws IOException {
}
};
+ when(response.getOutputStream()).thenReturn(socketStream);
}
@Test
public void shouldFlushWholeStreamToHttpResponse() throws IOException {
InputStreamDownload fd = new InputStreamDownload(inputStream, "type", "x.txt");
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- ignoring(anything());
- }
- });
-
fd.write(response);
-
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+
+ assertArrayEquals(bytes, outputStream.toByteArray());
}
@Test
public void shouldUseHeadersToHttpResponse() throws IOException {
InputStreamDownload fd = new InputStreamDownload(inputStream, "type", "x.txt");
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- one(response).setHeader("Content-type", "type");
-
- ignoring(anything());
- }
- });
-
fd.write(response);
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+ verify(response).setHeader("Content-type", "type");
+ assertArrayEquals(bytes, outputStream.toByteArray());
}
}
48 vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/download/JFreeChartDownloadTest.java
View
@@ -1,5 +1,9 @@
package br.com.caelum.vraptor.interceptor.download;
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -11,18 +15,16 @@
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
public class JFreeChartDownloadTest {
private JFreeChart chart;
private byte[] bytes;
- private Mockery mockery;
- private HttpServletResponse response;
+ private @Mock HttpServletResponse response;
private ServletOutputStream socketStream;
private ByteArrayOutputStream outputStream;
private int width;
@@ -30,19 +32,18 @@
@Before
public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
this.width = 400;
this.height = 300;
- this.mockery = new Mockery();
- this.response = mockery.mock(HttpServletResponse.class);
-
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (double i = 0.0; i < 10; i++) {
dataset.addValue(i, "A", new Double(i*0.7));
}
- this.chart = ChartFactory.createLineChart("Chart Test", "Axis X", "Axis Y", dataset , PlotOrientation.HORIZONTAL, false, false, false);
+ chart = ChartFactory.createLineChart("Chart Test", "Axis X", "Axis Y", dataset , PlotOrientation.HORIZONTAL, false, false, false);
bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
this.outputStream = new ByteArrayOutputStream();
@@ -52,44 +53,23 @@ public void write(int b) throws IOException {
}
};
+ when(response.getOutputStream()).thenReturn(socketStream);
}
@Test
public void shouldFlushWholeStreamToHttpResponse() throws IOException {
JFreeChartDownload chartDownload = new JFreeChartDownload(chart, width, height);
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- ignoring(anything());
- }
- });
-
chartDownload.write(response);
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+ assertArrayEquals(bytes, outputStream.toByteArray());
}
@Test
public void shouldUseHeadersToHttpResponse() throws IOException {
JFreeChartDownload chartDownload = new JFreeChartDownload(chart, width, height);
-
- mockery.checking(new Expectations() {
- {
- one(response).getOutputStream();
- will(returnValue(socketStream));
-
- one(response).setHeader("Content-type", "image/png");
-
- ignoring(anything());
- }
- });
-
chartDownload.write(response);
- Assert.assertArrayEquals(bytes, outputStream.toByteArray());
+ assertArrayEquals(bytes, outputStream.toByteArray());
+ verify(response).setHeader("Content-type", "image/png");
}
-
}
37 vraptor-core/src/test/java/br/com/caelum/vraptor/ioc/InterceptorStereotypeHandlerTest.java
View
@@ -16,10 +16,13 @@
*/
package br.com.caelum.vraptor.ioc;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.ComponentRegistry;
import br.com.caelum.vraptor.InterceptionException;
@@ -31,43 +34,31 @@
public class InterceptorStereotypeHandlerTest {
-
- private Mockery mockery;
- private InterceptorRegistry interceptorRegistry;
+ private @Mock InterceptorRegistry interceptorRegistry;
+ private @Mock ComponentRegistry componentRegistry;
private InterceptorStereotypeHandler handler;
- private ComponentRegistry componentRegistry;
@Before
public void setUp() throws Exception {
- mockery = new Mockery();
- interceptorRegistry = mockery.mock(InterceptorRegistry.class);
- componentRegistry = mockery.mock(ComponentRegistry.class);
+ MockitoAnnotations.initMocks(this);
handler = new InterceptorStereotypeHandler(interceptorRegistry, componentRegistry);
}
@SuppressWarnings("unchecked")
@Test
public void shouldRegisterInterceptorsOnRegistry() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(interceptorRegistry).register(InterceptorA.class);
- }
- });
handler.handle(InterceptorA.class);
- mockery.assertIsSatisfied();
+ verify(interceptorRegistry, times(1)).register(InterceptorA.class);
}
+
@SuppressWarnings("unchecked")
@Test
public void shouldRegisterInterceptorsFromInterceptorSequenceOnBothInterceptorAndComponentRegistrys() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(interceptorRegistry).register(InterceptorA.class, InterceptorB.class);
- one(componentRegistry).deepRegister(InterceptorA.class);
- one(componentRegistry).deepRegister(InterceptorB.class);
- }
- });
handler.handle(MySequence.class);
- mockery.assertIsSatisfied();
+
+ verify(interceptorRegistry).register(InterceptorA.class, InterceptorB.class);
+ verify(componentRegistry).deepRegister(InterceptorA.class);
+ verify(componentRegistry).deepRegister(InterceptorB.class);
}
public static class MySequence implements InterceptorSequence {
87 vraptor-core/src/test/java/br/com/caelum/vraptor/resource/HttpMethodTest.java
View
@@ -1,108 +1,69 @@
package br.com.caelum.vraptor.resource;
import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
import javax.servlet.http.HttpServletRequest;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
public class HttpMethodTest {
- private Mockery mockery;
- private HttpServletRequest request;
+ private @Mock HttpServletRequest request;
@Before
public void setup() {
- this.mockery = new Mockery();
- this.request = mockery.mock(HttpServletRequest.class);
+ MockitoAnnotations.initMocks(this);
}
@Test
public void shouldConvertGETStringToGetMethodForRequestParameter() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getParameter("_method");
- will(returnValue("gEt"));
-
- one(request).getMethod(); will(returnValue("POST"));
- }
- });
+ when(request.getParameter("_method")).thenReturn("gEt");
+ when(request.getMethod()).thenReturn("POST");
+
assertEquals(HttpMethod.GET, HttpMethod.of(request));
-
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNotKnowHttpMethodsForRequestParameter() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getParameter("_method");
- will(returnValue("JUMP!"));
-
- one(request).getMethod(); will(returnValue("POST"));
- }
- });
+ when(request.getParameter("_method")).thenReturn("JUMP!");
+ when(request.getMethod()).thenReturn("POST");
+
HttpMethod.of(request);
-
}
+
@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionIf_methodIsUsedInGETRequests() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getParameter("_method"); will(returnValue("DELETE"));
-
- one(request).getMethod(); will(returnValue("GET"));
- }
- });
+ when(request.getParameter("_method")).thenReturn("DELETE");
+ when(request.getMethod()).thenReturn("GET");
+
HttpMethod.of(request);
-
}
@Test
public void shouldConvertGETStringToGetMethod() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getParameter("_method");
- will(returnValue(null));
-
- one(request).getMethod();
- will(returnValue("gEt"));
- }
- });
+ when(request.getParameter("_method")).thenReturn(null);
+ when(request.getMethod()).thenReturn("gEt");
+
assertEquals(HttpMethod.GET, HttpMethod.of(request));
-
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNotKnowHttpMethods() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getParameter("_method");
- will(returnValue(null));
-
- one(request).getMethod();
- will(returnValue("JUMP!"));
- }
- });
+ when(request.getParameter("_method")).thenReturn(null);
+ when(request.getMethod()).thenReturn("JUMP!");
+
HttpMethod.of(request);
-
}
@Test
public void shouldUseParameterNameBeforeTryingHttpRealMethod() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getMethod();
- will(returnValue("dElEtE"));
-
- one(request).getParameter("_method");
- will(returnValue("post"));
- }
- });
+ when(request.getMethod()).thenReturn("dElEtE");
+ when(request.getParameter("_method")).thenReturn("post");
+
assertEquals(HttpMethod.POST, HttpMethod.of(request));
-
}
-
}
104 vraptor-core/src/test/java/br/com/caelum/vraptor/test/VRaptorMockery.java
View
@@ -70,17 +70,6 @@ public void checking(ExpectationBuilder expectations) {
return mockery.mock(typeToMock);
}
- public ResourceMethod method(final Method declaredMethod) {
- final ResourceMethod method = mockery.mock(ResourceMethod.class,"method" + declaredMethod.getName());
- checking(new Expectations() {
- {
- one(method).getMethod();
- will(returnValue(declaredMethod));
- }
- });
- return method;
- }
-
public <T> ResourceClass resource(final Class<T> type) {
final ResourceClass resource = mockery.mock(ResourceClass.class, "resource : " + type + (++count));
mockery.checking(new Expectations() {
@@ -92,103 +81,10 @@ public ResourceMethod method(final Method declaredMethod) {
return resource;
}
- public <T> Container container(final Class<T> type, final T object) {
- final Container container = mockery.mock(Container.class);
- mockery.checking(new Expectations() {
- {
- one(container).instanceFor(type);
- will(returnValue(object));
- }
- });
- return container;
- }
-
- public <T> ResourceMethod methodForResource(Class<T> type) {
- final ResourceClass resource = resource(type);
- final ResourceMethod method = mockery.mock(ResourceMethod.class);
- checking(new Expectations() {
- {
- one(method).getResource();
- will(returnValue(resource));
- }
- });
- return method;
- }
-
- public <T> ResourceMethod methodFor(final Class<T> type, final String methodName, final Class<?>... params)
- throws NoSuchMethodException {
- final ResourceClass resource = mockery.mock(ResourceClass.class, "resource" + type.getSimpleName());
- mockery.checking(new Expectations() {
- {
- allowing(resource).getType();
- will(returnValue(type));
- }
- });
- final ResourceMethod method = mockery.mock(ResourceMethod.class, "method" + methodName);
- checking(new Expectations() {
- {
- allowing(method).getResource();
- will(returnValue(resource));
- allowing(method).getMethod();
- will(returnValue(type.getDeclaredMethod(methodName, params)));
- }
- });
- return method;
- }
-
- public Localization localization() {
- final Localization loc = mockery.mock(Localization.class);
- final ResourceBundle bundle = ResourceBundle.getBundle("messages");
- mockery.checking(new Expectations() {
- {
- allowing(loc).getBundle();
- will(returnValue(bundle));
- }
- });
- return loc;
- }
-
- /**
- * Creates a "simplenode" from ognl which returns the desired toString.
- */
- public SimpleNode ognlNode(final String name) {
- // ognl design sucks when methods should return the interface types, not the implementation types
- return new SimpleNode(0) {
- private static final long serialVersionUID = 1L;
- @Override
- protected Object getValueBody(OgnlContext arg0, Object arg1) throws OgnlException {
- return null;
- }
- @Override
- public String toString() {
- return name;
- }
- };
-
- }
-
- public HttpServletRequest request() {
- return mockery.mock(HttpServletRequest.class);
- }
-
/**
* 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);
}
-
-// public ResourceBundle stubResourceBundle(final IogiParametersProviderTest iogiParametersProviderTest, final String key, final String value) {
-// return new ResourceBundle(){
-// @Override
-// protected Object handleGetObject(String key) {
-// return value;
-// }
-// @Override
-// public Enumeration<String> getKeys() {
-// return iogiParametersProviderTest.enumerationFor(key);
-// }
-// };
-// }
-
}
193 vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultRefererResultTest.java
View
@@ -2,11 +2,18 @@
import static br.com.caelum.vraptor.view.Results.logic;
import static br.com.caelum.vraptor.view.Results.page;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.Method;
-import org.jmock.Expectations;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.Localization;
@@ -14,183 +21,111 @@
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.http.route.ResourceNotFoundException;
import br.com.caelum.vraptor.http.route.Router;
+import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.HttpMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.test.VRaptorMockery;
public class DefaultRefererResultTest {
-
- private VRaptorMockery mockery;
- private Result result;
- private MutableRequest request;
- private Router router;
+ private @Mock Result result;
+ private @Mock MutableRequest request;
+ private @Mock Router router;
+ private @Mock Localization localization;
+ private @Mock ParametersProvider provider;
private DefaultRefererResult refererResult;
- private Localization localization;
- private ParametersProvider provider;
@Before
public void setUp() throws Exception {
- mockery = new VRaptorMockery();
- result = mockery.mock(Result.class);
- request = mockery.mock(MutableRequest.class);
- router = mockery.mock(Router.class);
- provider = mockery.mock(ParametersProvider.class);
- localization = mockery.mock(Localization.class);
+ MockitoAnnotations.initMocks(this);
refererResult = new DefaultRefererResult(result, request, router, provider, localization);
}
@Test
public void whenThereIsNoRefererShouldThrowExceptionOnForward() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getHeader("Referer");
- will(returnValue(null));
-
- ignoring(anything());
- }
- });
+ when(request.getHeader("Referer")).thenReturn(null);
+
try {
refererResult.forward();
- Assert.fail("Expected IllegalStateException");
+ fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
- mockery.assertIsSatisfied();
+
}
}
+
@Test
public void whenThereIsNoRefererShouldThrowExceptionOnRedirect() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getHeader("Referer");
- will(returnValue(null));
+ when(request.getHeader("Referer")).thenReturn(null);
- ignoring(anything());
- }
- });
try {
refererResult.redirect();
- Assert.fail("Expected IllegalStateException");
+ fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
- mockery.assertIsSatisfied();
+
}
}
+
@Test
public void whenRefererDontMatchAControllerShouldForwardToPage() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getHeader("Referer");
- will(returnValue("http://localhost:8080/vraptor/no-controller"));
-
- one(request).getContextPath();
- will(returnValue("/vraptor"));
-
- one(router).parse("/no-controller", HttpMethod.GET, request);
- will(throwException(new ResourceNotFoundException()));
-
- PageResult page = mockery.mock(PageResult.class);
-
- one(result).use(page());
- will(returnValue(page));
-
- one(page).forwardTo("/no-controller");
+ PageResult page = mock(PageResult.class);
+
+ when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller");
+ when(request.getContextPath()).thenReturn("/vraptor");
+ when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ResourceNotFoundException());
+ doReturn(page).when(result).use(page());
- ignoring(anything());
- }
- });
refererResult.forward();
-
- mockery.assertIsSatisfied();
+
+ verify(page).forwardTo("/no-controller");
}
+
@Test
public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception {
- mockery.checking(new Expectations() {
- {
- one(request).getHeader("Referer");
- will(returnValue("http://localhost:8080/vraptor/no-controller"));
-
- one(request).getContextPath();
- will(returnValue("/vraptor"));
-
- one(router).parse("/no-controller", HttpMethod.GET, request);
- will(throwException(new ResourceNotFoundException()));
-
- PageResult page = mockery.mock(PageResult.class);
-
- one(result).use(page());
- will(returnValue(page));
-
- one(page).redirectTo("/no-controller");
-
- ignoring(anything());
- }
- });
+ PageResult page = mock(PageResult.class);
+
+ when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller");
+ when(request.getContextPath()).thenReturn("/vraptor");
+ when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ResourceNotFoundException());
+ doReturn(page).when(result).use(page());
+
refererResult.redirect();
-
- mockery.assertIsSatisfied();
+
+ verify(page).redirectTo("/no-controller");
}
+
public static class RefererController {
public void index() {
}
}
+
@Test
public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception {