0
如果項目在「spring-boot:run」maven指令下運行,我有以下類可按預期工作。沒有XML Spring-data-jpa數據源和Junit測試(ManagedType失敗)
但現在我試圖創建一些測試情況一樣好做法,我努力使JUnit的理解我的XML-較少的配置...以下是相關代碼:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "emfIntranet", transactionManagerRef = "tmIntranet", basePackages = {"com.vnt.intranet.repositories"})
@ConfigurationProperties(prefix = "databases.sistemas")
public class IntranetPersistence {
private String address;
private String schema;
private String username;
private String password;
private String eclipselinklog;
private Boolean sqllog;
@Primary
@Bean(name = "dsIntranet")
DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:postgresql://" + address + "/" + schema);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setInitialSize(3);
dataSource.setMaxIdle(3);
dataSource.setMaxTotal(10);
return dataSource;
}
private EclipseLinkJpaVendorAdapter getEclipseLinkJpaVendorAdapter() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.PostgreSQLPlatform");
vendorAdapter.setShowSql(sqllog);
return vendorAdapter;
}
@Primary
@Bean(name = "emfIntranet")
EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan("com.vnt.intranet.entities");
Properties jpaProperties = new Properties();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.logging.level", eclipselinklog);
factoryBean.setJpaProperties(jpaProperties);
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
@Primary
@Bean(name = "tmIntranet")
PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory());
return transactionManager;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEclipselinklog() {
return eclipselinklog;
}
public void setEclipselinklog(String eclipselinklog) {
this.eclipselinklog = eclipselinklog;
}
public Boolean getSqllog() {
return sqllog;
}
public void setSqllog(Boolean sqllog) {
this.sqllog = sqllog;
}
}
測試用例:
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {Application.class}, initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ServiceOrderRepositoryTest {
private static Logger logger = LoggerFactory.getLogger(ServiceOrderRepositoryTest.class);
@Autowired
ServiceOrderRepository serviceOrderRepository;
@Test
public void serviceOrderTest() {
ServiceOrder serviceOrderList = serviceOrderRepository.findOne(51152L);
logger.debug("{}", serviceOrderList);
assertThat(serviceOrderList, is(not(nullValue())));
}
}
錯誤:
Caused by: java.lang.IllegalArgumentException: No [ManagedType] was found for the key class [com.vnt.mkdata.entities.ServiceOrder] in the Metamodel - please verify that the [Managed] class was referenced in persistence.xml using a specific <class>com.vnt.mkdata.entities.ServiceOrder</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
這測試用例會的工作,當然,如果我創建一個persistence.xml文件,但這正是點,我不想要一個...
任何人都可以幫助我嗎?