1

A set of tests should be run on every microservice. Current solution is to have an abstract class and extend in every service, providing the necessary properties in abstract getters.

public abstract class AbstractTest {

    @LocalServerPort
    protected int serverPort;

    protected abstract String getPath();
 
    @Test
    void someTest() {}

    @Test
    void conditionalTest() {}
}

@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
    classes = {...})
@ActiveProfiles(...) // etc
public class MyTest extends AbstractTest {
// ... implement getPath()
// tests from parent will be executed
}

The goal: Ditch inheritance and have the AbstractTest's logic get executed automatically with conditional @Test execution based on beans/properties etc.

The possible solution: A concrete class with all the tests or some sort of Configuration/TestFactory to create the necessary tests. It should take into account available properties and beans to determine which tests to run.

The problem: How can those tests (created in runtime) be discovered and registered for execution? How to inject all the properties that are part of the current context of the @SpringBootTest?

Failed attempts:

  • TestInstanceFactory extension doesn't seem to be the solution as it requires an instance of the class which it annotates.
  • Using the Launcher API seems overkill, and also doesn't seem to work, since the library class won't be created with the Spring context configs.
  • using cglib and a base class Spring Contract-style is not a desirable solution

Ideally I don't want the client of this lib to implement/create anything, so abstract String getPath(); would be a test.lib.path property, and if it's present, a test from the library which uses it will run.

Any thoughts on this would be great, because right now this just seems impossible to me.

dpozinen
  • 131
  • 7

1 Answers1

0

What is the reason to have the inheritance for tests? In case you need to share some common logic within the tests you may try JUnit features (custom rules/extensions), for example

  1. For junit < 5.x.x @Rule functionality https://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html https://stackoverflow.com/a/34608174/6916890
  2. For junit >= 5.x.x (jupiter) there is an extension API https://junit.org/junit5/docs/current/user-guide/#writing-tests-built-in-extensions-TempDirectory
lazylead
  • 1,453
  • 1
  • 14
  • 26
  • Technically, yes, I wan't to share some common logic, but those tests are basically one liners for health/sanity checks, swagger links etc. The point is to avoid repetition, so shared code can't be in a util class. Extensions provide functionality "around" the test, so to speak, I'm looking for a way to have the actual tests running. – dpozinen May 05 '22 at 17:33