Question

I'm trying to test a Singleton Startup Bean loading Countries from a Database using arquillian-persistence-impl 1.0.0.ALPHA6.

If I run the test class and try to access one of the test data tuples provided through @UsingDataSet everything works fine.

Here is what the Arquillian test class looks like

@RunWith(Arquillian.class)
public class CountryDAOTest {

    @Inject
    CountryDAO dao;

    @Produces
    @PersistenceContext(unitName="test-pu")
    EntityManager em;

    @Inject
    UserTransaction ut;

    @Deployment
    public static JavaArchive createDeployment() {

        JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                    .addClass(CountryDAO.class)
                    .addClass(UserTransaction.class)
                    .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                    .addAsManifestResource("jbossas-ds.xml")
                    .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
                 System.out.println(jar.toString(true));
                 return jar;
    }

    @Test
    @UsingDataSet("db-data/countryDAOTest-country.yml")
    @ShouldMatchDataSet("db-data/countryDAOTest-country-verify.yml")
    public void getCountryFromDAO(){    

        TypedQuery<Country> query = em.createQuery("FROM Country", Country.class);
        List<Country> newCountriesList = query.getResultList();
        System.out.println("LOADED COUNTRIES IN TEST CLASS: "+newCountriesList.size());/*this works fine and returns 2 elements */

        List<Country> daoCountry = dao.getCountries();
        System.out.println("LOADED COUNTRIES FROM DAO: "+daoCountry.size()); /*this returns 0 elements */

    }
}

If I try to call the database through my Singleton Startup CountryDAO It doesn't find any data in the test database.

@Startup
@Singleton
public class CountryDAO {

    @Inject
    private EntityManager entityManager;
    private List<Country> countries;

    @PostConstruct
    public void init(){
        TypedQuery<Country> query = entityManager.createQuery("FROM Country", Country.class);
        List<Country> newCountriesList = query.getResultList();

        System.out.println("LOADED COUNTRIES ON POSTCONSTRUCT:"+newCountriesList.size()); /* returns 0 elements */

        countries = Collections.unmodifiableList(newCountriesList);
    }

    public synchronized List<Country> getCountries(){
        return countries;
    }
}

I also tried using @PersistenceContext annotation on my dao entityManager with the same result.

I'm using jboss AS 7.1.1 as test container.

Any ideas why my DAO entityManager doesn't find my test data?

Was it helpful?

Solution

Thanks to John Ament's comment this is how I fixed my Problem:

I'm re-invoking the @PostConstruct annotated init() method of my DAO bean before the test is executed. So I added

@Before
public void before(){
    dao.init();
}

to my CountryDAOTest Junit test class.


The problem in detail is that the @PostConstruct annotated init() method is invoked when CountryDAO() is injected in CountryDAOTest.

The insert of my test data happens before the invocation of getCountryFromDAO() when CountryDAO is already injected in CountryDAOTest and init() already called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top