Skip to content

Commit f173b88

Browse files
committed
Add MethodSource and checking of body
1 parent 4fda6bc commit f173b88

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/test/java/com/example/postgresdemo/service/QuestionServiceTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import org.junit.jupiter.api.Test;
1010
import org.junit.jupiter.api.extension.ExtendWith;
1111
import org.junit.jupiter.params.ParameterizedTest;
12-
import org.junit.jupiter.params.provider.NullSource;
13-
import org.junit.jupiter.params.provider.ValueSource;
12+
import org.junit.jupiter.params.provider.Arguments;
13+
import org.junit.jupiter.params.provider.MethodSource;
1414
import org.mockito.*;
1515
import org.springframework.data.domain.Page;
1616
import org.mockito.junit.jupiter.MockitoExtension;
@@ -20,8 +20,7 @@
2020

2121
import java.util.Arrays;
2222
import java.util.Optional;
23-
24-
import static org.mockito.ArgumentMatchers.any;
23+
import java.util.stream.Stream;
2524

2625
@ExtendWith(MockitoExtension.class)
2726
class QuestionServiceTest {
@@ -60,9 +59,8 @@ void testFindAll() {
6059
}
6160

6261
@ParameterizedTest
63-
@NullSource
64-
@ValueSource(strings = {"", "Some Description"})
65-
void testCreateWithDescriptionVariations(String description) {
62+
@MethodSource("provideDescriptions")
63+
void testCreateWithDescriptionVariations(String description, String expectedBody) {
6664
QuestionRequestDTO request = new QuestionRequestDTO();
6765
request.setTitle("Title");
6866
request.setDescription(description);
@@ -84,6 +82,8 @@ void testCreateWithDescriptionVariations(String description) {
8482
Assertions.assertEquals(request.getTitle(), capturedQuestion.getTitle());
8583
Assertions.assertEquals(request.getDescription(), capturedQuestion.getDescription());
8684
Assertions.assertEquals(1L, result.getId());
85+
86+
Assertions.assertEquals(expectedBody, result.getBody());
8787
}
8888

8989
@Test
@@ -158,4 +158,12 @@ void deleteNonexistentQuestion() {
158158

159159
Mockito.verifyNoMoreInteractions(questionRepository);
160160
}
161+
162+
private static Stream<Arguments> provideDescriptions() {
163+
return Stream.of(
164+
Arguments.of(null, "Title\nnull"),
165+
Arguments.of("", "Title\n"),
166+
Arguments.of("Some description", "Title\nSome description")
167+
);
168+
}
161169
}

0 commit comments

Comments
 (0)