> Markdown version of [Unit Testing in Flow](https://vaadin.com/docs/v23/contributing/flow-unit-testing). Section index: [llms.txt](https://vaadin.com/docs/v23/contributing/llms.txt)

# Unit Testing in Flow

Every change in the code base requires a JUnit test for the code change. In cases where a JUnit test isn’t practicable, an [Integration Test](https://vaadin.com/docs/v23/contributing/flow-integration-testing.md) should be added instead.

## <a id="mocking"></a>Mocking

JUnit mocks use `Mockito`. Other mocking libraries shouldn’t be used, as they may break when there are version updates. No new mocking libraries should be added to the project.

To help with tests, there are many `Mock*` classes for use that make the setup for testing simpler.

## <a id="conventions"></a>Conventions

JUnit tests shouldn’t leak settings and changes outside the test execution. This means that any changes to current instances and system properties should be reset after the test execution.

Test method naming must follow the convention: `{given}_{when}_{then}`; for example:

Example standard names for unit test methods:

```
void setValue_sameValue_firesNoEvent()
void setValue_differentValue_firesOneEvent()
```

It’s always good practice to see existing tests as an example of how to write new tests.

## <a id="how-to-run-the-tests"></a>How to Run the Tests

Issuing the following command results in running all the tests in the specified module:

```terminal
mvn test -pl <module-name>
```

> **Tip: Tests in nested modules**
>
> The above command template works only for direct child modules of the directory. To run tests in nested modules, use the syntax `:<module-name>`, for example, `mvn test -pl :flow-maven-plugin`. Or use the full path instead: `mvn test -pl flow-plugins/flow-maven-plugin`. The same rule applies when targeting a specific nested module, as in the commands that follow.

To execute tests for a single class, use the following command:

```terminal
mvn -Dtest=<test-class-name> test -pl <module-folder-name>
```

Also, to run a single test inside a class:

```terminal
mvn -Dtest=<test-class-name>#<test-method-name> test -pl <module-folder-name>
```

To run all the unit tests in the project:

```terminal
mvn test -am -pl flow
```

To run tests via your IDE, see the IDE documentation.

> **Note: Chrome WebDriver is required**
>
> Running the unit tests in the `flow-client` module needs a Chrome WebDriver to be available on your environment, and its version should be configured in `flow-client/intern.json`. For more information on setting up the WebDriver, see [Installing WebDrivers](https://vaadin.com/docs/v23/testing/browser-based/installing-webdrivers.md).

`760C992B-3AFF-4F13-8C5A-6BF369C22668`
