jav*_*999 3 java junit hibernate jpa spring-data
对于下面的代码,我可以编写哪些最详尽的测试?
public void deleteFromPerson(person person) {
person = personRepository.returnPerson(person.getId());
personRepository.delete(person);
}
Run Code Online (Sandbox Code Playgroud)
这个方法在a service
课堂上.该方法调用JpaRepository,然后delete()
在实体上调用它的方法.
如果无法测试实体是否被删除,那么我可以在该方法上运行其他 任何东西tests cases
吗?
有两种测试策略.一个是单元测试,即确保您的服务正常运行.另一个是集成/端到端测试,即确保一切都很好地结合在一起.
你对你拥有的东西进行单元测试,你可以集成测试你拥有的一切 这是一个非常粗略的例子,只使用你的陈述,加上一些我无法填补空白的东西.
单元测试
使用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
}
Run Code Online (Sandbox Code Playgroud)
使用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
}
Run Code Online (Sandbox Code Playgroud)
是的,这个测试看起来很严格,但无论如何,这真的是你在单元测试中测试的全部内容.您希望数据库使用参数从数据库中获取Person,因此为什么有两个Person
对象,并且您希望删除该传递的Person
对象,这就是您期望调用的原因.简单方法产生简单的测试.您基本上希望确保按预期与存储库进行交互.在实际实现中,存储库可能会被破坏或为空,但这并不会改变您的服务正确实现的事实.
集成测试
另一方面,如果要进行集成测试,则不使用模拟.相反,您需要连接所有内容,如测试数据库和回购.我会把它留给你,因为没有参考实施.
@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);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您需要确保您的repo实际处理来自服务的所有调用.您知道您的服务是通过单元测试工作的,但是repo是从服务中运行还是您配置错误
归档时间: |
|
查看次数: |
14508 次 |
最近记录: |