Skip to content

Testing

Alexander Yevsyukov edited this page Aug 20, 2024 · 4 revisions

Use Kotlin for writing tests

  • Backticked function names are faster to write than @DisplayName("blah-blah-blah") following void blahBlahBlah() in Java.
  • Infix assertions from Kotest are faster to write and read.

Tests already written in Java are gradually migrated to Kotlin.

We do use Java for tests when we need to test Java-specific API. For tests in Java we use Google Truth.

Test framework

For running tests we use JUnit 5.

We don't see significant benefit in migrating to Kotest Framework, while we have loads of work for improving Spine SDK.

If you do need Kotest as the framework, please weight out its benefits with the burden of having two testing frameworks in the Spine family of projects.

Naming test suite classes

Unit tests

Please use the pattern <SubjectClassName>Spec. We use the Spec suffix to highlight the fact that tests are real and actionable specification of the code. For example:

  • MethodPatternSpec
  • PipelineSpec.

Tests for Java API

If we have a test suite in Kotlin, but also want to test Java-specific API, the test suite class should be named after the pattern: <TestSubject>JavaSpec.

This way both Kotlin and Java classes come next to each other in alphabetical sorting.

Integration tests

Integration test classes should have the IgTest suffix, where Ig stands for "Integration". For example, if a tests runs code generation for rejection types, it should be named as RejectionCodegenIgTest.

Make the test suite class internal

If it's not an abstract base to be used from other modules, do make the test suite class internal.

Add @DisplayName("...") before the test suite

We use the annotation instead of a backticked class name for the following reasons:

  1. It's easier to search in IDE.
  2. Tests and production code come close in the alphabetical order.
  3. The value of annotation as a string gives you more options in writing, including backticking the name of the test subject.
  4. It's easier to reference a test suite in writing if it MyClassSpec rather than 'MyClass' should.

Backtick test subject name

It should look like this:

@DisplayName("`MyClass` should")
internal class MyClassSpec {
  // ...
}

Use special wrapping for nested classes

For the nested classes prefer backticked names over @DisplayName annotations.

When using backticked names please put them on a new line, like this:

@DisplayName("`Type` extensions should")
internal class TypeExtsSpec {

    @Nested inner class
    `Obtain simple type name` {
       // ...
    }
}

This way it's easier to read the goal of the nested class when scanning the code from top to bottom.

Start the text of the name with a capital letter to help distinguish nested classes from test functions in the list of test results.

Do you test fixtures

You are encouraged to use Gradle Test Fixtures plugin. It helps separating test as specifications from stub classes.

Clone this wiki locally