Spring Rest Doc (Gradle) 片段在构建时消失

问题描述

最近从 Maven 切换到 Gradle。

按照本教程使用 Gradle 进行持续的 REST Doc 构建。 https://www.youtube.com/watch?v=k5ncCJBarRI&t=1490s

运行测试时,代码片段生成得很好。当我尝试生成 asciidoc 时,它似乎在没有片段的情况下重新创建了 /build 目录。所以我生成的 html 总是显示

Unresolved directive in index.adoc - include::{snippets}/home-json/curl-request.adoc[]

我正在通过终端中的以下命令生成 asciidoc

gradle asciidoctor -t

// Continuous build command
// Mentioned around @1:07:40 mark
// https://www.youtube.com/watch?v=k5ncCJBarRI&t=1490s

build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'org.asciidoctor.convert' version '1.5.8'
    id 'java'
}

group = 'lab.restdocs'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'

repositories {
    mavenCentral()
}

ext {
    set('snippetsDir',file("build/generated-snippets"))
}

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'junit:junit:4.12'

    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
}

test {
    outputs.dir snippetsDir
    useJUnitPlatform()
}

asciidoctor {
    inputs.dir snippetsDir
    dependsOn test
}


bootJar {
    dependsOn asciidoctor
    from ("${asciidoctor.outputDir}/html5") {
        into 'static/docs'
    }
}

我的测试

@RunWith(springrunner.class)
@SpringBoottest
@AutoConfiguremockmvc
public class HelloControllerTest {

    @Autowired
    private mockmvc mockmvc;

    @Rule
    public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();


    @InjectMocks
    private HelloController helloController;

    @Mock
    private HelloService helloService;

    @Before
    public void setUp() throws Exception {
        // create a mock environment of helloController
        mockmvc = mockmvcBuilders.standalonesetup(helloController)
                .apply(documentationConfiguration(this.restDocumentation))
                .build();

    }

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockmvc.perform(get("/hello/string"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsstring("hello there"))
                )
                .andDo(document("home-string"));

    }

我还根据 https://spring.io/guides/gs/testing-restdocs/https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/#configuration-uris 检查了我的 build.gradle。我不知道我错过了什么......

提前致谢。

已编辑

运行命令 gradle asciidoctor --console=plain

如果它能让我更轻松地创建一个 Git 存储库 https://github.com/erich5168/edu.rest-doc

 erichuang$ gradle asciidoctor --console=plain
> Task :compileJava
> Task :processResources
> Task :classes
> Task :compileTestJava
> Task :processtestResources NO-SOURCE
> Task :testClasses
> Task :test

> Task :asciidoctor
asciidoctor: WARNING: api.adoc: line 3: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/curl-request.adoc
asciidoctor: WARNING: api.adoc: line 5: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/http-request.adoc
asciidoctor: WARNING: api.adoc: line 7: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/http-response.adoc
asciidoctor: WARNING: api.adoc: line 20: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home-json/http-response.adoc

Deprecated Gradle features were used in this build,making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
5 actionable tasks: 5 executed
Apples-MBP:lab.restdocs-gradlebuild erichuang$ 

解决方法

从命令行运行时,您的构建没有运行任何测试。由于您的测试使用 JUnit 4 而未运行测试,而 test 任务已配置为使用 JUnit 平台 (JUnit 5):

test {
    outputs.dir snippetsDir
    useJUnitPlatform()
}

您可以通过更新测试以使用 JUnit 5 或通过从 useJUnitPlatform() 任务的配置中删除 test 以便它使用 JUnit 4 来解决该问题。后者是较小的更改并保留test 任务如下所示:

test {
    outputs.dir snippetsDir
}