如果项目在" spring-boot:run"下运行,我有以下类按预期工作。 maven指令。
但是现在我尝试创建一些测试用例就像一个好的做法一样,我努力让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文件,那么这个测试用例将会起作用,但这完全是重点,我不想要一个......
有人能帮助我吗?
答案 0 :(得分:0)
原来需要为数据源提供一个唯一的PersistenceUnitName ...显然,当从“spring-boot:run”运行时,某些东西更加自动化并且它是自动创建的。
所以,我刚刚添加了“factoryBean.setPersistenceUnitName(”some-unique-name“);”到每个DataSource
factoryBean.setPackagesToScan("com.vnt.intranet.entities");
factoryBean.setPersistenceUnitName("intranet");