Morphia,嵌入 Mongo 和 Spring。地址已被使用

Morphia, Embed Mongo and Spring. Address already in use

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

当我只有一个 DAO 可以坚持时,我的测试没有任何问题,但是,在某些情况下我需要使用多个 DAO,在这种情况下,我注入的 Datasore 给我一个问题:addr already正在使用中。

我的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 classes 与那个相似

@Repository
public class EventDAO {

private final BasicDAO<Event, ObjectId> basicDAO;

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

我的测试class与此类似:

@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();
}
...
}

我使用原型范围来解决单例问题,并确保我的模拟数据库在我开始测试时是干净的,之后我停止 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

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

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

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