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

Flesh out JUnit #400

Merged
merged 4 commits into from
Jun 18, 2022
Merged
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
58 changes: 58 additions & 0 deletions content/languages/java/junit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,61 @@
title: JUnit
kind: reference
---

You can use JUnit [4](https://junit.org/junit4/) or [5](https://junit.org/junit5/) in Codewars. 5 offers nesting, pretty name display and multiple test classes.
ggorlen marked this conversation as resolved.
Show resolved Hide resolved

Note that JUnit assertions use `(expected, actual)` parameter ordering rather than the typical `(actual, expected)`.
ggorlen marked this conversation as resolved.
Show resolved Hide resolved

## Basic Setup

### Example solution

```java
public class Adder {
public static int add(int a, int b) {
ggorlen marked this conversation as resolved.
Show resolved Hide resolved
return a + b;
}
}
```

### JUnit 4

```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class AdderTests {
@Test
public void testAdd() {
assertEquals(3, Adder.add(1, 2));
}
}
```

### JUnit 5

```java
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("Testing Adder")
class AdderTests {
@Test
@DisplayName("Adder.add(1, 1) returns 2")
void testPositives() {
assertEquals(2, Adder.add(1, 1), "1 + 1 should equal 2");
}

@Nested
@DisplayName("Negative Integers")
class NegativeTests {
@Test
@DisplayName("Adder.add(-1, -1) returns -2")
void testNegatives() {
assertEquals(-2, Adder.add(-1, -1), "-1 + -1 should equal -2");
}
}
}
```