Java 如何正确模拟Hibernate会话获取(类<;T>;entityType,可序列化id)
我正在用jUnit4和Mockito对Hibernate DAO层进行单元测试,在模拟Java 如何正确模拟Hibernate会话获取(类<;T>;entityType,可序列化id),java,spring,hibernate,unit-testing,mockito,Java,Spring,Hibernate,Unit Testing,Mockito,我正在用jUnit4和Mockito对Hibernate DAO层进行单元测试,在模拟get(Class entityType,Serializable id)时遇到问题 我一路上没有错误,唯一的问题是我的断言失败了,因为actualAddress是null // imports here @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.
get(Class entityType,Serializable id)
时遇到问题
我一路上没有错误,唯一的问题是我的断言失败了,因为actualAddress
是null
// imports here
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AddressDAOImplTest {
@Autowired
@InjectMocks
private AddressDAO addressDAO;
@Mock
private SessionFactory sessionFactory;
@Mock
private Session session;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getAddress() {
Address expectedAddress = getTestAddress(1);
Mockito
.doReturn(session)
.when(sessionFactory)
.openSession();
Mockito
.doReturn(expectedAddress)
.when(session)
.get(Matchers.eq(Address.class), Matchers.anyInt()); // doesn't match
Mockito
.doNothing()
.when(session)
.close();
Address actualAddress = addressDAO.getAddress(1);
Assert.assertEquals(expectedAddress, actualAddress);
}
}
在某个地方:
public Address getAddress(Integer id) {
Session session = sessionFactory.openSession();
Address address = null;
try {
address = session.get(Address.class, id); // returns null as it can't match the mocked method
} catch (Exception e) {
// some logging here :)
} finally {
session.close();
return address;
}
}
会话工厂设置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.webservices.models"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
据我所知,get(Matchers.eq(Address.class),Matchers.anyInt())
与实际方法不匹配,这导致返回值为null
我非常感谢你的帮助 尝试
Matchers.any(Address.class)
@Deadpool当我这样做时,我的IDE正在抱怨无法解析方法'get(T,int)
。我需要显式地将一个类类型作为它的第一个参数传递给它。只需说Matchers.any()
@Deadpool我不需要any()
,我想精确地说:)我会说只要试着让它工作,然后你就可以精确地接近Matchers.any(Address.class)
@Deadpool当我这样做时,我的IDE正在抱怨无法解析方法'get(T,int)
。我需要显式地将一个类类型作为它的第一个参数传递给它。只需说匹配器。any()
@Deadpool我不需要any()
,我想精确地说:)我会说只要试着让它工作,然后你就可以精确地接近它
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:orders;DB_CLOSE_DELAY=-1
jdbc.username=
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create-drop
hibernate.show_sql=true