如何在 Spring Boot 中模拟 db 连接以进行测试?
2022-09-02 21:44:43
情况:
- 我在微服务中使用,该微服务正在加载数据库配置信息以配置连接。
Spring Cloud
Spring Boot
- 我创建了一个测试来获取用于文档的其余接口。
Swagger
- 我想禁用数据库配置的加载,因为这不是必需的。
代码如下:
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class, Swagger2MarkupTest.class}, loader = SpringApplicationContextLoader.class)
@ActiveProfiles("test")
public class Swagger2MarkupTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Autowired
protected Environment env;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void convertSwaggerToAsciiDoc() throws Exception {
this.mockMvc.perform(get("/v2/api-docs").accept(MediaType.APPLICATION_JSON))
.andDo(Swagger2MarkupResultHandler.outputDirectory("target/docs/asciidoc/generated")
.withExamples("target/docs/asciidoc/generated/exampless").build())
.andExpect(status().isOk());
}
}
如何在不加载数据库配置的情况下运行测试?这可能吗?