Permalink
Browse files

Jmock to Mockito

  • Loading branch information...
1 parent f2d32f9 commit c120914e50a13225d888a27e9bd9c23b8deefcff @garcia-jj garcia-jj committed Sep 10, 2012
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
@@ -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);
}
-
}
@@ -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,48 +63,35 @@ 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);
}
@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);
}
-
}
@@ -30,17 +30,18 @@
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;
import java.util.Locale;
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))));
}
Oops, something went wrong.

0 comments on commit c120914

Please sign in to comment.