2つのテスト戦略があります。 1つは単体テスト、つまりサービスが確実に動作することを確認することです。もう1つは、統合/エンドツーエンドのテストです。つまり、すべてがうまくいっていることを確認します。
あなたが持っているユニットテストのものは、あなたが持っているすべてを統合テストしてください。これは、あなたのステートメントだけでなく、空白を埋めることのできないものを作ったかなり大まかな例です。
ユニットテストEasyMockを使用した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
}
を使用して
...
@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をフェッチするようにしたいのですが、なぜ2つのPerson
オブジェクトがあるのでしょうか?Person
オブジェクトを渡したものを削除すると予想されます。簡単な方法で簡単なテストができます。基本的には、期待どおりにリポジトリと対話していることを確認する必要があります。実際の実装ではリポジトリが破損したりnullになったりする可能性がありますが、サービスが正しく実装されているという事実は変わりません。
の統合では、統合テストを行いたい場合は、何のモックが使用されていない、一方
をテストします。代わりに、のテストDBとrepoのようなものすべてを結線する必要があります。私はそれを実装のための参照がないので、あなたに任せます。
@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);
}
この場合、レポがサービスからのすべてのコールを実際に処理していることを確認する必要があります。あなたはあなたのサービスが単体テストで動作していることを知っていますが、サービスからレポ作業をしたり、何かを間違って設定したりしましたか?
正常に取り込まれたことを確認してから、もう一度。 2回目にそれを取得できない場合、削除は成功しました。 –
*エンティティが削除されているかどうかをテストすることができない場合*単体テストを書く場合、テストの責任は 'personRepository.delete'メソッドが実際には動作していないことを確認することです。統合テストを書く場合は、 'Person'を作成し、その存在を検証し、それを削除し、その存在を検証する必要があります。 – Compass
また、無効なIDまたは有効なIDを2回削除することは、期待通りに行うことをテストします。 – walsht