【问题标题】:Spring REST docs: How to migrate Rule to JUnit 5Spring REST 文档:如何将规则迁移到 JUnit 5
【发布时间】:2016-08-31 17:52:40
【问题描述】:
我将我的 Spring 测试迁移到了 JUnit 5,它们运行良好。但是,我不知道如何迁移@Rule public JUnitRestDocumentation restDocumentation = ...。
任何提示表示赞赏。
【问题讨论】:
标签:
junit
junit4
junit5
spring-restdocs
junit-jupiter
【解决方案1】:
Spring RestDocs 2 为 JUnit 5 引入了一个新类:RestDocumentationExtension。您可以使用它来代替 Rule
@ExtendWith(RestDocumentationExtension.class)
public class JUnit5ExampleTests {
Spring RestDocs 2 需要 Spring 5 和 JDK 8
【解决方案2】:
在问题得到正式解决之前,我能够使用 JUnit 5 扩展(如下)使其工作。
使用该扩展,我修改了我的测试类:
@ExtendWith(RestDocsExtension.class)
和
@BeforeEach
void setUp(WebApplicationContext wac, ManualRestDocumentation restDocumentation) throws Exception {
这是扩展。
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);
}
}