0

我有一个使用 MongoDB 的 Spring Boot 应用程序。我想将嵌入式 FongoDB 用于我的 JUnit 测试。我关注一些文章,例如:

http://dontpanic.42.nl/2015/02/in-memory-mongodb-for-unit-and.html

我的 Fongo 测试配置看起来像

package com.myproject.rest;

import com.github.fakemongo.Fongo;
import com.mongodb.Mongo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Profile("test")
@ComponentScan(basePackages = "com.myproject.service.data")
@EnableMongoRepositories(basePackages = "com.myproject.service.data.repository")
@Configuration
public class MongoTestConfig extends AbstractMongoConfiguration {

    @Override
    protected String getDatabaseName() {
        return "demo-test";
    }

    @Bean
    @Override
    public Mongo mongo() {
        return new Fongo("mongo-test").getMongo();
    }

}

我对此有这种依赖性:

        <dependency>
            <groupId>com.github.fakemongo</groupId>
            <artifactId>fongo</artifactId>
            <version>2.1.0</version>
            <scope>test</scope>
        </dependency>

我想测试我的服务,它自动连接了一个扩展 MongoRepository 的存储库接口

package com.myproject.service.data.repository;

import com.myproject.service.data.entity.JournalData;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface JournalRepository extends MongoRepository<JournalData, String> {

    @Override
    public List<JournalData> findAll(Iterable<String> ids);
}

我的测试类继承自以下类:

package com.myproject.rest;

import com.lordofthejars.nosqlunit.mongodb.MongoDbRule;
import cz.csas.services.commons.api.RequestMetadata;
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@PowerMockIgnore({"javax.xml.*", "org.xml.*", "org.w3c.*", "javax.management.*", "javax.net.ssl.*"})
@PrepareForTest({RequestMetadata.class})
@ActiveProfiles("test")
public class EndpointTestContext {

    @Autowired
    protected MockMvc mockMvc;

    //@Rule
    //public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("demo-test");

    @Before
    public void setup() {
        mockStatic(RequestMetadata.class);
        when(RequestMetadata.builder()).thenCallRealMethod();
        RequestMetadata m = RequestMetadata.builder()
                .workingMode("TEST")
                .build();
        when(RequestMetadata.getMetadata()).thenReturn(m);
    }

}

但是当我运行 Maven 测试时,我收到以下信息:

Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.MongoClientOptions
    at com.mongodb.MockMongoClient.create(MockMongoClient.java:42)
    at com.github.fakemongo.Fongo.createMongo(Fongo.java:175)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:88)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:75)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:67)
    at com.myproject.rest.MongoTestConfig.mongo(MongoTestConfig.java:30)
    at com.myproject.rest.MongoTestConfig$$EnhancerBySpringCGLIB$$bf18b1d4.CGLIB$mongo$1(<generated>)
    at com.myproject.rest.MongoTestConfig$$EnhancerBySpringCGLIB$$bf18b1d4$$FastClassBySpringCGLIB$$3ab8bfd7.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at com.myproject.rest.MongoTestConfig$$EnhancerBySpringCGLIB$$bf18b1d4.mongo(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 115 common frames omitted

谁能建议如何解决它?先感谢您。

4

2 回答 2

0

Fongo 无法从mongo-java-driverartifact 中找到类。它宣布

<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.2</version>
</dependency>

作为提供的依赖。你应该mongo-java-driver在你的测试类路径上。最可能

<scope>runtime</scope>

或者test对你来说就足够了。

于 2017-11-15T17:26:35.527 回答
0

检查您是否有多个版本的 mongo java 驱动程序(版本冲突)。要检查这一点,您可以使用 mvn 依赖树来跟踪每个 jar 进入项目的路径。

mvn dependency:tree

只需要在 mongo java 驱动程序上找到正确的版本,并确保您没有版本冲突。我使用过 spring 数据并使用 fongo 为 mongodb 查询编写内存测试用例。

<fongo-version>1.6.3</fongo-version>
<spring-data-mongodb-version>1.7.2.RELEASE</spring-data-mongodb-version>
<spring-framework-version>4.1.5.RELEASE</spring-framework-version>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>${spring-data-mongodb-version}</version>
  </dependency>
<dependency>
    <groupId>com.github.fakemongo</groupId>
    <artifactId>fongo</artifactId>
    <version>${fongo-version}</version>
    <scope>test</scope>
  </dependency>

版本冲突可能来自可能的来源(可以使用上面的 mvc dependency:tree 命令确认,其中一个来源是mongobee,所以如果您正在使用它,请使用排除排除mongo-java-drive

<dependency>
    <groupId>com.github.mongobee</groupId>
    <artifactId>mongobee</artifactId>
    <version>${mongobee-version}</version>
    <exclusions>
      <exclusion>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
于 2018-02-05T19:20:55.667 回答