51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 534|回复: 2
打印 上一主题 下一主题

项目通过Maven管理,已配置Mongodb连接,测试dao接口报错

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 530 天

    连续签到: 2 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2022-3-4 13:26:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点

    项目是通过Maven的,我想从Mongodb数据库里面查询出来存进去的数据,然后做单元测试DAO接口,一直报错拒绝连接。

    本地测试,可以访问Mongodb,使用 navicat 测试连接也没有问题。

    DAO接口

    1. public interface BannerDao {

    2.     List<Banner> getBanners();
    3. }
    复制代码

    DAO实现类

    1. @Repository
    2. public class BannerDaoImpl implements BannerDao {

    3.     @Autowired
    4.     private MongoTemplate mongoTemplate;

    5.     /**
    6.      * 查询数据
    7.      * @return
    8.      */
    9.     public List<Banner> getBanners() {
    10.         Date now = new Date();
    11.         Query query = Query.query(
    12.                 Criteria.where("beginTime").lt(now)
    13.                         .and("endTime").gt(now)
    14.         );

    15.         return  mongoTemplate.find(query,Banner.class);
    16.     }
    17. }

    复制代码

    单元测试类


    1. @SpringBootTest(classes = {LivegoodsBannerApp.class})
    2. @RunWith(SpringRunner.class)
    3. public class TestDao {

    4.     @Autowired
    5.     private BannerDao bannerDao;

    6.     @Test
    7.     public void testSelect(){
    8.         List<Banner> banners = bannerDao.getBanners();
    9.         for (Banner b : banners){
    10.             System.out.println(b);
    11.         }
    12.     }

    13. }
    复制代码

    demo_banner的pom-xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6.         <artifactId>demo</artifactId>
    7.         <groupId>com.demo</groupId>
    8.         <version>1.0-SNAPSHOT</version>
    9.     </parent>
    10.     <modelVersion>4.0.0</modelVersion>

    11.     <artifactId>demo_banner</artifactId>
    12.     <version>1.0-SNAPSHOT</version>
    13.     <dependencies>
    14.         <dependency>
    15.             <groupId>org.springframework.boot</groupId>
    16.             <artifactId>spring-boot-starter-web</artifactId>
    17.         </dependency>
    18.         <dependency>
    19.             <groupId>org.springframework.cloud</groupId>
    20.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    21.         </dependency>
    22.         <dependency>
    23.             <groupId>com.demo</groupId>
    24.             <artifactId>demo_mongodb</artifactId>
    25.             <version>1.0-SNAPSHOT</version>
    26.         </dependency>
    27.         <dependency>
    28.             <groupId>org.springframework.boot</groupId>
    29.             <artifactId>spring-boot-starter-test</artifactId>
    30.         </dependency>
    31.     </dependencies>

    32. </project>
    复制代码

    另一个子项目demo_mongodb配置文件如下:application-mongodb.yml

    1. spring:
    2.   data:
    3.     mongodb:
    4.       host: 192.168.1.13
    5.       port: 27017
    6.       username: admin
    7.       password: admin
    8.       authentication-database: admin
    9.       database: livegoods
    复制代码

    demo_mongodb的pom-xml


    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.     <parent>
    6.         <artifactId>demo</artifactId>
    7.         <groupId>com.demo</groupId>
    8.         <version>1.0-SNAPSHOT</version>
    9.     </parent>
    10.     <modelVersion>4.0.0</modelVersion>

    11.     <artifactId>demo_mongodb</artifactId>
    12.     <version>1.0-SNAPSHOT</version>

    13.     <dependencies>
    14.         <dependency>
    15.             <groupId>org.springframework.boot</groupId>
    16.             <artifactId>spring-boot-starter-data-mongodb</artifactId>
    17.         </dependency>
    18.     </dependencies>
    19. </project>
    复制代码

    run testDao报错信息如下:

    1. org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]

    2.     at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:88)
    3.     at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2874)
    4.     at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2799)
    5.     at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2518)
    6.     at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2500)
    7.     at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:856)
    8.     at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:842)
    9.     at com.livegoods.banner.dao.impl.BannerDaoImpl.getBanners(BannerDaoImpl.java:37)
    10.     at com.livegoods.banner.dao.impl.BannerDaoImpl$FastClassBySpringCGLIB$127a1d05.invoke(<generated>)
    11.     at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    12.     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
    13.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    14.     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    15.     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    16.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    17.     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    18.     at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
    19.     at com.livegoods.banner.dao.impl.BannerDaoImpl$EnhancerBySpringCGLIB$5c04f349.getBanners(<generated>)
    20.     at com.livegoods.test.dao.TestDao.testSelect(TestDao.java:29)
    21.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    22.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    23.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    24.     at java.lang.reflect.Method.invoke(Method.java:498)
    25.     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    26.     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    27.     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    28.     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    29.     at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    30.     at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    31.     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    32.     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    33.     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    34.     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    35.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    36.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    37.     at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    38.     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    39.     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    40.     at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    41.     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    42.     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    43.     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    44.     at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    45.     at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    46.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    47.     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    48.     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    49.     at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    50.     at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    51.     at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    52.     at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    53.     at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
    54. Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
    55.     at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:177)
    56.     at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
    57.     at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:147)
    58.     at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:98)
    59.     at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:278)
    60.     at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:182)
    61.     at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
    62.     at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
    63.     at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2782)
    64.     ... 49 more
    复制代码

    一开始是无法访问到,后面通过配置开放防火墙端口,并将mongodb配置文件添加成任何IP都可以访问,但运行后还是无法查询到数据库的数据,显示连接超时,拒绝连接什么的。

    我想测试DAO接口,看是否能查询出数据库里面的轮播图的图片路径、开始时间、过期时间这些的。不知道是哪里出错了,希望能帮忙看下是什么原因。


    附件: 您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    3 天前
  • 签到天数: 1521 天

    连续签到: 5 天

    [LV.Master]测试大本营

    2#
    发表于 2022-3-7 10:28:08 | 只看该作者
    看下链路通不通
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    1 小时前
  • 签到天数: 2819 天

    连续签到: 1 天

    [LV.Master]测试大本营

    3#
    发表于 2022-3-7 18:05:15 | 只看该作者
    先不要查询,先看链接
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-25 08:39 , Processed in 0.071650 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表