Created
September 25, 2010 17:17
-
-
Save nurkiewicz/597066 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldThrowNpeWhenNullName() throws Exception { | |
//given | |
String name = null; | |
//when | |
fooService.echo(name); | |
//then | |
exception.expect(NullPointerException.class); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@UnderTest | |
private FooService fooService = new DefaultFooService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldThrowIllegalArgumentWhenNameJohn() throws Exception { | |
//given | |
String name = "John"; | |
//when | |
fooService.echo(name); | |
//then | |
expect(IllegalArgumentException.class) | |
.withMessage("Name: 'John' is not allowed"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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