首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring中使用Fongo (假芒果)进行集成测试

如何在Spring中使用Fongo (假芒果)进行集成测试
EN

Stack Overflow用户
提问于 2020-08-10 20:26:05
回答 3查看 2.3K关注 0票数 0

我正在使用mongodb作为后端开发spring引导应用程序。

用于crud操作的蒙古存储库

我想用假芒果(fongo)进行集成测试。

我已经参考了从下面的链接使用fongo进行集成测试,但没有运气。

https://www.paradigmadigital.com/dev/tests-integrados-spring-boot-fongo/

集成测试试图连接到真正的数据库,而IT失败了

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { ConfigServerWithFongoConfiguration.class }, properties = {
        "server.port=8980" }, webEnvironment = WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
@TestPropertySource(properties = { "spring.data.mongodb.database=test" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class PersonControllerITest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MockMvc mockMvc;

    private ObjectMapper jsonMapper;

    @Before
    public void setUp() {
        jsonMapper = new ObjectMapper();
    }

    @Test
    public void testGetPerson() throws Exception {

        Person personFongo = new Person();
        personFongo.setId(1);
        personFongo.setName("Name1");
        personFongo.setAddress("Address1");
        mongoTemplate.createCollection("person");
        mongoTemplate.insert(personFongo);

        ResultActions resultAction = mockMvc.perform(MockMvcRequestBuilders.get("http://localhost:8090/api/person/1"));
        resultAction.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
        MvcResult result = resultAction.andReturn();
        Person personResponse = jsonMapper.readValue(result.getResponse().getContentAsString(), Person.class);
        Assert.assertEquals(1, personResponse.getId());
        Assert.assertEquals("Name1", personResponse.getName());
        Assert.assertEquals("Address1", personResponse.getAddress());

    }

    @Test
    public void testCreatePerson() throws Exception {

        Person personRequest = new Person();
        personRequest.setId(1);
        personRequest.setName("Name1");
        personRequest.setAddress("Address1");

        String body = jsonMapper.writeValueAsString(personRequest);

        ResultActions resultAction = mockMvc.perform(MockMvcRequestBuilders.post("http://localhost:8090/api/person")
                .contentType(MediaType.APPLICATION_JSON).content(body));
        resultAction.andExpect(MockMvcResultMatchers.status().isCreated());

        Person personFongo = mongoTemplate.findOne(new Query(Criteria.where("id").is(1)), Person.class);
        Assert.assertEquals(personFongo.getName(), "Name1");
        Assert.assertEquals(personFongo.getAddress(), "Address1");
    }
}

在执行集成测试时,我会收到以下错误

代码语言:javascript
复制
   java.lang.IllegalStateException: Failed to load ApplicationContext
    
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
        at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
        at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'entitlementsRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entitlementsRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        ... 25 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entitlementsRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        
        ... 44 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
        
        ... 57 more
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 66 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        
        ... 67 more
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDbFactory]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 89 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in class path resource [com/company/myapp/configs/MongoConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:389)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
        at com.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08.mongoClient(<generated>)
        at com.company.myapp.configs.MongoConfig.mongoDbFactory(MongoConfig.java:73)
        at com.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08.CGLIB$mongoDbFactory$1(<generated>)
        at com.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08$$FastClassBySpringCGLIB$$50ee8948.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.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08.mongoDbFactory(<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:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        ... 90 more
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 112 more
    Caused by: java.lang.NullPointerException
        at java.util.Hashtable.put(Hashtable.java:460)
        at java.util.Properties.setProperty(Properties.java:166)
        at java.lang.System.setProperty(System.java:796)
        at com.company.myapp.configs.MongoConfig.mongoClient(MongoConfig.java:78)
        at com.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08.CGLIB$mongoClient$3(<generated>)
        at com.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08$$FastClassBySpringCGLIB$$50ee8948.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.company.myapp.configs.MongoConfig$$EnhancerBySpringCGLIB$$33034a08.mongoClient(<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:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        ... 113 more

我的主Application.java

代码语言:javascript
复制
@SpringBootApplication(exclude = {MongoDataAutoConfiguration.class, MongoAutoConfiguration.class})
@EntityScan(basePackageClasses = Jsr310Converters.class)
@ComponentScan(basePackages = {"com.company.myapp", "com.company.myapp.repository"})
@AutoConfigureAfter(MongoConfig.class)
@EnableSwagger2
@EnableScheduling
public class Application
{
public class Application
{
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Value("${http.port}")
    int httpPort;

    @Value("${ssl.port}")
    int sslPort;

    @Value("${ssl.keystore.file}")
    String keyStoreFile;

    @Value("${ssl.keystore.password}")
    String keyStorePassword;

    @Autowired
    FileUtil fileUtil;

    public static void main(String[] args)
    {
        Runtime.getRuntime().addShutdownHook(new Thread() {                       
            @Override
            public void run() {
                logger.info("myapp server shutdown initiated");
                LogManager.shutdown();
                logger.info("myapp server shutdown completed");
            }
        });
        SpringApplication.run(Application.class, args);                          
    }

    @Bean
    public CompressingFilter compressingFilter()
    {
        return new CompressingFilter();
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer()
    {
        String keystore = "dev";

        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) con -> {
                    //if ("true".equalsIgnoreCase(sslEnabled)) {
                    logger.info("----------------------------------------- Initialising ssl endpoint -------------------------------------");
                    Http11NioProtocol proto = (Http11NioProtocol) con.getProtocolHandler();
                    proto.setSSLEnabled(true);
                    con.setScheme("https");
                    con.setSecure(true);
                    con.setPort(sslPort);
                    proto.setKeystoreFile(fileUtil.getConfigFilePath(keyStoreFile));
                    proto.setKeystorePass(keystore);
                    proto.setKeyPass(keystore);
                    logger.info("---------------------------------------- Initialised ssl endpoint ----------------------------------------");
                    //}
                });

        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    private Connector createStandardConnector()
    {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}

AbstractFongoBaseConfiguration.java

代码语言:javascript
复制
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public abstract class AbstractFongoBaseConfiguration extends AbstractMongoConfiguration{

    @Autowired
    private Environment env;

    @Override
    protected String getDatabaseName() {
        return env.getRequiredProperty("spring.data.mongodb.database");
    }

    @Override
    public Mongo mongo() throws Exception {
        return new Fongo(getDatabaseName()).getMongo();
    }
}

ConfigServerWithFongoConfiguration.java

代码语言:javascript
复制
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class,
        MongoDataAutoConfiguration.class })
@Configuration
@ComponentScan(basePackages = { "com.company.myapp" }, excludeFilters = {
        @ComponentScan.Filter(classes = { SpringBootApplication.class }) })
public class ConfigServerWithFongoConfiguration extends AbstractFongoBaseConfiguration {

}

请你提出建议,为解决这一问题需要做些什么?

EN

回答 3

Stack Overflow用户

发布于 2020-08-21 16:54:27

我尝试过使用您的Integration代码和相同的配置文件。

但是对于AbstractFongoBaseConfiguration.class,我提供了下面的代码。

我已经删除了@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})的那条线。对我来说很好。

代码语言:javascript
复制
package com.company.myapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

import com.github.fakemongo.Fongo;
import com.mongodb.Mongo;
//this exclude i had removed
public abstract class AbstractFongoBaseConfiguration extends AbstractMongoConfiguration{

    @Autowired
    private Environment env;

    @Override
    protected String getDatabaseName() {
        return env.getRequiredProperty("spring.data.mongodb.database");
    }

    @Override
    public Mongo mongo() throws Exception {
        return new Fongo(getDatabaseName()).getMongo();
    }
}

这里我用同样的代码附加了视频。

还有一点我想告诉您,让您的ConfigServerWithFongoConfigurationAbstractFongoBaseConfigurationPersonControllerITest类具有相同的测试目录包。

我还预先假定您的应用程序运行良好,并且只存在集成测试问题。

在这里,我已经成功地附加了运行测试用例屏幕。

票数 1
EN

Stack Overflow用户

发布于 2020-08-22 05:32:55

Fongo似乎不再被维护了。如果可行的话,我会切换到测试容器( https://www.testcontainers.org/modules/databases/mongodb/ )

这是您要做的事情的更好的参考:https://www.baeldung.com/spring-boot-embedded-mongodb

您需要在测试中添加@AutoConfigureDataMongo

票数 0
EN

Stack Overflow用户

发布于 2020-08-15 08:52:51

在堆栈跟踪中,异常发生在"MongoConfig“类中。如图所示:

com.company.myapp.configs.MongoConfig.mongoClient(MongoConfig.java:78)

它向我表明,您的Spring配置链没有按您的意愿设置。看起来,在实际应用程序环境中使用的实际MongoConfig类仍在加载中。

如果没有完整的项目(或者实际上导致NullPointer的Config类),几乎不可能对您的问题给出一个详细的答案。但是,我建议您创建一个特定的TestConfig spring配置类,这样您可以只包含希望成为测试上下文一部分的主配置类。

TLDR:查看配置设置,并找出为什么加载MongoConfig.java。如果它是要加载的,在这里包含它,这样人们就可以找出问题所在。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63347525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档