8

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.

florian negre
  • 114
  • 1
  • 5
Juergen Zimmermann
  • 2,084
  • 7
  • 29
  • 43
  • 1
    There is a very good blog entry about it http://www.codeaffine.com/2016/04/06/replace-rules-in-junit5/, nevertheless, this is asking for documentation :) – cheffe Sep 01 '16 at 05:07
  • 1
    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: https://github.com/spring-projects/spring-restdocs/issues/296 – Sam Brannen Sep 01 '16 at 14:07

2 Answers2

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

florian negre
  • 114
  • 1
  • 5
  • 5
    A slight change was made in Spring REST Docs 2.0.2.RELEASE. See https://github.com/spring-projects/spring-restdocs/blob/v2.0.2.RELEASE/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java – Mike Partridge Jan 24 '20 at 19:59
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);
    }
}
Eric J Turley
  • 350
  • 1
  • 5
  • 20