2015-01-04 33 views
2

我在嘗試使用MongoDB,Morphia和Spring並測試它,所以我開始使用嵌入式Mongo。Morphia,嵌入Mongo和Spring。地址已經在使用

當我只有一個DAO堅持我並沒有任何問題,我的測試,但是,在某些情況下,我需要使用一個以上的DAO,並在這情況下,我的注射Datasore給我一個問題:地址已經在使用。

我的春天測試數據庫配置是這樣的:

@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只是爲了測試,但我認爲可能是一個更好的解決方案

回答

相關問題