Tests Everywhere - Java
Tags: test, java, junit5, mockito
Published: 2023-11-23 00:00:13

{% github rogervinas/tests-everywhere no-readme %}

[Java](https://openjdk.org/) testing [this simple Hello World](https://dev.to/rogervinas/tests-tests-everywhere-1b6h) with [JUnit5](https://junit.org/junit5/), [Mockito](https://site.mockito.org/) and [AssertJ](https://assertj.github.io/doc/)

- [Show me the code](#show-me-the-code)
  - [Implementation](#implementation)
  - [Test](#test)
- [Run this project using 🐳 docker](https://github.com/rogervinas/tests-everywhere/tree/main/java#run-this-project-using--docker)
- [Run this project locally](https://github.com/rogervinas/tests-everywhere/tree/main/java#run-this-project-locally)

## Show me the code

### Implementation

1. Create `HelloMessage` class in [HelloMessage.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/main/java/org/hello/HelloApp.java):

```java
public class HelloMessage {
  public String getText() {
    return "Hello World!";
  }
}
```

We can create it as a class as [Mockito](https://site.mockito.org/) is able to mock final classes.

2. Same way create `HelloConsole` class in [HelloConsole.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/main/java/org/hello/HelloConsole.java):

```java
public class HelloConsole {
  public void print(String text) {
    System.out.println(text);
  }
}
```

3. Create `HelloApp` class in [HelloApp.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/main/java/org/hello/HelloApp.java):

```java
public class HelloApp {

  private final HelloMessage message;
  private final HelloConsole console;

  public HelloApp(HelloMessage message, HelloConsole console) {
    this.message = message;
    this.console = console;
  }

  public void printHello() {
    console.print(message.getText());
  }
}
```

4. Create main class in [Main.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/main/java/org/hello/Main.java) that wraps it all together:

```java
public class Main {

  public static void main(String[] args) {
    var message = new HelloMessage();
    var console = new HelloConsole();
    var app = new HelloApp(message, console);
    app.printHello();
  }
}
```

### Test

Following [JUnit5 > Writing Tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests), [Mockito > Stubbing](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#stubbing) and [AssertJ > Core assertions guide](https://assertj.github.io/doc/#assertj-core-assertions-guide) guides ...

1. Test `HelloMessage` in [HelloMessageTest.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/test/java/org/hello/HelloMessageTest.java):

```java
@Test
void shouldReturnHelloWorld() {
  var message = new HelloMessage();
  assertThat(message.getText()).isEqualTo("Hello World!");
}
```

2. Test `HelloApp` in [HelloAppTest.java](https://github.com/rogervinas/tests-everywhere/blob/main/java/src/test/java/org/hello/HelloAppTest.java):

```java
@Test
void shouldPrintHelloMessage() {

  var messageText = "Hello Test!";

  // 2.1 Create a mock of HelloMessage
  var message = mock(HelloMessage.class);
  // 2.2 Return "Hello Test!" whenever getText() is called
  when(message.getText()).thenReturn(messageText);

  // 2.2 Create a mock of HelloConsole
  var console = mock(HelloConsole.class);

  // 2.3 Create a HelloApp, the one we want to test, passing the mocks
  var app = new HelloApp(message, console);
  // 2.4 Execute the method we want to test
  app.printHello();

  // 2.5 Verify HelloConsole mock print() method
  // has been called once with "Hello Test!"
  verify(console).print(messageText);
}
```

3. Test output should look like:

```
> Task :test

HelloAppTest > shouldPrintHelloMessage() PASSED

HelloMessageTest > shouldReturnHelloWorld() PASSED

BUILD SUCCESSFUL in 2s
```

Happy Testing! 💙