- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用嵌入的 MongoDB 在我的应用程序 SpringBoot 中进行测试,但是当我运行我的应用程序时出现错误,这是我第一次,我从来没有使用 Springboot、mongodb、junit 进行测试...
首先,我在 pom.xml 中添加此依赖项:
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>1.50.2</version>
<scope>runtime</scope>
</dependency>
有了这个依赖项,我将嵌入 mongoDB(但我不知道这个版本是否正确......)
然后我创建了两个java类:
1°
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
@EnableAutoConfiguration
public class TestApplicationConfiguration {
@Bean
GenerarCsv service() {
return new GenerarCsvImpl();
}
}
在这个类中,我有我的接口(interface),我将生成我的实现,现在是我的 2° 类 java:
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
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.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.batch.DesarrolloBatch.model.Documento;
import com.batch.DesarrolloBatch.persistence.DocumentoRepository;
import com.batch.DesarrolloBatch.service.interfaces.GenerarCsv;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.IMongodConfig;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes=TestApplicationConfiguration.class)
public class GenerarDocumentoTest {
@Autowired
private GenerarCsv service;
@Autowired
private DocumentoRepository repository;
private static final String DATABASE_NAME = "documento";
private MongodExecutable mongodExe;
private MongodProcess mongod;
private MongoClient mongo;
@Before
public void beforeEach() throws Exception {
MongodStarter starter = MongodStarter.getDefaultInstance();
String bindIp = "localhost";
int port = 27017;
IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
.build();
this.mongodExe = starter.prepare(mongodConfig);
this.mongod = mongodExe.start();
this.mongo = new MongoClient(bindIp, port);
}
@After
public void afterEach() throws Exception {
if (this.mongod != null) {
this.mongod.stop();
this.mongodExe.stop();
}
}
@Test
public void shouldCreateNewObjectInEmbeddedMongoDb() {
// given
MongoDatabase db = mongo.getDatabase(DATABASE_NAME);
db.createCollection("documento");
MongoCollection<Documento> col = db.getCollection("documento", Documento.class);
// when
Documento doc = new Documento();
doc.set_id("999999999999999999999999");
col.insertOne(doc);
// then
assertEquals(1L, col.countDocuments());
}
}
好的,在这个类中,我在内存中使用我的 mongoDB 创建了 conexión,然后我尝试在 mongDB 中插入一个对象类型 documento,最后我询问在我的 mongoDB 中是否有 1 个注册,但是,当我尝试运行时出现错误此应用程序 ->
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.MongodExecutable]: Factory method 'embeddedMongoServer' threw exception; nested exception is de.flapdoodle.embed.process.exceptions.DistributionException: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.5.5.zip
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 26 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.MongodExecutable]: Factory method 'embeddedMongoServer' threw exception; nested exception is de.flapdoodle.embed.process.exceptions.DistributionException: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.5.5.zip
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
Caused by: de.flapdoodle.embed.process.exceptions.DistributionException: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.5.5.zip
at de.flapdoodle.embed.process.runtime.Starter.prepare(Starter.java:68) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
at org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.embeddedMongoServer(EmbeddedMongoAutoConfiguration.java:116) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration$$EnhancerBySpringCGLIB$$67c9dfd6.CGLIB$embeddedMongoServer$1(<generated>) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration$$EnhancerBySpringCGLIB$$67c9dfd6$$FastClassBySpringCGLIB$$8d580d09.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration$$EnhancerBySpringCGLIB$$67c9dfd6.embeddedMongoServer(<generated>) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
... 26 more
Caused by: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.5.5.zip
at de.flapdoodle.embed.process.store.Downloader.downloadInputStream(Downloader.java:131) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
at de.flapdoodle.embed.process.store.Downloader.download(Downloader.java:69) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
at de.flapdoodle.embed.process.store.ArtifactStore.checkDistribution(ArtifactStore.java:66) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
at de.flapdoodle.embed.process.store.ExtractedArtifactStore.checkDistribution(ExtractedArtifactStore.java:60) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
at de.flapdoodle.embed.process.runtime.Starter.prepare(Starter.java:55) ~[de.flapdoodle.embed.process-1.50.1.jar:?]
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_171]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_171]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_171]
如果我在导航中复制这些链接“htpp....zip”,我可以下载这些.zip,那么我不知道为什么SpringBoot不能下载.zip。
最后,我想记住我不知道这样做,我复制并粘贴了谷歌中的其他帖子。如果有人知道如何在 Springboot 中使用 mongoDB 进行 Junit 测试是我的最终目标。这是我第一次。谢谢。
编辑:
我改变了我的依赖:
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
现在,当我运行我的应用程序时,她正在下载,但始终是 0% 从未启动...
019-06-12 09:45:02.242 INFO 14372 --- [ main] c.b.D.GenerarDocumentoTest : The following profiles are active: test
2019-06-12 09:45:03.108 INFO 14372 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-06-12 09:45:03.282 INFO 14372 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 166ms. Found 1 repository interfaces.
2019-06-12 09:45:04.005 INFO 14372 --- [ main] o.s.b.a.m.e.EmbeddedMongo : Download Version{3.5.5}:Windows:B64 : starting...
2019-06-12 09:45:06.621 INFO 14372 --- [ main] o.s.b.a.m.e.EmbeddedMongo : Download Version{3.5.5}:Windows:B64 : DownloadSize: 245893992
2019-06-12 09:45:08.809 INFO 14372 --- [ main] o.s.b.a.m.e.EmbeddedMongo : Download Version{3.5.5}:Windows:B64 : 0 %
最佳答案
方法 de.flapdoodle.embed.process.store.LocalArtifactStore.getArtifact(IDownloadConfig runtime, Distribution distribution) 检查用户的本地文件夹以查看本地是否已存在分发文件。如果该方法尚不存在,则返回 null。
然后,调用方法将尝试下载文件,超时,并抛出 de.flapdoodle.embed.process.distribution.Distributon.DistributionException 包装 IO 异常。
这会中断 Spring Boot 运行时注入(inject),并且所有依赖于此的测试都会失败。
下载文件 http://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.5.5.zip
在 Windows 上,将其复制到您的用户文件夹,如下所示:c:\users\your_user_name\.embeddedmongo\win32\mongodb-win32-x86_64-3.5.5.zip
现在运行您的测试,它将提取嵌入式服务器可执行文件c:\users\your_user_name\.embeddedmongo\extracted\Windows-B64--3.5.5\extractmongod.exe
您的测试现在应该可以正常运行。
关于java - JUnit 测试 MongoDB SpringBoot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56555933/
本着this question from JUnit 3 to JUnit 4的精神, 是否有任何正则表达式列表到 有效地从 junit 4 API 迁移到 junit 5 API ,不管代码大小?
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
当尝试在JUNITTEST的内存数据库中使用derby时,出现以下异常。 java.sql.SQLNonTransientConnectionException: Database 'memory:t
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to run Junit testcases from command line? 如何在 Linu
可以将 Junitperf 与 junit4 一起使用吗?我有一个带有多个测试的简单 Junit4 测试类,我想对该类的单个测试进行 TimedTest。我该怎么做? 更清楚地说,我的 Junit4
我想将 JUnit 4 测试添加到使用 JUnit 3 测试套件(“AllTests”)来组织测试的遗留项目中。 测试已经用 JUnit 4 执行了,所以我知道 JUnit 4 测试在原则上是有效的。
我正在将我的代码库从 junit4 迁移到 junit5。我在我的测试用例中使用了 mockito。下面是我用于依赖项的不同版本。 5.2.0 1.2.0 1.10.19 or
我刚刚使用 qunit-reporter-junit 生成了以下 XML: 但是当我运行它时,我在以下位置找到了 xsd:http
我已经编写了一个自定义 JUnit 运行器,我希望它成为 eclipse 插件的一部分,该插件将使用该运行器启动测试,而无需将 @RunWith 注释应用于该类。我已经设法使用 org.eclipse
我发现我的Sonar实例5.1或5.1.1(带有最新的声纳运行器2.x)停止在项目的仪表板上显示部分单元测试信息(单元测试小部件)。 我拥有的属性是(在Gradle的sonarRunner> sona
我有一个 JUnit 测试。但是当我使用“Run as -> JUnit”时它会成功,而当我使用“Cover as -> JUnit”时它会失败。这是为什么?代码确实有问题。在代码中,我使用了一些遗留
这个问题在这里已经有了答案: How to test code dependent on environment variables using JUnit? (20 个答案) 关闭 8 年前。 我
当我们的临时服务器因每周维护而停机时,我们有许多集成测试失败。当临时服务器关闭时,我们会发送一个特定的响应,我可以在集成测试中检测到该响应。当我得到这个响应而不是测试失败时,我想知道是否可以跳过/忽略
我需要测试一个程序,它首先预处理一些数据,然后使用这些预处理过的数据计算几个不同的结果——为每个计算编写单独的测试是有意义的。 官方 JUnit 政策似乎是我应该在每次计算测试之前运行预处理。 我如何
JUnit 是否可以为每个测试方法添加描述文本,以便描述文本稍后出现在surefire/failsave xml 报告中!? 背景:我在受监管的环境中工作,必须编写大量文档、测试规范和测试报告。 JU
当 JUnit 中的断言失败时,我想做一些“自己的事情”。我想要这个: public class MyAssert extends org.junit.Assert { // @Overrid
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我想将参数从运行配置传递给我的 JUnit 测试。我如何到达 JUnits 的主要方法来访问这些参数?有谁知道如何做到这一点? 谢谢 最佳答案 您可以使用 -D 系统属性运行单元测试,并使用 Syst
我是一名优秀的程序员,十分优秀!