Spring REST docs: How to migrate Rule to JUnit 5
Asked Answered
P

2

9

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.

Permeability answered 31/8, 2016 at 17:52 Comment(2)
There is a very good blog entry about it codeaffine.com/2016/04/06/replace-rules-in-junit5, nevertheless, this is asking for documentation :)Cuisine
That's awesome that you've successfully migrated to JUnit Jupiter (JUnit 5)! Regarding a replacement for the JUnit 4 support in Spring REST Docs, I've created an issue for that here: github.com/spring-projects/spring-restdocs/issues/296Genia
H
9

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

@ExtendWith(RestDocumentationExtension.class) public class JUnit5ExampleTests {

Spring RestDocs 2 requires Spring 5 and JDK 8

Hyonhyoscine answered 7/12, 2017 at 15:49 Comment(1)
A slight change was made in Spring REST Docs 2.0.2.RELEASE. See github.com/spring-projects/spring-restdocs/blob/v2.0.2.RELEASE/…Depurate
I
2

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);
    }
}
Iodism answered 10/11, 2016 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.