title: MongoDb Factory with spring
body: <p>I want to use MongoDB with Spring. 
  I'm trying to inject a MongoDbFactory in my DAO main class.</p>
  
  <p>I dont want to use MongoTemplate because I need to use the MongoDB-Driver</p>
  
  <p>When I try to run a jUnit Test, for test my DAO class, i get an NullPointerExeption on my factory....</p>
  
  <p>I think it's a problem with my injection.</p>
  
  <p><strong>My app-config :</strong></p>
  
  <pre><code>&lt;bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoFactoryBean"&gt;
      &lt;property name="host" value="127.0.0.1" /&gt;
      &lt;property name="port" value="27017"/&gt;   
  &lt;/bean&gt;
  
  &lt;bean id="mongoDbFactory"
      class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"&gt;
      &lt;constructor-arg name="mongo" ref="mongoFactoryBean" /&gt;
      &lt;constructor-arg name="databaseName" value="agence_voyage" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dao" class="dao.daoImpl.DaoImpl"&gt;
      &lt;property name="mongoFactory" ref="mongoDbFactory" /&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p><strong>my DAO class :</strong></p>
  
  <pre><code>public class DaoImpl implements Dao {
   private MongoDbFactory mongoFactory;
   private DB db;
  
   @Required
   public void setMongoFactory(MongoDbFactory myMongoFactory){
       this.mongoFactory= myMongoFactory;
   }
  
  //TODO development mongodb://localhost
  public void connect() throws UnknownHostException {
      try{
          this.db = mongoFactory.getDb("agence_voyage"); //NullPointer here
      }
      catch(DataAccessException d){
          System.out.println(d);
      }
  }
  
  public int getVoyageCount(String collection) {
      DBCollection col = db.getCollection(collection);
      return (int) col.count();
  }
  }
  </code></pre>
  
  <p><strong>And then my little test :</strong></p>
  
  <pre><code>public class TestDao {
  
  @Test
  public void test() {
      Dao test = new DaoImpl();
      try {
          test.connect();
      } catch (UnknownHostException e) {
          System.out.println(e);
          assertTrue(false);
      }
      assertTrue(test.getVoyageCount("voyage")== 1);
  }
  
  }
  </code></pre>
  
  <p>Have you got any solution ?
  I'm sure it's an idiot error but I don't find it !</p>
  
  <p>Thanks in advance !</p>