> For the complete documentation index, see [llms.txt](https://bernardo.gitbook.io/development-docs-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bernardo.gitbook.io/development-docs-java/testing/junit/testing-exceptions.md).

# Testing Exceptions

## JUnit 4

There are two options to test an exception with JUnit 4.

### Annotation

```java
@Test(expected = Exception.class)
public final void testWithException() {
   service.throwsException();
}
```

### Rule

```java
@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public final void testWithException() {
   expectedEx.expect(Exception.class);
   expectedEx.expectMessage("error.message");

   service.throwsException();
}
```

## JUnit 5

### Thrown Exception

```java
@Test
public final void testWithException() {
   Assertions.assertThrows(
      Exception.class,
      service::throwsException);
}
```

### Exception Content

```java
@Test
public final void testWithException() {
   final ExceptionWithCode ex;

   ex = Assertions.assertThrows(ExceptionWithCode .class, service::throwsException);
   
   Assertions.assertEquals(1, ex.getCode());
}
```
