title: Morphia, Embed Mongo and Spring. Address already in use
body: <p>I am trying use MongoDB, Morphia and Spring and test it, so I started use Embedded Mongo.</p>
  
  <p>When I had only one DAO to persist I did not had any problem with my tests, however, in some cases I needed use more than one DAO, and in that cases my injected Datasore give me an problem: addr already in use.</p>
  
  <p>My Spring Test Database Configuration is this:</p>
  
  <pre><code>@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);
  }
  }
  </code></pre>
  
  <p>And my DAO classes are similar to that</p>
  
  <pre><code>@Repository
  public class EventDAO {
  
  private final BasicDAO&lt;Event, ObjectId&gt; basicDAO;
  
  @Autowired
  public EventDAO(final Datastore datastore) {
      this.basicDAO = new BasicDAO&lt;&gt;(Event.class, datastore);
  }
  ...
  }
  </code></pre>
  
  <p>My test class is similar to that:</p>
  
  <pre><code>@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();
  }
  ...
  }
  </code></pre>
  
  <p>I use prototype scope to solve problem with singleton and make sure that my mock database is clean when I start my test, after that I stop mongod process and mongod executable.</p>
  
  <p>However since I need use more than one DAO I receive that error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I know what the error means, I just do not know how can I design my Configuration to solve that. As last option I am considering install a localhost MongoDB just for tests, however I think could be a better solution</p>