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

如何解决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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?