我有三个实体(每个字段都有getter和setter):
public class IndicatorSet {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NaturalId
@Column(unique=true, nullable=false)
private String code;
@Column(nullable=false)
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(
name = "indicator_set_indicators",
joinColumns = @JoinColumn(name = "indicator_set_id"),
inverseJoinColumns = @JoinColumn(name = "indicator_id")
)
private List<Indicator> indicators;
}
public class Indicator {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(nullable=false, unique=true)
private String code;
}
public class IndicatorSetIndicator {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name="indicator_id", nullable=false)
private Indicator indicator;
@ManyToOne
@JoinColumn(name="indicator_set_id", nullable=false)
private IndicatorSet indicatorSet;
}
我在数据库中放入一个IndicatorSet,Indicators并将其与我的服务器上的IndicatorSetIndicator表连接,并在junit test中运行以下代码。
IndicatorSet set = indicatorSetDAO.get(idOfSet);
List<Indicator> indicators = set.getIndicators();
当我在服务器上调用此代码时,它可以工作,我在列表中看到我的指标。 当我尝试在junit测试中调用此代码时,我在list null引用中看到。 这很奇怪。
当我在junit测试中调用以下代码时,它工作正常,指示符出现在列表中:
IndicatorSet set = indicatorSetDAO.get(idOfSet);
List<Indicator> indicators = indicatorSetIndicatorDAO.getIndicatorsBySet(set);
DAO使用Criteria API获取指标列表。 通过测试在课堂上有这组注释:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:TestContext.xml")
@Transactional
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SessionRequestTestExecutionListener.class })
TestContext.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans [schemes]>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="org.example.monitoring" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<jdbc:embedded-database id="dataSource" type="HSQL" />
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
<property name="contexts" value="test" />
<!-- <property name="dropFirst" value="true" /> -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name='packagesToScan' value='pro.sisit.etalon.monitoring.entities' />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="request">
<bean class="org.springframework.web.context.request.RequestScope" />
</entry>
<entry key="session">
<bean class="org.springframework.web.context.request.SessionScope" />
</entry>
</map>
</property>
</bean>
</beans>
不在测试环境中配置Hibernate:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/monitoring" />
</bean>
<bean id='sessionFactory'
class='org.springframework.orm.hibernate4.LocalSessionFactoryBean'>
<property name='dataSource' ref='dataSource' />
<property name='packagesToScan' value='pro.sisit.etalon.monitoring.entities'/>
<property name='hibernateProperties'>
<props>
<prop key='hibernate.dialect'>org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key='hibernate.show_sql'>false</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
如何让我的代码在JUint测试中运行?