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

Spring Rest Doc (Gradle) Snippets disappear upon build

最近从 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/ and 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 或从 test 任务的配置中删除 useJUnitPlatform() 以便它使用 JUnit 4 来解决问题。后者是较小的更改,并且让 test 任务看起来像这样:

test {
    outputs.dir snippetsDir
}