After some research, specially follow 'ignore-some-classes-while-scanning-packagestoscan' idea:
// add only required enitites from a libray
localContainerEntityManagerFactoryBean.setPersistenceUnitPostProcessors(new PersistenceUnitPostProcessor() {
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo persistenceUnit) {
persistenceUnit.addManagedClassName("my.package.po.EntityA");
}
});
I make some custom code encapsulation to simplify my unit-test:
package my.package.dao;
import tech.simter.test.jpa.EntityScan;
import tech.simter.test.jpa.JpaTestConfiguration;
...
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {EntityADaoJpaImpl.class, JpaTestConfiguration.class})
@DataJpaTest
@EntityScan({EntityA.class})
public class EntityADaoJpaImplTest {
@Inject
private TestEntityManager entityManager;
@Inject
private EntityADaoJpaImpl dao;
//...
}
It totally resolve my problem. And the code encapsulation idea comes from the implementation of spring-boot-autoconfigure org.springframework.boot.autoconfigure.domain.EntityScan
class.
My source code hosts here. It is on github.