【问题标题】:Spring embedded Mongo DBSpring嵌入MongoDB
【发布时间】:2020-01-24 14:59:27
【问题描述】:

我有一个带有 Spring Boot 和 Embedded Mongo DB 的项目,我还想查找已存储在那里的数据。怎么做 我跟着这个教程https://springframework.guru/spring-boot-with-embedded-mongodb/

一切正常,除了我无法使用这个 mongo-express 连接到嵌入式数据库?设置了哪个密码和用户名?我如何连接到本地主机嵌入式数据库???完成本教程后?

【问题讨论】:

    标签: spring mongodb embedded-database


    【解决方案1】:

    为了在您的测试中使用 EmbeddedMongo,必须在 application.properties 中设置此属性。

    spring.data.mongodb.uri=mongodb://localhost/test
    

    您可以测试在 SpringBootMongodbApplicationTests.java 中添加测试方法,如下所示。

    package guru.springframework;
    
    import guru.springframework.domain.Product;
    import guru.springframework.repositories.ProductRepository;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.math.BigDecimal;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringBootMongodbApplicationTests {
    
        @Autowired
        private ProductRepository productRepository;
    
        @Test
        public void contextLoads() {
    
        }
    
        @Test
        public void testMongoDbRepository(){
            Product product = new Product();
            product.setDescription("DESC");
            product.setImageUrl("test.png");
            product.setPrice( BigDecimal.valueOf(1500) );
            productRepository.save(product);
    
            long count = productRepository.count();
            Assert.assertEquals( 1, count );
            productRepository.deleteAll();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      相关资源
      最近更新 更多