All Projects → Codearte → Catch Exception

Codearte / Catch Exception

Licence: apache-2.0

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Catch Exception

Expekt
BDD assertion library for Kotlin
Stars: ✭ 163 (+18.98%)
Mutual labels:  bdd, junit, assertions
justtestlah
Dynamic test framework for web and mobile applications
Stars: ✭ 43 (-68.61%)
Mutual labels:  bdd, junit
zerocode-hello-world
Zerocode YAML and JSON based declarative steps hello world rest api testing example - soap, database
Stars: ✭ 18 (-86.86%)
Mutual labels:  assertions, junit
Scott
Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java
Stars: ✭ 125 (-8.76%)
Mutual labels:  junit, assertions
Should.js
BDD style assertions for node.js -- test framework agnostic
Stars: ✭ 1,908 (+1292.7%)
Mutual labels:  bdd, assertions
Pester
Pester is the ubiquitous test and mock framework for PowerShell.
Stars: ✭ 2,620 (+1812.41%)
Mutual labels:  bdd, assertions
ginkgo4j
A Java BDD Testing Framework (based on RSpec and Ginkgo)
Stars: ✭ 25 (-81.75%)
Mutual labels:  bdd, junit
karate
Test Automation Made Simple
Stars: ✭ 6,384 (+4559.85%)
Mutual labels:  bdd, assertions
bdd
JUnit 5 based BDD library to create and run stories and behaviors a.k.a BDD specification tests
Stars: ✭ 25 (-81.75%)
Mutual labels:  bdd, junit
Expect More
Curried Type Testing library, and Test Matchers for Jest
Stars: ✭ 124 (-9.49%)
Mutual labels:  bdd, assertions
Karate
Test Automation Made Simple
Stars: ✭ 5,497 (+3912.41%)
Mutual labels:  assertions, bdd
Spectrum
A BDD-style test runner for Java 8. Inspired by Jasmine, RSpec, and Cucumber.
Stars: ✭ 142 (+3.65%)
Mutual labels:  bdd, junit
Verify
BDD Assertions for PHPUnit and Codeception
Stars: ✭ 127 (-7.3%)
Mutual labels:  bdd, assertions
chai
BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
Stars: ✭ 7,842 (+5624.09%)
Mutual labels:  bdd, assertions
Postman Bdd
A BDD test framework for Postman and Newman
Stars: ✭ 139 (+1.46%)
Mutual labels:  bdd, assertions
codeceptjs-bdd
Javascript BDD UI Automation Framework. Exclusive LWC Shadow DOM Support. Playwright, Webdriver.io, Appium, Saucelabs.
Stars: ✭ 35 (-74.45%)
Mutual labels:  bdd, assertions
Should Enzyme
Useful functions for testing React Components with Enzyme.
Stars: ✭ 41 (-70.07%)
Mutual labels:  bdd, assertions
Grappa
Behavior-oriented, expressive, human-friendly Python assertion library for the 21st century
Stars: ✭ 119 (-13.14%)
Mutual labels:  bdd, assertions
Test Smells
Test Smells for Android developers
Stars: ✭ 120 (-12.41%)
Mutual labels:  junit
Ensure
Validate conditions, Python style.
Stars: ✭ 130 (-5.11%)
Mutual labels:  bdd

catch-exception 2

Catch and verify exceptions in a single line of code

Build Status Coverage Status Maven Central Apache 2

This is continuation of famous catch-exception library created by Rod Woo.

This library catches exceptions in a single line of code and makes them available for further analysis.

Usage

The most basic usage is:

import static com.googlecode.catchexception.CatchException.*;

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

// when: we try to get the first element of the list
// then: catch the exception if any is thrown
catchException(() -> myList.get(1));

// then: we expect an IndexOutOfBoundsException
assert caughtException() instanceof IndexOutOfBoundsException;

The last both lines of code can be combined in a single line of code if you like:

verifyException(myList, IndexOutOfBoundsException.class).get(1);

More information about the usage you find in the Javadoc documentation.

BDD-like

If you prefer a BDD-like approach, you can use BDDCatchException for another code style:

import static com.googlecode.catchexception.apis.BDDCatchException.*;

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

// 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();

The assertions used here are part of AssertJ.

Hamcrest

If you prefer Hamcrest matchers to express assertions, you can use CatchExceptionHamcrestMatchers with the following code style:

import static com.googlecode.catchexception.CatchException.*;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;

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

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

// then: we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
assertThat(caughtException(),
    allOf(
        instanceOf(equalTo(IndexOutOfBoundsException.class)),
        CatchExceptionHamcrestMatchers.hasMessage("Index: 1, Size: 0"),
        CatchExceptionHamcrestMatchers.hasNoCause()
    )
);

Catch constructor exceptions

Catch-exception supports exceptions that are thrown by constructors.

verifyException(() -> new Thing("baddata"));

Thanks to the community for the example.

Catch throwables

If you want to catch both throwables and exceptions have a look at the catch-throwable packages in javadoc. They provide the same API as the catch-exception packages but they belong to a different maven module.

JUnit4

If you want to handle expected exceptions, the documentation of catch-exception names some general reasons to prefer catch-exception in comparison to mechanisms that are provided by testing frameworks.

But some reasons that are specific to JUnit4 are outlined only here.

Collecting errors

If you want to combine the JUnit4's rules ExpectedException and ErrorCollector you will find out: this won't work.

Catch-exception instead can be easily combined with the error collecting rule:

@Rule
public ErrorCollector collector = new ErrorCollector();

@Test
public void testErrorCollectorWithExpectedException() {

    // collect first error
    collector.checkThat("a", equalTo("b"));

    // collect second error
    catchException(() -> new ArrayList().get(1));
    collector.checkThat(caughtException(), instanceOf(IllegalArgumentException.class));

    // collect third error
    collector.checkThat(1, equalTo(2));
}

Theories respectively parameterized tests

Sometimes you want to test for an optional exception in a parameterized test. JUnit4's ExpecteException rule does not help in this case. This is another use case where catch-exception comes in quite handy.

Download

Go to the Installation page to get the latest release. This page provides also the Maven coordinates, prerequisites, and information about dependencies to other libraries.

Credits

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

Thanks to Szczepan Faber who made some suggestions about a BDD-like syntax for catch-exception. Finally, his ideas triggered the enhancements that came with the 1.0.4 release.

Questions, Suggestions, Issues

Questions, suggestions and Issues are welcome and can be reported via Issues page of this project.

Please give me feedback of any kind. It is highly appreciated.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].