gpt4 book ai didi

mongodb - Morphia、嵌入 Mongo 和 Spring。地址已被使用

转载 作者:可可西里 更新时间:2023-11-01 10:29:55 26 4
gpt4 key购买 nike

我正在尝试使用 MongoDB、Morphia 和 Spring 并对其进行测试,所以我开始使用 Embedded Mongo。

当我只有一个 DAO 可以持久化时,我的测试没有任何问题,但是,在某些情况下我需要使用多个 DAO,在这种情况下,我注入(inject)的 Datasore 给我带来了一个问题:addr already in use .

我的Spring测试数据库配置是这样的:

@Configuration
public class DatabaseMockConfig {

private static final int PORT = 12345;

private MongodConfigBuilder configBuilder;

private MongodExecutable mongodExecutable;

private MongodProcess mongodProcess;


@Bean
@Scope("prototype")
public MongodExecutable getMongodExecutable() {
return this.mongodExecutable;
}

@Bean
@Scope("prototype")
public MongodProcess mongodProcess() {
return this.mongodProcess;
}

@Bean
public IMongodConfig getMongodConfig() throws UnknownHostException, IOException {

if (this.configBuilder == null) {
configBuilder = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net(PORT, Network.localhostIsIPv6()));
}

return this.configBuilder.build();
}

@Autowired
@Bean
@Scope("prototype")
public Datastore datastore(IMongodConfig mongodConfig) throws IOException {

MongodStarter starter = MongodStarter.getDefaultInstance();

this.mongodExecutable = starter.prepare(mongodConfig);

this.mongodProcess = mongodExecutable.start();

MongoClient mongoClient = new MongoClient("localhost", PORT);

return new Morphia().createDatastore(mongoClient, "morphia");
}

@Autowired
@Bean
@Scope("prototype")
public EventDAO eventDAO(final Datastore datastore) {
return new EventDAO(datastore);
}

@Autowired
@Bean
@Scope("prototype")
public EditionDAO editionDAO(final Datastore datastore) {
return new EditionDAO(datastore);
}
}

我的 DAO 类与此类似

@Repository
public class EventDAO {

private final BasicDAO<Event, ObjectId> basicDAO;

@Autowired
public EventDAO(final Datastore datastore) {
this.basicDAO = new BasicDAO<>(Event.class, datastore);
}
...
}

我的测试类与此类似:

@ContextConfiguration(classes = AppMockConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class EventDAOTest {

@Autowired
private EventDAO eventDAO;

@Autowired
private MongodExecutable mongodExecutable;

@Autowired
private MongodProcess mongodProcess;

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@After
public void tearDown() {

this.mongodProcess.stop();
this.mongodExecutable.stop();
}
...
}

我使用原型(prototype)范围来解决单例问题,并确保我的模拟数据库在我开始测试时是干净的,之后我停止 mongod 进程和 mongod 可执行文件。

但是因为我需要使用多个 DAO,所以我收到了这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'editionDAO' defined in class br.com.mymusicapp.spring.DatabaseMockConfig: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.mongodb.morphia.Datastore]: : 
Error creating bean with name 'datastore' defined in class br.com.mymusicapp.spring.DatabaseMockConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mongodb.morphia.Datastore]:
Factory method 'datastore' threw exception; nested exception is java.io.IOException: Could not start process: ERROR: listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:12345
2015-01-04T01:05:04.128-0200 [initandlisten] ERROR: addr already in use

我知道错误是什么意思,我只是不知道如何设计我的配置来解决这个问题。作为最后一个选项,我正在考虑安装一个 localhost MongoDB 只是为了测试,但我认为这可能是一个更好的解决方案

最佳答案

那是基于embedded mongod by flapdoodle ,对吧?

如果您想并行运行多个测试(可以通过 JUnit 注释进行更改,但并行可能更快),您不能使用单个硬编码端口。相反,让嵌入式进程 select an available port automatically .

关于mongodb - Morphia、嵌入 Mongo 和 Spring。地址已被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27762002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com