1

我有这个方法:

@Transactional
@Service("vacancyService")
public class VacancyService {

   public boolean delete(Integer id) {
        Vacancy vacancy = vacancyDao.findById(id);
        return vacancy != null && vacancyDao.remove(vacancy);
    }
  ...
}

我想测试上述方法。

实现 vacancyDao.remove(vacancy)

  public boolean remove(Vacancy vacancy) throws HibernateException {
            Session session = sessionFactory.getCurrentSession();
            if (vacancy == null) {
                return false;
            }

            int result = session.createQuery("delete from Vacancy where id = :id")
                    .setInteger("id", vacancy.getId()).executeUpdate();
            return result > 0;

        }

我的测试课:

@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>

数据.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>

如果您需要更多详细信息,我将其发布在这里。

4

4 回答 4

2

由于提供了服务类单元测试,这里是单元测试你的 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);


}



}
于 2016-07-21T07:13:16.740 回答
1

为了使其成为单元测试而不是集成测试,我将完全删除 spring 测试注释并通过像Mockito这样的模拟对象框架“注入”模拟服务。然后你需要几个简单的单元测试来覆盖几个条件。我会保存 DAO 层的集成测试。

作为第一个剪辑(省略了 Mockito 静态导入):

@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));
    }
于 2013-09-26T18:10:26.390 回答
0

您可以创建 Vacancy 对象,通过 Id 查询它,它应该返回值。然后删除它,然后查询id,如果id不存在你的测试应该通过。

但这不会是纯粹的单元测试,因为它假设“创建”和“按 id 查询”工作正常,如果它们有错误,这个测试也会失败,

您可以在 JUnit 中使用 Assume Tag,以确保此测试独立于通过 id 创建和查询。

另一个选项是使用带有 DBUnit 的 unitils,检查:http ://www.unitils.org/tutorial-database.html

于 2013-09-26T14:17:17.920 回答
0

编写您的测试类,如下所示:

@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));
    }

}

默认情况下,框架将为每个测试创建和回滚事务。即使测试方法在为测试管理的事务中运行时删除了选定表的内容,事务也会默认回滚,并且数据库将返回到执行测试之前的状态。

于 2013-09-26T14:27:04.673 回答