Morphia,Embed Mongo和Spring。地址已在使用中

时间:2015-01-04 03:16:07

标签: mongodb mongodb-java morphia spring-mongo

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

当我只有一个DAO持续存在时,我的测试没有任何问题,但是,在某些情况下我需要使用多个DAO,在这种情况下,我注入的Datasore给我一个问题:addr已经在使用

My Spring Test Database Configuration是这样的:

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

我使用原型范围来解决单例问题,并确保我的模拟数据库在我开始测试时是干净的,之后我停止了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只是为了测试,但我认为可能是一个更好的解决方案

1 个答案:

答案 0 :(得分:2)

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

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