2016-06-28 41 views
2

什么将是最详尽的测试,我可以写下面的一段代码?Junit:为删除实体的方法编写测试?

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

此方法在a service类中。该方法调用JpaRepository,然后在该实体上调用它的delete()方法。

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

+0

你可以通过检索person对象来确定它被成功检索然后删除它并试图检索它再次。如果第二次无法检索,则删除成功。 –

+1

*如果无法测试实体是否被删除*如果您正在编写单元测试,测试的责任是确保调用'personRepository.delete'方法,而不是实际工作。如果你正在编写一个集成测试,你应该创建一个'Person',验证它的存在,然后删除它并验证它不存在。 – Compass

+0

还测试删除一个无效的id或一个有效的id两次,如你所料。 – walsht

回答

5

有两种测试策略。一个是单元测试,即确保你的服务有效。另一个是集成/端到端测试,即确保一切都很好地结合在一起。

你单元测试你拥有的东西,你集成测试你拥有的所有东西。这是一个非常粗略的例子,只是使用你的陈述,另外一些是填充空白的东西。

单元测试

使用的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 ...

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

是,这个测试看起来是硬性写的,但这是真的你”反正在单元测试中重新测试。您希望数据库使用参数从数据库中获取人员,因此为什么有两个Person对象,并且您希望删除传递Person对象的对象,这就是您期望该调用的原因。简单的方法产生简单的测试您基本上需要确保您按照预期与您的存储库进行交互。在实际的实现中,存储库可能会被破坏或为null,但这并不会改变您的服务正确实施的事实。

集成测试

在另一方面,如果你想要做一个集成测试,没有嘲讽使用。相反,你需要连接一切,如测试DB和回购。由于没有实施参考,我会把它留给你。

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

在这种情况下,你想确保你的回购实际上处理来自服务的所有调用。你知道你的服务可以在单元测试中使用,但是服务器可以从服务中工作或者你配置了错误的东西

+0

谢谢你的例子,你能否快速重新写下使用Mockito的例子,我从来没有用过简单的模拟,所以我觉得它很混乱? – java123999

+0

新增了Mockito会是什么的粗略想法。如果你知道Mockito,如果你切换到EasyMock,语法应该非常相似。 – Compass