Spring Data:服务层单元测试 [英] Spring Data: Service layer unit testing

查看:231
本文介绍了Spring Data:服务层单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我在进行单元测试时遇到了麻烦。一个问题是,只需进行集成测试就可以更快地编写并测试组件实际协同工作。单元测试新颖的算法左右似乎更容易。单元测试服务类它只是感觉错误和无用。

In my project I'm having trouble doing unit testing. One issue is that just doing an integration test is much faster to write and also tests that the components actually work together. Unit testing novel "algorithms" or so seems much easier. Unit Testing service classes it just feels wrong and useless.

我正在使用mockito来模拟spring数据存储库(以及数据库访问)。问题是如果我告诉模拟的存储库在方法调用getById上返回实体A,它显然会返回它,服务也将返回它。是的,该服务做了一些额外的东西,但非常小的事情,如加载惰性集合(来自休眠)。显然我在单元测试中没有任何惰性集合(代理)。

I'm using mockito to mock spring data repository (and hence DB access). The thing is if i tell the mocked repository to return entity A on method call getById it will obviously return that and the service will return it too. Yes, the service does some extra stuff, but very minor things, like load lazy collections (from hibernate). Obviously I don't have any lazy collections (proxies) in a unit test.

示例:

@Test
public void testGetById() {
    System.out.println("getById");
    TestCompound expResult = new TestCompound(id, "Test Compound", "9999-99-9", null, null, null);

    TestCompoundRepository mockedRepository = mock(TestCompoundRepository.class);
    when(mockedRepository.findOne(id)).thenReturn(expResult);

    ReflectionTestUtils.setField(testCompoundService, "testCompoundRepository",
            mockedRepository, TestCompoundRepository.class);

    TestCompound result = testCompoundService.getById(id);
    assertEquals(expResult, result);
}

万岁,其余成功。多么惊喜!不是真的没有。

hooray, the rest succeeds. What a surprise! Not really no.

有人可以向我解释我做错了什么吗?或者这样一个测试的重点是什么?我的意思是我告诉返回expResult然后它返回。哇。多么惊喜!感觉就像我在测试mockito是否有效而不是我的服务。

Can some one explain to me what I'm doing wrong? Or else what the point of such a test is? I mean I tell to return expResult and then it is returned. Wow. What a surprise! Feels like I'm testing if mockito works and not my Service.

编辑:

唯一的好处我看看是否有一些是愚蠢的错误发生像在那里留下一个不需要的行,将返回值设置为null或类似的愚蠢。这种情况将由单元测试捕获。 奖励 - 努力比例看起来还不错吗?

The only benefit I see if some were stupid error happens like leaving an unwanted line there that sets return value to null or something similar stupid. Such cases would be caught by the unit test. Still the "reward-effort" ratio seems bad?

推荐答案

问题可能有点旧,但我会给出答案以防万一有人偶然发现。

Question might be a bit old but I will put an answer in case someone stumbles across.


  • 我正在使用Mockito和JUnit。

  • AccountRepository是一个普通的春天数据存储库扩展JPARepository。

  • 帐户是一个普通的JPA实体。

测试你的服务并模拟Spring Data存储库,你需要如下所示。

To test your services and mock Spring Data repositories, you need something like below.

package foo.bar.service.impl;

import foo.bar.data.entity.Account;
import foo.bar.data.repository.AccountRepository;
import foo.bar.service.AccountService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AccountServiceImplTest {

    @Mock
    private static AccountRepository accountRepository;

    @InjectMocks
    private static AccountService accountService = new AccountServiceImpl();

    private Account account;

    @Test
    public void testFindAccount() {

        Integer accountId = new Integer(1);

        account = new Account();
        account.setId(accountId);
        account.setName("Account name");
        account.setCode("Accont code");
        account.setDescription("Account description");

        Mockito.when(accountRepository.findOne(accountId)).thenReturn(account);

        Account retrivedAccount = accountService.findAccount(accountId);

        Assert.assertEquals(account, retrivedAccount);

    }

}

这篇关于Spring Data:服务层单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆