이번 포스팅에선 Mockito가 지원하고 있는 BDD 스타일 API에 대해 알아보겠습니다.
먼저 BDD에 대한 개념부터 알아보고 가겠습니다.
BDD (Behavior Driven Development)란?
TDD (Test Driven Development)는 "테스트"를 기준으로 하는 개발 방법론입니다.
BDD (Behavior Driven Development)는 "행동"을 기준으로 하는 개발 방법론입니다. TDD를 참고했다고 하네요.
테스트할 때 Title, Narrative까진 사용하지 않고 Acceptance criteria만 사용합니다.
Title
An explicit title.
Narrative
A short introductory section with the following structure:
- As a: the person or role who will benefit from the feature;
- I want: the feature;
- so that: the benefit or value of the feature.
Acceptance criteria
A description of each specific scenario of the narrative with the following structure:
- Given: the initial context at the beginning of the scenario, in one or more clauses;
- When: the event that triggers the scenario
- Then: the expected outcome, in one or more clauses.
Given : 초기 context 값 , 어떠한 상황, 상황이 주어졌을때
When : 테스트하려는 조건, 뭔가를 하면
Then : 테스트 결과 , 그러면 이러할 것이다.
라고 생각하시면 됩니다.
Mockito 가 지원하는 BDD
Mockito는 BDD 스타일로 테스트 코드를 짤 수 있게 BDDMockito 클래스를 제공합니다.
엄청 간단합니다. 기존 메소드를 아래와 같이 변경해주면 됩니다.
when -> given
verify -> then
StudyServiceTest
@DisplayName("BDD")
@Test
void createStudyService6() {
//Given 어떠한 상황
StudyService studyService = new StudyService(memberService, studyRepository);
assertNotNull(studyService);
Member member = new Member();
member.setId(1L);
member.setEmail("jisu@email.com");
when(memberService.findById(1L)).thenReturn(Optional.of(member));
Study study = new Study(10, "테스트");
when(studyRepository.save(study)).thenReturn(study);
//When
studyService.createNewStudy(1L, study);
//Then
assertEquals(member, study.getOwner());
verify(memberService, times(1)).notify(study);
verifyNoMoreInteractions(memberService);
}
이제 위의 코드를 BDD로 바꿔보겠습니다.
@DisplayName("BDD")
@Test
void createStudyService6() {
//Given 어떠한 상황
StudyService studyService = new StudyService(memberService, studyRepository);
assertNotNull(studyService);
Member member = new Member();
member.setId(1L);
member.setEmail("jisu@email.com");
Study study = new Study(10, "테스트");
given(memberService.findById(1L)).willReturn(Optional.of(member)); //org.mockito.BDDMockito.given
given(studyRepository.save(study)).willReturn(study);
//When
studyService.createNewStudy(1L, study);
//Then
assertEquals(member, study.getOwner());
then(memberService).should(times(1)).notify(study); //org.mockito.BDDMockito.then
then(memberService).shouldHaveNoMoreInteractions();
}
org.mockito.BDDMockito 에서 지원해주는 given, then 입니다.
when -> given
verify -> then
verifyNoMoreInteractions -> shouldHaveNoMoreInteractions
위에서 말한것처럼 이렇게 바꿔 주기만 하면 됩니다.
'Spring Boot > Mockito' 카테고리의 다른 글
[Mockito] Verify(검증) (0) | 2022.08.07 |
---|---|
[Mockito] Mock 객체 Stubbing (0) | 2022.08.07 |
[Mockito] Mockito 시작하기 (0) | 2022.08.07 |
[Mockito] Mockito (0) | 2022.08.07 |
댓글