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

BDD Api Improvment #27

Merged
merged 3 commits into from
Apr 5, 2015
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ More information about the usage you find in the [Javadoc documentation](http://
If you prefer a BDD-like approach, you can use [BDDCatchException](http://codearte.github.io/catch-exception/apidocs/com/googlecode/catchexception/apis/CatchExceptionBdd.html) for another code style:

```java
import static com.googlecode.catchexception.CatchException.*;
import static com.googlecode.catchexception.apis.BDDCatchException.*;

// given: an empty list
Expand Down Expand Up @@ -133,12 +132,14 @@ Sometimes you want to test for an [optional exception in a parameterized test](h
Go to the [Installation page](https://github.com/Codearte/catch-exception/wiki/Installation) to get the latest release. This page provides also the Maven coordinates, prerequisites, and information about dependencies to other libraries.

# Future enhancements
This is maintenance project only - new features are not planned.
<del>This is maintenance project only - new features are not planned.</del>

Read about catching exception with Java 8:
* [Clean JUnit Throwable-Tests with Java 8 Lambdas](http://www.codeaffine.com/2014/07/28/clean-junit-throwable-tests-with-java-8-lambdas/)
* [Java 8 Friday: Better Exceptions](http://blog.jooq.org/2014/05/23/java-8-friday-better-exceptions/)

We have plan to release Catch-Exception 2 for Java 8 only with breaking API change.

# Credits
Thanks to Rod Woo, the former author of catch-exception for creating this awesome library.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public class CatchException {
* caught exception belongs to a class that is no longer
* {@link ClassLoader loaded}.
*/
public static Exception caughtException() {
public static <E extends Exception> E caughtException() {
return ExceptionHolder.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,35 @@
*/
package com.googlecode.catchexception.apis;

import com.googlecode.catchexception.CatchException;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.CompatibilityAssertions;

import com.googlecode.catchexception.CatchException;
import com.googlecode.catchexception.internal.ExceptionHolder;

/**
* Supports <a
* href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>-like
* approach to catch and verify exceptions (<i>given/when/then</i>).
* <p>
* EXAMPLE:
* <code><pre class="prettyprint lang-java">import static org.assertj.core.api.BDDAssertions.then;
* <code><pre class="prettyprint lang-java">import static com.googlecode.catchexception.api.BDDCatchException.*;

// given an empty list
List myList = new ArrayList();
// given an empty list
List myList = new ArrayList();

// when we try to get the first element of the list
when(myList).get(1);
// when we try to get the first element of the list
when(myList).get(1);

// then we expect an IndexOutOfBoundsException
then(caughtException())
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessage("Index: 1, Size: 0")
.hasNoCause();
// then we expect an IndexOutOfBoundsException
then(caughtException())
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessage("Index: 1, Size: 0")
.hasNoCause();

// then we expect an IndexOutOfBoundsException (alternatively)
thenThrown(IndexOutOfBoundsException.class);
</pre></code>
// then we expect an IndexOutOfBoundsException (alternatively)
thenThrown(IndexOutOfBoundsException.class);
</pre></code>
*
* @author rwoo
* @author mariuszs
Expand Down Expand Up @@ -71,24 +73,24 @@ public static <T> T when(T obj) {
* <p>
* EXAMPLE:
* <code><pre class="prettyprint lang-java">// given a list with nine members
List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

// when we try to get the 500th member of the fellowship
when(myList).get(500);
// when we try to get the 500th member of the fellowship
when(myList).get(500);

// then we expect an IndexOutOfBoundsException
thenThrown(IndexOutOfBoundsException.class);
</pre></code>
// then we expect an IndexOutOfBoundsException
thenThrown(IndexOutOfBoundsException.class);
</pre></code>
*
* @param actualExceptionClazz
* the expected type of the caught exception.
*/
@SuppressWarnings("rawtypes")
public static void thenThrown(Class actualExceptionClazz) {
CatchExceptionUtils.thenThrown(actualExceptionClazz);
CatchExceptionUtils.thenThrown(actualExceptionClazz);
}

/**
/**
* Enables <a
* href="https://github.com/joel-costigliola/assertj-core">AssertJ</a>
* assertions about the caught exception.
Expand All @@ -110,7 +112,7 @@ public static void thenThrown(Class actualExceptionClazz) {
.hasNoCause();
</pre></code>
*
* @deprecated Use BDDAssertions#then instead
* @deprecated Use {@link #then(CaughtException)} instead
* @param actualException
* the value to be the target of the assertions methods.
* @return Returns the created assertion object.
Expand All @@ -121,4 +123,52 @@ public static void thenThrown(Class actualExceptionClazz) {
return CompatibilityAssertions.assertThat(actualException);
}

/**
* Enables <a
* href="https://github.com/joel-costigliola/assertj-core">AssertJ</a>
* assertions about the caught exception.
* <p>
* EXAMPLE:
* <code><pre class="prettyprint lang-java">// given an empty list
List myList = new ArrayList();

// when we try to get first element of the list
when(myList).get(1);

// then we expect an IndexOutOfBoundsException
then(caughtException())
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessage("Index: 1, Size: 0")
.hasMessageStartingWith("Index: 1")
.hasMessageEndingWith("Size: 0")
.hasMessageContaining("Size")
.hasNoCause();
</pre></code>
*
* @param actualException
* the value to be the target of the assertions methods.
* @return Returns the created assertion object.
*/
public static CatchExceptionAssert then(CaughtException actualException) {
return new CatchExceptionAssert((Exception) actualException.getCause());
}

/**
* Returns the exception caught during the last call on the proxied object
* in the current thread boxed inside {@link CaughtException}.
*
* @return Returns the exception caught during the last call on the proxied
* object in the current thread - if the call was made through a
* proxy that has been created via {@link #when(Object)}.
* Returns null if the proxy has not caught an exception. Returns null
* if the caught exception belongs to a class that is no longer
* {@link ClassLoader loaded}.
*/
public static CaughtException caughtException() {
return new CaughtException(ExceptionHolder.get());
}

public static CatchExceptionAssert thenCaughtException() {
return new CatchExceptionAssert(CatchException.caughtException());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2011 rwoo@gmx.de
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.catchexception.apis;

import org.assertj.core.api.AbstractThrowableAssert;

public class CatchExceptionAssert extends AbstractThrowableAssert<CatchExceptionAssert, Exception> {

protected CatchExceptionAssert(Exception actual) {
super(actual, CatchExceptionAssert.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2011 rwoo@gmx.de
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.catchexception.apis;

class CaughtException extends Exception {

public CaughtException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package com.googlecode.catchexception.apis;

import org.junit.Test;
import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
import static com.googlecode.catchexception.apis.BDDCatchException.then;
import static com.googlecode.catchexception.apis.BDDCatchException.thenThrown;
import static com.googlecode.catchexception.apis.BDDCatchException.when;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.apis.BDDCatchException.thenThrown;
import static com.googlecode.catchexception.apis.BDDCatchException.when;
import static com.googlecode.catchexception.apis.BDDCatchException.then;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* Tests {@link com.googlecode.catchexception.apis.BDDCatchException}.
Expand All @@ -37,7 +37,7 @@ public class BDDCatchExceptionTest {

@SuppressWarnings("rawtypes")
@Test
public void testAssertThat() {
public void testThen() {
// given an empty list
List myList = new ArrayList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.googlecode.catchexception.apis;

import org.junit.Test;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.caughtException;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.then;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.thenCaughtException;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.when;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.then;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.when;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* Tests {@link com.googlecode.catchexception.apis.CatchExceptionAssertJ}.
Expand All @@ -50,6 +51,14 @@ public void testThen() {
.hasMessageContaining("Size") //
.hasNoCause();

thenCaughtException() //
.isInstanceOf(IndexOutOfBoundsException.class) //
.hasMessage("Index: 1, Size: 0") //
.hasMessageStartingWith("Index: 1") //
.hasMessageEndingWith("Size: 0") //
.hasMessageContaining("Size") //
.hasNoCause();

// test: caughtException() == new RuntimeException()
try {
then(new RuntimeException()).isInstanceOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CatchThrowable {
* not caught an throwable. Returns null if the caught throwable belongs to a class that is no longer
* {@link ClassLoader loaded}.
*/
public static Throwable caughtThrowable() {
public static <E extends Throwable> E caughtThrowable() {
return ThrowableHolder.get();
}

Expand Down
Loading