我正在尝试使用 MongoDB、Morphia 和 Spring 并对其进行测试,所以我开始使用 Embedded Mongo。
当我只有一个 DAO 可以坚持时,我的测试没有任何问题,但是,在某些情况下,我需要使用多个 DAO,在这种情况下,我注入的 Datasore 给了我一个问题:addr 已经在使用中。
我的 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();
}
...
}
我使用原型范围来解决单例问题,并确保在我开始测试时我的模拟数据库是干净的,然后我停止 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 仅用于测试,但我认为可能是一个更好的解决方案