2016-06-28 46 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