Migrate tests to JUnit 5

Closes gh-959
This commit is contained in:
Andy Wilkinson
2025-06-03 16:47:20 +01:00
parent d0c224de95
commit c7bde714d6
105 changed files with 4310 additions and 4163 deletions

View File

@@ -10,10 +10,15 @@ dependencies {
internal(platform(project(":spring-restdocs-platform")))
testImplementation("junit:junit")
testImplementation("org.apache.pdfbox:pdfbox")
testImplementation("org.assertj:assertj-core")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.springframework:spring-core")
testRuntimeOnly("org.asciidoctor:asciidoctorj-pdf")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.named("test") {
useJUnitPlatform()
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,11 +35,10 @@ import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.FileSystemUtils;
@@ -51,42 +50,42 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Gerrit Meier
* @author Andy Wilkinson
*/
public abstract class AbstractOperationBlockMacroTests {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private Options options;
abstract class AbstractOperationBlockMacroTests {
private final Asciidoctor asciidoctor = Asciidoctor.Factory.create();
@Before
public void setUp() throws IOException {
@TempDir
protected File temp;
private Options options;
@BeforeEach
void setUp() throws IOException {
prepareOperationSnippets(getBuildOutputLocation());
this.options = Options.builder().safe(SafeMode.UNSAFE).baseDir(getSourceLocation()).build();
this.options.setAttributes(getAttributes());
CapturingLogHandler.clear();
}
@After
public void verifyLogging() {
@AfterEach
void verifyLogging() {
assertThat(CapturingLogHandler.getLogRecords()).isEmpty();
}
public void prepareOperationSnippets(File buildOutputLocation) throws IOException {
private void prepareOperationSnippets(File buildOutputLocation) throws IOException {
File destination = new File(buildOutputLocation, "generated-snippets/some-operation");
destination.mkdirs();
FileSystemUtils.copyRecursively(new File("src/test/resources/some-operation"), destination);
}
@Test
public void codeBlockSnippetInclude() throws Exception {
void codeBlockSnippetInclude() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets='curl-request']", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-simple"));
}
@Test
public void operationWithParameterizedName() throws Exception {
void operationWithParameterizedName() throws Exception {
Attributes attributes = getAttributes();
attributes.setAttribute("name", "some");
this.options.setAttributes(attributes);
@@ -95,20 +94,20 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void codeBlockSnippetIncludeWithPdfBackend() throws Exception {
void codeBlockSnippetIncludeWithPdfBackend() throws Exception {
File output = configurePdfOutput();
this.asciidoctor.convert("operation::some-operation[snippets='curl-request']", this.options);
assertThat(extractStrings(output)).containsExactly("Curl request", "$ curl 'http://localhost:8080/' -i", "1");
}
@Test
public void tableSnippetInclude() throws Exception {
void tableSnippetInclude() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets='response-fields']", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-table"));
}
@Test
public void tableSnippetIncludeWithPdfBackend() throws Exception {
void tableSnippetIncludeWithPdfBackend() throws Exception {
File output = configurePdfOutput();
this.asciidoctor.convert("operation::some-operation[snippets='response-fields']", this.options);
assertThat(extractStrings(output)).containsExactly("Response fields", "Path", "Type", "Description", "a",
@@ -116,14 +115,14 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void includeSnippetInSection() throws Exception {
void includeSnippetInSection() throws Exception {
String result = this.asciidoctor.convert("= A\n:doctype: book\n:sectnums:\n\nAlpha\n\n== B\n\nBravo\n\n"
+ "operation::some-operation[snippets='curl-request']\n\n== C\n", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section"));
}
@Test
public void includeSnippetInSectionWithAbsoluteLevelOffset() throws Exception {
void includeSnippetInSectionWithAbsoluteLevelOffset() throws Exception {
String result = this.asciidoctor
.convert("= A\n:doctype: book\n:sectnums:\n:leveloffset: 1\n\nAlpha\n\n= B\n\nBravo\n\n"
+ "operation::some-operation[snippets='curl-request']\n\n= C\n", this.options);
@@ -131,7 +130,7 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void includeSnippetInSectionWithRelativeLevelOffset() throws Exception {
void includeSnippetInSectionWithRelativeLevelOffset() throws Exception {
String result = this.asciidoctor
.convert("= A\n:doctype: book\n:sectnums:\n:leveloffset: +1\n\nAlpha\n\n= B\n\nBravo\n\n"
+ "operation::some-operation[snippets='curl-request']\n\n= C\n", this.options);
@@ -139,7 +138,7 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void includeSnippetInSectionWithPdfBackend() throws Exception {
void includeSnippetInSectionWithPdfBackend() throws Exception {
File output = configurePdfOutput();
this.asciidoctor.convert("== Section\n" + "operation::some-operation[snippets='curl-request']", this.options);
assertThat(extractStrings(output)).containsExactly("Section", "Curl request",
@@ -147,26 +146,26 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void includeMultipleSnippets() throws Exception {
void includeMultipleSnippets() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets='curl-request,http-request']",
this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("multiple-snippets"));
}
@Test
public void useMacroWithoutSnippetAttributeAddsAllSnippets() throws Exception {
void useMacroWithoutSnippetAttributeAddsAllSnippets() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[]", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("all-snippets"));
}
@Test
public void useMacroWithEmptySnippetAttributeAddsAllSnippets() throws Exception {
void useMacroWithEmptySnippetAttributeAddsAllSnippets() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets=]", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("all-snippets"));
}
@Test
public void includingMissingSnippetAddsWarning() throws Exception {
void includingMissingSnippetAddsWarning() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets='missing-snippet']", this.options);
assertThat(result).startsWith(getExpectedContentFromFile("missing-snippet"));
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
@@ -177,13 +176,13 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void defaultTitleIsProvidedForCustomSnippet() throws Exception {
void defaultTitleIsProvidedForCustomSnippet() throws Exception {
String result = this.asciidoctor.convert("operation::some-operation[snippets='custom-snippet']", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("custom-snippet-default-title"));
}
@Test
public void missingOperationIsHandledGracefully() throws Exception {
void missingOperationIsHandledGracefully() throws Exception {
String result = this.asciidoctor.convert("operation::missing-operation[]", this.options);
assertThat(result).startsWith(getExpectedContentFromFile("missing-operation"));
assertThat(CapturingLogHandler.getLogRecords()).hasSize(1);
@@ -194,14 +193,14 @@ public abstract class AbstractOperationBlockMacroTests {
}
@Test
public void titleOfBuiltInSnippetCanBeCustomizedUsingDocumentAttribute() throws URISyntaxException, IOException {
void titleOfBuiltInSnippetCanBeCustomizedUsingDocumentAttribute() throws URISyntaxException, IOException {
String result = this.asciidoctor.convert(":operation-curl-request-title: Example request\n"
+ "operation::some-operation[snippets='curl-request']", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("built-in-snippet-custom-title"));
}
@Test
public void titleOfCustomSnippetCanBeCustomizedUsingDocumentAttribute() throws Exception {
void titleOfCustomSnippetCanBeCustomizedUsingDocumentAttribute() throws Exception {
String result = this.asciidoctor.convert(":operation-custom-snippet-title: Customized title\n"
+ "operation::some-operation[snippets='custom-snippet']", this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("custom-snippet-custom-title"));

View File

@@ -21,7 +21,7 @@ import java.io.File;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,23 +30,23 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
public class DefaultAttributesPreprocessorTests {
class DefaultAttributesPreprocessorTests {
@Test
public void snippetsAttributeIsSet() {
void snippetsAttributeIsSet() {
String converted = createAsciidoctor().convert("{snippets}", createOptions("projectdir=../../.."));
assertThat(converted).contains("build" + File.separatorChar + "generated-snippets");
}
@Test
public void snippetsAttributeFromConvertArgumentIsNotOverridden() {
void snippetsAttributeFromConvertArgumentIsNotOverridden() {
String converted = createAsciidoctor().convert("{snippets}",
createOptions("snippets=custom projectdir=../../.."));
assertThat(converted).contains("custom");
}
@Test
public void snippetsAttributeFromDocumentPreambleIsNotOverridden() {
void snippetsAttributeFromDocumentPreambleIsNotOverridden() {
String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}",
createOptions("projectdir=../../.."));
assertThat(converted).contains("custom");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,47 +19,42 @@ package org.springframework.restdocs.asciidoctor;
import java.io.File;
import org.asciidoctor.Attributes;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests for Ruby operation block macro when used in a Gradle build.
*
* @author Andy Wilkinson
*/
@RunWith(Parameterized.class)
public class GradleOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
@ParameterizedClass
@ValueSource(strings = { "projectdir", "gradle-projectdir" })
class GradleOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
private final String attributeName;
public GradleOperationBlockMacroTests(String attributeName) {
GradleOperationBlockMacroTests(String attributeName) {
this.attributeName = attributeName;
}
@Parameters(name = "{0}")
public static Object[] parameters() {
return new Object[] { "projectdir", "gradle-projectdir" };
}
@Override
protected Attributes getAttributes() {
Attributes attributes = Attributes.builder()
.attribute(this.attributeName, new File(this.temp.getRoot(), "gradle-project").getAbsolutePath())
.attribute(this.attributeName, new File(this.temp, "gradle-project").getAbsolutePath())
.build();
return attributes;
}
@Override
protected File getBuildOutputLocation() {
File outputLocation = new File(this.temp.getRoot(), "gradle-project/build");
File outputLocation = new File(this.temp, "gradle-project/build");
outputLocation.mkdirs();
return outputLocation;
}
@Override
protected File getSourceLocation() {
File sourceLocation = new File(this.temp.getRoot(), "gradle-project/src/docs/asciidoc");
File sourceLocation = new File(this.temp, "gradle-project/src/docs/asciidoc");
if (!sourceLocation.exists()) {
sourceLocation.mkdirs();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,23 +20,23 @@ import java.io.File;
import java.io.IOException;
import org.asciidoctor.Attributes;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
/**
* Tests for Ruby operation block macro when used in a Maven build.
*
* @author Andy Wilkinson
*/
public class MavenOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
class MavenOperationBlockMacroTests extends AbstractOperationBlockMacroTests {
@Before
public void setMavenHome() {
@BeforeEach
void setMavenHome() {
System.setProperty("maven.home", "maven-home");
}
@After
public void clearMavenHome() {
@AfterEach
void clearMavenHome() {
System.clearProperty("maven.home");
}
@@ -55,14 +55,14 @@ public class MavenOperationBlockMacroTests extends AbstractOperationBlockMacroTe
@Override
protected File getBuildOutputLocation() {
File outputLocation = new File(this.temp.getRoot(), "maven-project/target");
File outputLocation = new File(this.temp, "maven-project/target");
outputLocation.mkdirs();
return outputLocation;
}
@Override
protected File getSourceLocation() {
File sourceLocation = new File(this.temp.getRoot(), "maven-project/src/main/asciidoc");
File sourceLocation = new File(this.temp, "maven-project/src/main/asciidoc");
if (!sourceLocation.exists()) {
sourceLocation.mkdirs();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2024 the original author or authors.
* Copyright 2014-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,9 +21,8 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -35,23 +34,23 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/
public class SnippetsDirectoryResolverTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
File temp;
@Test
public void mavenProjectsUseTargetGeneratedSnippets() throws IOException {
this.temporaryFolder.newFile("pom.xml");
new File(this.temp, "pom.xml").createNewFile();
Map<String, Object> attributes = new HashMap<>();
attributes.put("docdir", new File(this.temporaryFolder.getRoot(), "src/main/asciidoc").getAbsolutePath());
attributes.put("docdir", new File(this.temp, "src/main/asciidoc").getAbsolutePath());
File snippetsDirectory = getMavenSnippetsDirectory(attributes);
assertThat(snippetsDirectory).isAbsolute();
assertThat(snippetsDirectory).isEqualTo(new File(this.temporaryFolder.getRoot(), "target/generated-snippets"));
assertThat(snippetsDirectory).isEqualTo(new File(this.temp, "target/generated-snippets"));
}
@Test
public void illegalStateExceptionWhenMavenPomCannotBeFound() {
Map<String, Object> attributes = new HashMap<>();
String docdir = new File(this.temporaryFolder.getRoot(), "src/main/asciidoc").getAbsolutePath();
String docdir = new File(this.temp, "src/main/asciidoc").getAbsolutePath();
attributes.put("docdir", docdir);
assertThatIllegalStateException().isThrownBy(() -> getMavenSnippetsDirectory(attributes))
.withMessage("pom.xml not found in '" + docdir + "' or above");