51Testing软件测试论坛

标题: 项目通过Maven管理,已配置Mongodb连接,测试dao接口报错 [打印本页]

作者: 测试积点老人    时间: 2022-3-4 13:26
标题: 项目通过Maven管理,已配置Mongodb连接,测试dao接口报错

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

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

[attach]136716[/attach]

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接口,看是否能查询出数据库里面的轮播图的图片路径、开始时间、过期时间这些的。不知道是哪里出错了,希望能帮忙看下是什么原因。



作者: qqq911    时间: 2022-3-7 10:28
看下链路通不通
作者: jingzizx    时间: 2022-3-7 18:05
先不要查询,先看链接




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2