Java 如何正确测试此方法?(junit)
我有这个方法:Java 如何正确测试此方法?(junit),java,spring,hibernate,jpa,junit,Java,Spring,Hibernate,Jpa,Junit,我有这个方法: @Transactional @Service("vacancyService") public class VacancyService { public boolean delete(Integer id) { Vacancy vacancy = vacancyDao.findById(id); return vacancy != null && vacancyDao.remove(vacancy); } .
@Transactional
@Service("vacancyService")
public class VacancyService {
public boolean delete(Integer id) {
Vacancy vacancy = vacancyDao.findById(id);
return vacancy != null && vacancyDao.remove(vacancy);
}
...
}
我想测试一下上面的方法
实现vacancyDao.删除(空缺)
:
我的测试班:
@TransactionConfiguration(defaultRollback = false)
@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
public class VacancyServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
@Test
public void testDeleteMethod(){
//what I can write here?
}
我不知道如何测试这个方法。你能帮我吗
更新
在此添加我的配置文件:
BeanConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Включаем опцию использования конфигурационных аннотаций (@Annotation-based configuration)-->
<context:annotation-config />
<context:component-scan base-package="com.epam.hhsystem.jpa" />
<context:component-scan base-package="com.epam.hhsystem.services" />
<!-- Файл с настройками ресурсов для работы с данными (Data Access Resources) -->
<import resource="data.xml" />
</beans>
data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Настраивает управление транзакциями с помощью аннотации @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Менеджер транзакций -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Непосредственно бин dataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
p:url="jdbc:sqlserver://10.16.9.52:1433;databaseName=hhsystemTest;"
p:username="userNew"
p:password="Pass12345" />
<!-- Настройки фабрики сессий Хибернейта -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:test/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create-drop</prop> -->
</props>
</property>
</bean>
</beans>
类路径:test/hibernate.cfg.xml
真的
org.hibernate.dialogue.sqlserverdialogue
UTF-8
如果您需要更多详细信息,我将其发布在这里。您可以创建空缺对象, 按Id查询,它应该返回值。 然后删除它, 然后查询id,如果id不存在,您的测试应该通过 但这不是纯粹的单元测试,因为它假设“Create”和“querybyid”工作正常,如果它们有错误,这个测试也会失败 您可以在JUnit中使用假定标记,以确保此测试独立于按id创建和查询
另一个选项是将unitils与DBUnit一起使用,选中:将测试类编写为:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:BeanConfig.xml",
"classpath:data.xml" })
// This annotation is needed because the configuration does not support creation
// of non-transactional session.
@Transactional
public class VacancyServiceTest {
@Autowired
VacancyService serviceUnderTest;
@Test
public void test() {
assertTrue(serviceUnderTest.delete(1));
}
}
默认情况下,框架将为每个测试创建并回滚一个事务。即使测试方法在为测试管理的事务中运行时删除了所选表的内容,默认情况下事务也会回滚,数据库将返回到执行测试之前的状态。为了使其成为单元测试而不是集成测试,我将完全删除spring测试注释,并通过类似的模拟对象框架“注入”模拟服务。然后您需要几个简单的单元测试来覆盖这些条件。我将保存DAO层的集成测试 作为第一次切割(省略Mockito静态导入):
由于提供了服务类单元测试,下面是对Dao类删除方法进行单元测试的方法
public class vacancyDaoTest {
@InjectMocks
VacancyDao vacancyDao = new VacancyDao();
@Mock
Session session;
@Mock
Query query;
@Mock
SessionFactory sessionFactory;
@BeforeClass
public void setup(){
MockitoAnnotations.initMocks(this);
when(sessionFactory.getSession()).thenReturn(session);
}
@Test
public void testRemove(){
String hql = "delete from Vacancy where id = :id";
when(session.createQuery(hql)).thenReturn(query);
when(query.setInteger(eq("id"), anyInt())).thenReturn(query);
when(query.executeUpdate).thenReturn(1);
int result = vacancyDao.remove(new Vacancy());
verify(session).createQuery(any(String.class));
verify(query).setInteger(eq("id"), anyInt());
verify(query).executeUpdate();
assertNotNull(result);
assertTure(result);
}
}
请发布您的
BeanConfig.xml
。我们是在谈论单元测试delete方法吗?如果是这样,您是否考虑过对您的VacancyDao
类进行存根/模拟?不,我想要测试完整方法我想要测试,如果在执行此方法之后,如果我调用findById,我会得到null您想要测试vacancyDao.findById
或VacancyService.delete
?我想确保在执行此方法之后,数据库中不存在此对象我在回答中编写的测试方法public void test()
,具有assertTrue(serviceUnderTest.delete(1))
。假设您的vacancyDao.remove(空缺)
在成功删除记录时返回true
,如果对象从DB中删除,则assertTrue
测试将成功通过,否则它将失败。因为vacancyDao.remove(空缺)如果无法删除DB记录,并且我们希望结果为true
,则
将返回false
。
@RunWith(MockitoJUnit4Runner.class)
public class VacancyServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
@InjectMocks private VacancyService vacancyService;
@Mock private VacancyDAO vacancyDao;
@Mock Vacancy vacancy;
@Test
public void testDeleteMethod_notFound(){
when(vacancyDao.findById(10).thenReturn(null);
assertFalse(vacancyService.delete(10));
}
@Test
public void testDeleteMethod_foundButRemoveFailed(){
when(vacancyDao.findById(10).thenReturn(mock(Vacancy.class));
when(vacancyDao.remove(vacancy).thenReturn(false);
assertFalse(vacancyService.delete(10));
}
@Test
public void testDeleteMethod_success(){
when(vacancyDao.findById(10).thenReturn(mock(Vacancy.class));
when(vacancyDao.remove(vacancy).thenReturn(true);
assertTrue(vacancyService.delete(10));
}
public class vacancyDaoTest {
@InjectMocks
VacancyDao vacancyDao = new VacancyDao();
@Mock
Session session;
@Mock
Query query;
@Mock
SessionFactory sessionFactory;
@BeforeClass
public void setup(){
MockitoAnnotations.initMocks(this);
when(sessionFactory.getSession()).thenReturn(session);
}
@Test
public void testRemove(){
String hql = "delete from Vacancy where id = :id";
when(session.createQuery(hql)).thenReturn(query);
when(query.setInteger(eq("id"), anyInt())).thenReturn(query);
when(query.executeUpdate).thenReturn(1);
int result = vacancyDao.remove(new Vacancy());
verify(session).createQuery(any(String.class));
verify(query).setInteger(eq("id"), anyInt());
verify(query).executeUpdate();
assertNotNull(result);
assertTure(result);
}
}