如何测试这种方法对吗?(junit)

时间:2013-09-26 13:32:54

标签: 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);
    }
  ...
}

我想测试上面的方法。

实现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>

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>

如果您需要更多详细信息,请在此处发布。

4 个答案:

答案 0 :(得分: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);


}



}

答案 1 :(得分:1)

为了使它成为单元测试而不是集成测试,我将完全删除弹簧测试注释并注入&#34;注入&#34;通过像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));
    }

答案 2 :(得分:0)

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

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

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

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

答案 3 :(得分: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));
    }

}

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