<commit_before>#JUnit-BDD

[![Build Status](https://lpandzic.ci.cloudbees.com/buildStatus/icon?job=junit-bdd)](https://lpandzic.ci.cloudbees.com/job/junit-bdd/)

JUnit-BDD is a JUnit [Rule][1] which provides a simple and fluent API that gives you a way of structuring your test code within when and then blocks used in Behavior-driven development.

This projects depends on Java 8 SE and [JUnit][2] (with transitive dependencies excluded).

For more information on the subject of  Behavior-driven development see the following links: [Introducing BDD][3], [GivenWhenThen article by <NAME>][4] or [Wikipedia article][5].

##Contents

1. [Features](#Features)
    * [Introduction](#Introduction)
    * [Return value assertion](#ReturnValueAssertion)
    * [Thrown exception assertion](#ThrownExceptiosAssertion)
    * [Checked exceptions assertion](#ThrownCheckedExceptiosAssertion)
    * [Assertion framework flexibility](#AssertionFrameworkFlexibility)
2. [Installation](#Installation)
3. [Changes](#Changes)
4. [License](#License)
5. [Credits](#Credits)

## <a name="Features"></a> Features

### <a name="Introduction"></a>Introduction

Testing with JUnit-BDD starts by defining a JUnit test class that contains following rule definition:

```java
import com.github.lpandzic.junit.bdd.Bdd;
import static com.github.lpandzic.junit.bdd.Bdd.when;

@Rule
public Bdd bdd = Bdd.initialized();
```

Note: in the following examples [Hamcrest][6] is used for assertions but you [are free to use any assertion framework you like](#AssertionFrameworkFlexibility).

### <a name="ReturnValueAssertion"></a>Return value assertion

For a given class `DeathStar` that contains method with signature `Target fireAt(Target target) throws TargetAlreadyDestroyedException` where `TargetAlreadyDestroyedException` is a checked exception, we can do the following value assertion:

```java
when(deathStar.fireAt(alderaan)).then(target -> {
    assertThat(target.isDestroyed(), is(true));
    assertThat(target, is(alderaan));
    assertThat(target, is(not(coruscant)));
});
```

### <a name="ThrownExceptiosAssertion"></a>Thrown exception assertion

In order to catch exception for an assertion we pass a lambda to the when block:

```java
when(deathStar.fireAt(alderaan));
when(() -> deathStar.fireAt(alderaan)).then(thrownException -> {
    assertThat(thrownException, is(instanceOf(TargetAlreadyDestroyedException.class)));
    assertThat(thrownException.getMessage(), is(equalTo("Cannot fire at a destroyed " + alderaan)));
});
```

### <a name="ThrownCheckedExceptiosAssertion"></a>Thrown checked exceptions assertion

If we decide to change the `fireAt` method so that it doesn't throw the `TargetAlreadyDestroyedException` the test mentioned in previous sub chapter will fail. Since `TargetAlreadyDestroyedException` is a checked exception we can use Generics to prevent that test from compiling and reduce the time required to detect the error!
We change `then` to `thenChecked` and use `isA` matcher:

```java
when(deathStar.fireAt(alderaan));
when(() -> deathStar.fireAt(alderaan)).thenChecked(thrownException -> {
    assertThat(thrownException, isA(TargetAlreadyDestroyedException.class));<commit_msg>- typos fix in README.md anchor links<commit_after>#JUnit-BDD

[![Build Status](https://lpandzic.ci.cloudbees.com/buildStatus/icon?job=junit-bdd)](https://lpandzic.ci.cloudbees.com/job/junit-bdd/)

JUnit-BDD is a JUnit [Rule][1] which provides a simple and fluent API that gives you a way of structuring your test code within when and then blocks used in Behavior-driven development.

This projects depends on Java 8 SE and [JUnit][2] (with transitive dependencies excluded).

For more information on the subject of  Behavior-driven development see the following links: [Introducing BDD][3], [GivenWhenThen article by <NAME>][4] or [Wikipedia article][5].

##Contents

1. [Features](#Features)
    * [Introduction](#Introduction)
    * [Return value assertion](#ReturnValueAssertion)
    * [Thrown exception assertion](#ThrownExceptionsAssertion)
    * [Checked exceptions assertion](#ThrownCheckedExceptionsAssertion)
    * [Assertion framework flexibility](#AssertionFrameworkFlexibility)
2. [Installation](#Installation)
3. [Changes](#Changes)
4. [License](#License)
5. [Credits](#Credits)

## <a name="Features"></a> Features

### <a name="Introduction"></a>Introduction

Testing with JUnit-BDD starts by defining a JUnit test class that contains following rule definition:

```java
import com.github.lpandzic.junit.bdd.Bdd;
import static com.github.lpandzic.junit.bdd.Bdd.when;

@Rule
public Bdd bdd = Bdd.initialized();
```

Note: in the following examples [Hamcrest][6] is used for assertions but you [are free to use any assertion framework you like](#AssertionFrameworkFlexibility).

### <a name="ReturnValueAssertion"></a>Return value assertion

For a given class `DeathStar` that contains method with signature `Target fireAt(Target target) throws TargetAlreadyDestroyedException` where `TargetAlreadyDestroyedException` is a checked exception, we can do the following value assertion:

```java
when(deathStar.fireAt(alderaan)).then(target -> {
    assertThat(target.isDestroyed(), is(true));
    assertThat(target, is(alderaan));
    assertThat(target, is(not(coruscant)));
});
```

### <a name="ThrownExceptionsAssertion"></a>Thrown exception assertion

In order to catch exception for an assertion we pass a lambda to the when block:

```java
when(deathStar.fireAt(alderaan));
when(() -> deathStar.fireAt(alderaan)).then(thrownException -> {
    assertThat(thrownException, is(instanceOf(TargetAlreadyDestroyedException.class)));
    assertThat(thrownException.getMessage(), is(equalTo("Cannot fire at a destroyed " + alderaan)));
});
```

### <a name="ThrownCheckedExceptionsAssertion"></a>Thrown checked exceptions assertion

If we decide to change the `fireAt` method so that it doesn't throw the `TargetAlreadyDestroyedException` the test mentioned in previous sub chapter will fail. Since `TargetAlreadyDestroyedException` is a checked exception we can use Generics to prevent that test from compiling and reduce the time required to detect the error!
We change `then` to `thenChecked` and use `isA` matcher:

```java
when(deathStar.fireAt(alderaan));
when(() -> deathStar.fireAt(alderaan)).thenChecked(thrownException -> {
    assertThat(thrownException, isA(TargetAlreadyDestroyedException.class));
{'fraction_non_alphanumeric': 0.11638065522620905, 'fraction_numerical': 0.00374414976599064, 'mean_word_length': 6.147157190635451, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1577648', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1795, 'n_words': 579}