- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 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/
如何使用 Morphia 查询最新插入的项目 以及如何获得集合的大小? 最佳答案 这取决于你说的最新是什么意思,但如果你有一个带有创建日期的日期字段,那么你可以这样做: T latest = ds.f
在我现在的初始化代码中,我创建了 1 个 Morphia 实例并使用它来映射所有数据库中的所有类。我注意到,当我调用 ensureIndexes() 时,它将初始化一个空集合并为所有数据库中的所有集合
我是新手,所以开始吧。 试图从 MongoDb 中获取名为“Bob”的用户。 我有: UserData ud = MonConMan.instance().getDb().find(UserData.
长话短说:是否可以使用 Morphia 为第三方库类编写类型转换器? 长话短说:我是 Morphia 的新手。我有一个实体类,其中包含一个类型为 javax.activation.MimeType 的
长话短说:是否可以使用 Morphia 为第 3 方库类编写类型转换器? 长话短说:我是 Morphia 的新手。我有一个实体类,其中包含类型为 javax.activation.MimeType 的
我有一个名为 Project 的 Java 类,其 users ArrayList包含用户对象的属性。 我的项目集合看起来像 [ { "_id" : ObjectId("56410150d
所以这就是导致问题的问题...... 我使用 MongoDB 数据库在 jBoss 上运行这个 javaEE 应用程序。我使用Morphia与mongo进行通信...该应用程序具有以下基本程序组件;
我编写了一个代码,其中包含套件信息,其中嵌入了测试用例信息。我编写了 TestCase.java 和 Suite.java,它们似乎没有错误。但是使用我编写的 MongoMapper.java 我我收
com.google.code.morphia:morphia:jar:1.00-SNAPSHOT 的 POM 丢失,没有可用的依赖信息 scm:svn:svn://morphia.googl
不确定这是否可行,但可以了。 我有一个 MongoDB(使用 morphia 来访问它),其表如下所示(本示例进行了简化,但概念是相同的): { Name : "Robert", NickNames
我对 Morphia 还很陌生。有没有一种简单的方法可以进行求和、平均值、最大值、最小值等操作? 我已经看到了一些“Play”插件,但我不确定它是如何工作的,我想使用普通的 Morphia。 最佳答案
我试图了解如何使用 Morphia 正确注释基类和扩展类。给定以下示例,我应该 @Entity 到 Employee 类还是只是 Developer 类? public abstract class
我想使用 Morphia 将对象存储到 MongoDB。 但是我遇到了很多异常(exception): Exception in thread "main" java.lang.IllegalArgu
我有一个像这样的简单文档: @Entity("users") @Indexes(@Index(fields = {@Field("lastName"), @Field("firstName")}))
在我的Java方法中,我想将一个像这样的复杂查询作为参数传递给我的 MongoDB 集合: {"$or": [{"$and": [{"contextID": "AKKA"}, {"messageID"
Morphia 中以下 Mongo 查询的等价物是什么? db.events.find({ "date": { $gte: ISODate("2001-01-01") , $lt: ISODate("
为了模拟我看到的行为,我编写了这个测试(给出了完全相同的错误): @Test public void testListGenerics() { @Entity class BaseOn
我有一个嵌入式实体列表: @Embedded private List embedded = new ArrayList(); 在此列表中,我想搜索任何具有特定属性(我们称它为 foo)但不是另一个(
我是新手, 假设我有集合“UserData”并且 UserData 中有 1000 个文档。 现在我需要再添加一个名为“position”的字段。 我可以这样做吗?怎么做?或者它是如何工作的? 查看维
使用吗啡,java play 来完成作业。 我有 2 个馆藏,一个部门馆藏和代理馆藏。一个部门有很多代理人。部门类有一个带有注释@Reference 的List。 当我删除代理并尝试获取代理所在的部门
我是一名优秀的程序员,十分优秀!