Skip to content

Instantly share code, notes, and snippets.

@nurkiewicz
Created September 25, 2010 17:17
Show Gist options
  • Save nurkiewicz/597066 to your computer and use it in GitHub Desktop.
Save nurkiewicz/597066 to your computer and use it in GitHub Desktop.
public class DefaultFooServiceTest {
private FooService fooService = new DefaultFooService();
@Rule
public ExpectedException exception = new ExpectedException();
@Test
public void shouldThrowNpeWhenNullName() throws Exception {
//given
String name = null;
//when
exception.expect(NullPointerException.class);
fooService.echo(name);
//then
}
}
@Test
public void shouldThrowNpeWhenNullName() throws Exception {
//given
String name = null;
//when
fooService.echo(name);
//then
exception.expect(NullPointerException.class);
}
@UnderTest
private FooService fooService = new DefaultFooService();
public class DefaultFooServiceTest {
@UnderTest
private FooService fooService = new DefaultFooService();
@Rule
public ExceptionAssert exception = new ExceptionAssert();
@Test
public void shouldReturnHelloString() throws Exception {
//given
String name = "Tomek";
//when
final String result = fooService.echo(name);
//then
assertEquals("Hello, Tomek!", result);
}
@Test
public void shouldThrowNpeWhenNullName() throws Exception {
//given
String name = null;
//when
fooService.echo(name);
//then
exception.expect(NullPointerException.class);
}
@Test
public void shouldThrowIllegalArgumentWhenNameJohn() throws Exception {
//given
String name = "John";
//when
fooService.echo(name);
//then
exception.expect(IllegalArgumentException.class)
.expectMessage("Name: 'John' is not allowed");
}
}
@Test
public void shouldThrowIllegalArgumentWhenNameJohn() throws Exception {
//given
String name = "John";
//when
fooService.echo(name);
//then
expect(IllegalArgumentException.class)
.withMessage("Name: 'John' is not allowed");
}
package com.blogspot.nurkiewicz.junit.exceptionassert;
public class ExceptionAssert implements MethodRule {
@Override
public Statement apply(Statement base, FrameworkMethod method, Object testCase) {
this.testCase = testCase;
return new ExceptionAssertStatement(base);
}
private class ExceptionAssertStatement extends Statement {
private final Statement base;
private Throwable exceptionThrownFromClassUnderTest;
private Field underTestField;
public ExceptionAssertStatement(Statement base) {
this.base = base;
underTestField = findClassUnderTestField(testCase);
}
@Override
public void evaluate() throws Throwable {
final Object originalClassUnderTest = wrapClassUnderTest(testCase);
try {
base.evaluate();
} finally {
setUnderTestField(originalClassUnderTest);
}
verifyException();
}
}
private Object wrapWithProxy(final Object classUnderTest) {
return Proxy.newProxyInstance(classUnderTest.getClass().getClassLoader(), new Class[]{underTestField.getType()}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(classUnderTest, args);
} catch (InvocationTargetException e) {
exceptionThrownFromClassUnderTest = e.getCause();
return null;
}
}
});
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UnderTest {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment