对嵌入式Fongo DB的JUnit测试失败

时间:2017-11-15 17:15:33

标签: java maven spring-boot junit fongo

我有一个使用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

有人可以建议如何解决它吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

Fongo无法从mongo-java-driver神器找到课程。它宣布

<!-- 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对您来说已经足够了。

答案 1 :(得分: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>

版本冲突可以来自may源(可以确认使用上面的mvc依赖: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>