【发布时间】:2016-01-21 08:14:53
【问题描述】:
我们认为Spring Rest Doc 非常适合记录rest api。但它是基于Spring MVC Test,我们无法弄清楚如何在我的grails应用程序(Grails 3.0.5)中使用Spring MVC Test。
我尝试使用配置类(使用@Configuration 和@ComponentScan)将grails 组件扫描到测试上下文中,但似乎没有加载任何内容(当对mockmvc 执行http 请求时,它得到 404)。
我也试过直接配置grails控制器,运行时出错。
无法自动装配字段:私有 reactor.bus.EventBus
我还尝试在测试类中添加@Integration(来自 grails),但收到了同样的错误。
请帮忙。
这里有一些代码示例。
我尝试将配置类或类位置或 grails 控制器添加到下面测试类的 ContextConfiguration 中。并且测试类本身基本遵循spring rest doc参考。
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.restdocs.RestDocumentation;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
//TODO how to scan Grails components into the test context
public class QuestionRestSpec {
@Rule
public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
}
配置类(没有用):
@Configuration
@EnableWebMvc
@ComponentScan
public class AskConfig {
}
【问题讨论】:
-
您使用的是哪个版本的 Grails?
-
我正在使用 Grails 3.0.5
-
Grails 3 只是一个 Spring Boot 应用程序,因此,至少在理论上,它应该是可能的,尽管可能不是真正的 Grails 方式。
@Integration并使用@Autowired注入ApplicationContext将是我会尝试的。你能分享一下你对那不起作用的尝试吗? -
我不太清楚 @Integration 和使用 @Autowired 注入 ApplicationContext 是什么意思。 我通过添加一些代码示例更新了问题。基本上,它遵循
spring rest doc参考。
标签: grails grails-3.0 spring-mvc-test spring-restdocs