Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using JUnit rules to test exceptions #814

Merged
merged 7 commits into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected void processFile(FileItem item, String name, MutableRequest request) {

logger.debug("Uploaded file: {} with {}", name, upload);
} catch (IOException e) {
throw new InvalidParameterException("Cant parse uploaded file " + item.getName(), e);
throw new InvalidParameterException("Can't parse uploaded file " + item.getName(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import br.com.caelum.vraptor.converter.ConversionException;
import br.com.caelum.vraptor.converter.ConversionMessage;
import br.com.caelum.vraptor.http.route.Route;
import br.com.caelum.vraptor.validator.Message;

Expand Down Expand Up @@ -92,4 +94,25 @@ protected boolean matchesSafely(Message message) {
}
};
}

public static TypeSafeMatcher<Exception> hasConversionException(String message) {
final Matcher<? extends String> delegate = equalTo(message);
return new TypeSafeMatcher<Exception>() {
@Override
protected boolean matchesSafely(Exception item) {
if (item instanceof ConversionException) {
ConversionMessage message = ((ConversionException) item).getValidationMessage();
message.setBundle(ResourceBundle.getBundle("messages"));
return delegate.matches(message.getMessage());
}

return false;
}

@Override
public void describeTo(Description description) {
delegate.describeTo(description);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

@RunWith(WeldJunitRunner.class)
public class VRaptorTest {

@Rule
public ExpectedException exception = ExpectedException.none();

@Inject private VRaptor vRaptor;
@Inject private MockStaticContentHandler handler;

@Test(expected = ServletException.class)
@Test
public void shoudlComplainIfNotInAServletEnviroment() throws Exception {
exception.expect(ServletException.class);

ServletRequest request = mock(ServletRequest.class);
ServletResponse response = mock(ServletResponse.class);
vRaptor.doFilter(request, response, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import javax.servlet.ServletException;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

Expand All @@ -38,6 +40,9 @@

public class ControllerNotFoundHandlerTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private ControllerNotFoundHandler notFoundHandler;
private @Mock MutableRequest webRequest;
private @Mock MutableResponse webResponse;
Expand All @@ -56,16 +61,17 @@ public void couldntFindDefersRequestToContainer() throws Exception {
verify(chain, only()).doFilter(webRequest, webResponse);
}

@Test(expected=InterceptionException.class)
@Test
public void shouldThrowInterceptionExceptionIfIOExceptionOccurs() throws Exception {
exception.expect(InterceptionException.class);
doThrow(new IOException()).when(chain).doFilter(webRequest, webResponse);
notFoundHandler.couldntFind(chain, webRequest, webResponse);
}

@Test(expected=InterceptionException.class)
@Test
public void shouldThrowInterceptionExceptionIfServletExceptionOccurs() throws Exception {
exception.expect(InterceptionException.class);
doThrow(new ServletException()).when(chain).doFilter(webRequest, webResponse);
notFoundHandler.couldntFind(chain, webRequest, webResponse);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.MutableResponse;

public class DefaultMethodNotAllowedHandlerTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private DefaultMethodNotAllowedHandler handler;
private MutableResponse response;
private MutableRequest request;
Expand Down Expand Up @@ -71,10 +76,11 @@ public void shouldNotSendMethodNotAllowedIfTheRequestMethodIsOptions() throws Ex
verify(response, never()).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

@Test(expected=InterceptionException.class)
@Test
public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Exception {
doThrow(new IOException()).when(response).sendError(anyInt());
exception.expect(InterceptionException.class);

doThrow(new IOException()).when(response).sendError(anyInt());
this.handler.deny(request, response, EnumSet.of(HttpMethod.GET, HttpMethod.POST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
*/
package br.com.caelum.vraptor.controller;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;


public class HttpMethodTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private @Mock HttpServletRequest request;

@Before
Expand All @@ -39,20 +45,25 @@ public void setup() {
public void shouldConvertGETStringToGetMethodForRequestParameter() throws Exception {
when(request.getParameter("_method")).thenReturn("gEt");
when(request.getMethod()).thenReturn("POST");

assertEquals(HttpMethod.GET, HttpMethod.of(request));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void shouldThrowExceptionForNotKnowHttpMethodsForRequestParameter() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(containsString("HTTP Method not known"));

when(request.getParameter("_method")).thenReturn("JUMP!");
when(request.getMethod()).thenReturn("POST");

HttpMethod.of(request);
}
@Test(expected = InvalidInputException.class)

@Test
public void shouldThrowInvalidInputExceptionIf_methodIsUsedInGETRequests() throws Exception {
exception.expect(InvalidInputException.class);

when(request.getParameter("_method")).thenReturn("DELETE");
when(request.getMethod()).thenReturn("GET");
HttpMethod.of(request);
Expand All @@ -66,8 +77,11 @@ public void shouldConvertGETStringToGetMethod() throws Exception {
assertEquals(HttpMethod.GET, HttpMethod.of(request));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void shouldThrowExceptionForNotKnowHttpMethods() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(containsString("HTTP Method not known"));

when(request.getParameter("_method")).thenReturn(null);
when(request.getMethod()).thenReturn("JUMP!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

package br.com.caelum.vraptor.converter;

import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage;
import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
Expand All @@ -32,17 +31,18 @@
import javax.servlet.http.HttpSession;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import br.com.caelum.vraptor.converter.ConversionException;
import br.com.caelum.vraptor.converter.BigDecimalConverter;
import br.com.caelum.vraptor.http.MutableRequest;

public class BigDecimalConverterTest {

static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale";
@Rule
public ExpectedException exception = ExpectedException.none();

private BigDecimalConverter converter;
private @Mock MutableRequest request;
Expand All @@ -52,7 +52,6 @@ public class BigDecimalConverterTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);

when(request.getServletContext()).thenReturn(context);

converter = new BigDecimalConverter(new Locale("pt", "BR"));
Expand Down Expand Up @@ -83,11 +82,8 @@ public void shouldBeAbleToConvertNull() {

@Test
public void shouldThrowExceptionWhenUnableToParse() {
try {
converter.convert("vr3.9", BigDecimal.class);
fail("Should throw exception");
} catch (ConversionException e) {
assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number."));
}
exception.expect(hasConversionException("vr3.9 is not a valid number."));

converter.convert("vr3.9", BigDecimal.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

package br.com.caelum.vraptor.converter;

import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage;
import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.fail;

import java.math.BigInteger;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.junit.rules.ExpectedException;

/**
* VRaptor's BigInteger converter test.
Expand All @@ -37,6 +37,9 @@
*/
public class BigIntegerConverterTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private BigIntegerConverter converter;

@Before
Expand All @@ -51,21 +54,14 @@ public void shouldBeAbleToConvertIntegerNumbers() {

@Test
public void shouldComplainAboutNonIntegerNumbers() {
try {
converter.convert("2.3", BigInteger.class);
} catch (ConversionException e) {
assertThat(e.getValidationMessage(), hasMessage("2.3 is not a valid number."));
}
exception.expect(hasConversionException("2.3 is not a valid number."));
converter.convert("2.3", BigInteger.class);
}

@Test
public void shouldComplainAboutInvalidNumber() {
try {
converter.convert("---", BigInteger.class);
fail("should throw an exception");
} catch (ConversionException e) {
assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number."));
}
exception.expect(hasConversionException("--- is not a valid number."));
converter.convert("---", BigInteger.class);
}

@Test
Expand All @@ -77,5 +73,4 @@ public void shouldNotComplainAboutNull() {
public void shouldNotComplainAboutEmpty() {
assertThat(converter.convert("", BigInteger.class), is(nullValue()));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@

package br.com.caelum.vraptor.converter;

import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage;
import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.junit.rules.ExpectedException;

public class BooleanConverterTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private BooleanConverter converter;

@Before
Expand Down Expand Up @@ -58,11 +61,13 @@ public void shouldConvertYesNo() {
assertThat(converter.convert("yes", Boolean.class), is(equalTo(true)));
assertThat(converter.convert("no", Boolean.class), is(equalTo(false)));
}

@Test
public void shouldConvertYN() {
assertThat(converter.convert("y", Boolean.class), is(equalTo(true)));
assertThat(converter.convert("n", Boolean.class), is(equalTo(false)));
}

@Test
public void shouldConvertOnOff() {
assertThat(converter.convert("on", Boolean.class), is(equalTo(true)));
Expand All @@ -79,11 +84,7 @@ public void shouldConvertIgnoringCase() {

@Test
public void shouldThrowExceptionForInvalidString() {
try {
converter.convert("not a boolean!", Boolean.class);
fail("should throw an exception");
} catch(ConversionException e) {
assertThat(e.getValidationMessage(), hasMessage("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off"));
}
exception.expect(hasConversionException("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off"));
converter.convert("not a boolean!", Boolean.class);
}
}
}
Loading