【问题标题】:Using embedded MongoDB in Spring JUnit @WebMvcTest在 Spring JUnit @WebMvcTest 中使用嵌入式 MongoDB
【发布时间】:2018-03-28 08:56:29
【问题描述】:

我目前在我的 Spring 应用程序中使用 MongoDB。由于我添加了 Mongo,由于以下错误,我的端点测试不再起作用:

No qualifying bean of type 'xxx' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我在控制器中自动装配的存储库如下:

private final RuleRepository ruleRepository;

@Autowired
public TestController(RuleRepository ruleRepository) {
    this.ruleRepository = ruleRepository;
}

我认为这与 Mongo 以及我目前使用 AutoConfiguration 的事实有关。对于测试,我将 Flapdoodle Embed Mongo 依赖项添加到我的 pom.xml 中,范围设置为测试:

<dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>2.0.3</version>
        <scope>test</scope>
    </dependency>

我的测试类如下所示:

RunWith(SpringRunner.class)
@WebMvcTest(value = RouteController.class, secure = false)
@ActiveProfiles("test")
public class TestControllerEndpointTests {

@Autowired
private MockMvc mockMvc;

@Autowired
private RuleRepository ruleRepository;

@Before
public void setupTests() {
    //Setup for the tests
}

    //Actual tests
}

我还为 Mongo 测试数据库创建了一个配置类,但我不知道如何正确注册它:

@Configuration
@Profile("test")
public class TestMongoConfig {

    @Autowired
    private MongoProperties properties;

    @Autowired(required = false)
    private MongoClientOptions options;

    @Bean(destroyMethod = "close")
    public Mongo mongo(MongodProcess mongodProcess) throws IOException {
        Net net = mongodProcess.getConfig().net();
        return new MongoClient(net.getServerAddress().getHostName(), net.getPort());
    }

    @Bean(destroyMethod = "stop")
    public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException {
        return mongodExecutable.start();
    }

    @Bean(destroyMethod = "stop")
    public MongodExecutable mongodExecutable(MongodStarter mongodStarter, IMongodConfig iMongodConfig) throws IOException {
        return mongodStarter.prepare(iMongodConfig);
    }

    @Bean
    public IMongodConfig mongodConfig() throws IOException {
        return new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();
    }

    @Bean
    public MongodStarter mongodStarter() {
        return MongodStarter.getDefaultInstance();
    }

}

如何获得带有 @WebMvcTest 注释的端点测试以使用嵌入式 Mongo 数据库?

【问题讨论】:

    标签: mongodb spring-boot junit spring-data-mongodb


    【解决方案1】:

    敲了一阵子后,我们找到了@AutoConfigureDataMongo注解。

    import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
    
    @RunWith(SpringRunner.class)
    @WebMvcTest(value = SampleController.class, secure = false)
    @AutoConfigureDataMongo
    public class SampleControllerTest {
    

    只需用它注释您的控制器,运行此控制器测试时您应该会看到 org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo 日志。

    【讨论】:

    • 它现在可以在@WebMvcTest 上运行,但是这个注释破坏了我在@DataMongoTest 注释的其他测试......
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 2018-06-10
    • 2020-09-05
    • 1970-01-01
    • 2018-10-09
    • 2020-01-24
    • 2016-08-26
    • 1970-01-01
    相关资源
    最近更新 更多