I am having trouble with writing unitary test for a service that use a Spring Data repository and Daly generated class.
On this test, I am mocking the Spring Data repository. But inside my repository, I am using a class generated by Daly to get the name of an entity's field. My test fail because, the public static SingularAttribute field on my Daly generated class are not initialized.
This does not happen, when I do a integration test and I load Spring and Hibernate. I am assuming that when Spring start, it make hibernate populating fields on all class annotated by @StaticMetamodel.
I was wondering if there is a simple way to populate those field when my Junit test start without doing it by hand ?
Maybe some code will make my question more clear :
My entity is named PageHits, and it associated daly generated class is PageHits_ :
import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
@Generated(value="Dali", date="2015-10-23T10:07:28.339+0200")
@StaticMetamodel(PageHits.class)
public class PageHits_ {
public static volatile SingularAttribute<PageHits, PagePK> pagePK;
public static volatile SingularAttribute<PageHits, Integer> numHits;
}
This is the method which I am testing on my service :
@Transactional
public List<PageHits> getMostViewedPage(String baseUrl, int num) {
Pageable pageable = new PageRequest(0, num, Sort.Direction.DESC, PageHits_.numHits.getName()); //it fail here because PageHits_.numHits is null
//pageHitsRepository is mocked when running the code
return pageHitsRepository.findByBaseUrl(baseUrl, pageable).getContent();
}