Junit:为删除实体的方法编写测试? [英] Junit: writing a test for a method that deletes an entity?

查看:142
本文介绍了Junit:为删除实体的方法编写测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以为以下代码编写的详尽的测试是什么?

What would be the most exhaustive tests that I could write for the following piece of code?

public void deleteFromPerson(person person) {
    person = personRepository.returnPerson(person.getId());
    personRepository.delete(person);
}

此方法在服务内上课。该方法调用 JpaRepository 然后在实体上调用它的 delete()方法。

This method is within a service class. The method makes calls to a JpaRepository that then calls it's delete() method on the entity.

如果无法测试实体是否被删除,是否有其他 测试用例我可以运行该方法吗?

If it is not possible to test if the entity is being deleted, is there any other tests cases I can run on the method?

推荐答案

有两种测试策略。一个是单元测试,即确保您的服务正常运行。另一个是集成/端到端测试,即确保所有内容都能很好地协同工作。

There are two testing strategies. One is unit testing, i.e. making sure your service works. The other is integration/end-to-end testing, i.e. making sure everything plays nicely together.

您可以对您拥有的内容进行单元测试,您可以对所有内容进行集成测试。这是一个非常粗略的例子,只使用你的陈述,加上一些我无法填补空白的东西。

You unit test stuff you own, you integration test everything you have. This is a pretty rough example using just your statement, plus some made up stuff where I can't fill in the blanks.

单元测试

使用Mockito

PersonRepository personRepository = mock(PersonRepository.class);

@TestSubject
PersonService personService = new PersonService(): 

@Test
public void unitTest() {
    personService.setPersonRepository(personRepository);
    Person person = new Person(1L);
    Person person2 = new Person(1L);

    when(personRepository.returnPerson(1L)).thenReturn(person2); //expect a fetch, return a "fetched" person;

    personService.deleteFromPerson(person);

    verify(personRepository, times(1)).delete(person2); //pretty sure it is verify after call
}

使用EasyMock ...

Using EasyMock...

@Mock
PersonRepository personRepository; //assuming it is autowired

@TestSubject
PersonService personService = new PersonService(): 

@Test
public void unitTest() {
    Person person = new Person(1L);
    Person person2 = new Person(1L);

    EasyMock.expect(personRepository.returnPerson(1L)).andReturn(person2); //expect a fetch, return a "fetched" person;
    personRepository.delete(person2);
    EasyMock.expectLastCall(); //expect a delete for person2 we plan to delete
    replayAll();

    personService.deleteFromPerson(person);

    verifyAll(); //make sure everything was called
}

是的,这个测试看起来像是写的严格来说,无论如何,这真的是你在单元测试中测试的全部内容。您希望DB使用参数从数据库中获取Person,因此为什么有两个 Person 对象,并且您希望删除传递的 Person object,这就是你期望通话的原因。简单方法产生简单的测试。您基本上希望确保按预期与存储库进行交互。在实际实现中,存储库可能会被破坏或为空,但这并不会改变您的服务正确实现的事实。

Yes, this test looks like it is written rigidly, but that's really all you're testing in the unit test, anyways. You want the DB to fetch a Person from the database using an argument, hence why there are two Person objects, and you expect to delete that passed Person object, which is why you expect the call. Simple method yields simple test. You basically want to ensure you're interacting with your repository as you expect. Repository could be broken or null in actual implementation, but that doesn't change the fact that your service is implemented correctly.

集成测试

另一方面,如果要进行集成测试,则不使用模拟。相反,您需要连接所有内容,例如测试数据库和repo。由于没有实施参考,我会把它留给你。

On the other hand, if you want to do an integration test, no mocking is used. Instead, you'll need to wire up everything like a test DB and repo. I'll leave that up to you since there's no reference for implementation.

@Test
public void integrationTestForAddAndDelete() {
    Person person = createDummyPersonForInsertion(); //static method that creates a test Person for you
    Person comparePerson;
    //make sure we haven't added the person yet
    Assert.assertNull(personService.getPerson(person));

    //add the Person
    comparePerson = personService.addPerson(person);
    Assert.assertNotNull(personService.getPerson(person));
    //add a rigorous compare method to make sure contents are the same, i.e. nothing is lost or transmuted incorrectly, ignoring ID if that is autogen
    //alternatively, you can create a unit test just for Person
    Assert.assertEquals(person, comparePerson); 

    //remove the Person
    personService.deleteFromPerson(person);
    Assert.assertNull(personService.getPerson(person));

    //test for exception handling when you try to remove a non-existent person;
    personService.deleteFromPerson(person);

    //test for exception handling when you try to remove null
    personService.deleteFromPerson(null);
}

在这种情况下,您要确保您的仓​​库实际处理所有呼叫从服务。您知道您的服务是通过单元测试工作的,但是回购是否来自服务或者您配置了错误

In this case, you want to make sure your repo actually handles all the calls from the service. You know your service works from the unit test, but does the repo work from the service or have you configured something wrong

这篇关于Junit:为删除实体的方法编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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