Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST docs: How to migrate Rule to JUnit 5

I migrated my Spring tests to JUnit 5, and they work fine. However, I don't know how to migrate @Rule public JUnitRestDocumentation restDocumentation = .... Any hint is appreciated.

like image 916
Juergen Zimmermann Avatar asked Mar 11 '23 06:03

Juergen Zimmermann


2 Answers

Spring RestDocs 2 introduces a new class : RestDocumentationExtension for JUnit 5. You can use it instead of Rule

@ExtendWith(RestDocumentationExtension.class) public class JUnit5ExampleTests {

  • A complete example is available on https://github.com/spring-projects/spring-restdocs/blob/v2.0.0.RELEASE/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java
  • The extension is documented on https://github.com/spring-projects/spring-restdocs/blob/master/docs/src/docs/asciidoc/getting-started.adoc#setting-up-your-junit-5-tests

Spring RestDocs 2 requires Spring 5 and JDK 8

like image 64
florian negre Avatar answered Apr 01 '23 17:04

florian negre


Until the issue is officially resolved, I was able to get it working with a JUnit 5 extension (below).

Using that extension, I modified my test class thusly:

@ExtendWith(RestDocsExtension.class)

and

@BeforeEach
void setUp(WebApplicationContext wac, ManualRestDocumentation restDocumentation) throws Exception {

Here is the Extension.

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestExtensionContext;
import org.springframework.restdocs.ManualRestDocumentation;

import java.lang.reflect.Method;
import java.util.Optional;

public class RestDocsExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver {

    private static final String REST_DOC_STORE_KEY = "restDocumentation";

    private ManualRestDocumentation restDocumentation;

    @Override
    public void beforeAll(ContainerExtensionContext context) throws Exception {
        if (restDocumentation == null) {
            restDocumentation = new ManualRestDocumentation("target/generated-snippets");
            getStore(context).put(REST_DOC_STORE_KEY, restDocumentation);
        }
    }

    @Override
    public void beforeEach(TestExtensionContext context) throws Exception {
        Optional<Class<?>> testClass = context.getTestClass();
        Optional<Method> methodNameOpt = context.getTestMethod();
        if (testClass.isPresent() && methodNameOpt.isPresent()) {
            getDoc(context).beforeTest(testClass.get().getClass(), methodNameOpt.get().getName());
        } else {
            throw new Exception("TestExtensionContext with no class or method. wat");
        }
    }

    @Override
    public void afterEach(TestExtensionContext context) throws Exception {
        getDoc(context).afterTest();
    }

    @Override
    public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return parameterContext.getParameter().getType() == ManualRestDocumentation.class;
    }

    @Override
    public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return getDoc(extensionContext);
    }

    private ManualRestDocumentation getDoc(ExtensionContext context) {
        return (ManualRestDocumentation) getStore(context).get(REST_DOC_STORE_KEY);
    }

    private ExtensionContext.Store getStore(ExtensionContext context) {
        return context.getStore(ExtensionContext.Namespace.DEFAULT);
    }
}
like image 33
Eric J Turley Avatar answered Apr 01 '23 19:04

Eric J Turley