Skip to content

Commit

Permalink
Jmock to Mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
garcia-jj committed Sep 20, 2012
1 parent f2d32f9 commit c120914
Show file tree
Hide file tree
Showing 21 changed files with 445 additions and 879 deletions.
137 changes: 56 additions & 81 deletions vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -190,23 +171,17 @@ public <T> boolean canProvide(Class<T> type) {

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -56,48 +63,35 @@ public static class Dependency {

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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))));
}
Expand Down
Loading

0 comments on commit c120914

Please sign in to comment.