Mockito:如何通过模拟测试我的服务?

发布于 2024-08-21 20:16:47 字数 448 浏览 3 评论 0原文

我是模拟测试新手。

我想测试我的服务方法 CorrectionService. CorrectPerson(Long personId)。 实现尚未编写,但这就是它将执行的操作:

CorrectionService 将调用 AddressDAO 的方法,该方法将删除一些 Adress一个有。一个有许多地址

不确定我的CorrectionServiceTest.testCorrectPerson的基本结构是什么。

另外请不要确认在这个测试中我不需要测试地址是否被实际删除(应该在 AddressDaoTest 中完成),只需要测试 DAO 方法被调用。

谢谢

I'm new to mock testing.

I want to test my Service method CorrectionService.correctPerson(Long personId).
The implementation is not yet written but this it what it will do:

CorrectionService will call a method of AddressDAO that will remove some of the Adress that a Person has. One Person has Many Addresses

I'm not sure what the basic structure must be of my CorrectionServiceTest.testCorrectPerson.

Also please do/not confirm that in this test i do not need to test if the adresses are actually deleted (should be done in a AddressDaoTest), Only that the DAO method was being called.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-08-28 20:16:47

更干净的版本:

@RunWith(MockitoJUnitRunner.class)
public class CorrectionServiceTest {

    private static final Long VALID_ID = 123L;

    @Mock
    AddressDao addressDao;

    @InjectMocks
    private CorrectionService correctionService;

    @Test
    public void shouldCallDeleteAddress() { 
        //when
        correctionService.correct(VALID_ID);
        //then
        verify(addressDao).deleteAddress(VALID_ID);
    }
}

Cleaner version:

@RunWith(MockitoJUnitRunner.class)
public class CorrectionServiceTest {

    private static final Long VALID_ID = 123L;

    @Mock
    AddressDao addressDao;

    @InjectMocks
    private CorrectionService correctionService;

    @Test
    public void shouldCallDeleteAddress() { 
        //when
        correctionService.correct(VALID_ID);
        //then
        verify(addressDao).deleteAddress(VALID_ID);
    }
}
输什么也不输骨气 2024-08-28 20:16:47

CorrectionService 类的简化版本(为了简单起见,删除了可见性修饰符)。

class CorrectionService {

   AddressDao addressDao;

   CorrectionService(AddressDao addressDao) {
       this.addressDao;
   }

   void correctPerson(Long personId) {
       //Do some stuff with the addressDao here...
   }

}

在你的测试中:

import static org.mockito.Mockito.*;

public class CorrectionServiceTest {

    @Before
    public void setUp() {
        addressDao = mock(AddressDao.class);
        correctionService = new CorrectionService(addressDao);
    }


    @Test
    public void shouldCallDeleteAddress() {
        correctionService.correct(VALID_ID);
        verify(addressDao).deleteAddress(VALID_ID);
    }
}  

A simplified version of the CorrectionService class (visibility modifiers removed for simplicity).

class CorrectionService {

   AddressDao addressDao;

   CorrectionService(AddressDao addressDao) {
       this.addressDao;
   }

   void correctPerson(Long personId) {
       //Do some stuff with the addressDao here...
   }

}

In your test:

import static org.mockito.Mockito.*;

public class CorrectionServiceTest {

    @Before
    public void setUp() {
        addressDao = mock(AddressDao.class);
        correctionService = new CorrectionService(addressDao);
    }


    @Test
    public void shouldCallDeleteAddress() {
        correctionService.correct(VALID_ID);
        verify(addressDao).deleteAddress(VALID_ID);
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文