vendredi 11 septembre 2015

Spring DAO testing

I've heard it's recommended to create separated interfaces for the each DAO classes to make it's test easier. The answer on the question "why?" was about of possibility of substitution the original DAO implementation by test one. But there was no info about how to do that.

So, I'm interested how to do that.

Assuming we've the simple DAO interface:

public interface PersonDao {
    void add();
    ...
}

and the simple as well it's implementation:

@Repository
public class PersonDaoImpl implements PersonDao {

   public void add() {
      // doing something
   }
}

how can we test it like that:

public class PersonDaoTest {

   @Autowire
   FakePersonDao fakePersonDao;

   @Test
   public void addTest() {
      fakePersonDao.add();
   }
}

where's the FakeDao is second "test" implementation of PersonDao interface:

public class FakePersonDaoImpl implements PersonDao {

    public void add() {
        // doing something different
    }
}

hm?

Should we use everywhere @Autowired in combination with @Qualifier or there's a better way?

Aucun commentaire:

Enregistrer un commentaire