Skip to content

Commit f59ab3b

Browse files
yrodieregsmet
authored andcommitted
Add a few mocking utils to the testing framework
(cherry picked from commit ac4cc88)
1 parent da0a6ef commit f59ab3b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

integration-tests/testing-framework/src/test/java/io/quarkiverse/githubapp/it/testingframework/TestingFrameworkTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.quarkiverse.githubapp.it.testingframework;
22

3+
import static io.quarkiverse.githubapp.testing.GitHubAppMockito.mockPagedIterable;
34
import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
45
import static io.quarkiverse.githubapp.testing.GitHubAppTesting.when;
56
import static org.assertj.core.api.Assertions.assertThat;
@@ -276,18 +277,17 @@ void clientProvider() {
276277
Mockito.when(mocks.applicationClient().getApp()).thenReturn(app);
277278
Mockito.when(installation1.getId()).thenReturn(1L);
278279
Mockito.when(installation2.getId()).thenReturn(2L);
279-
PagedIterable<GHAppInstallation> appInstallations = MockHelper.mockPagedIterable(installation1,
280-
installation2);
280+
PagedIterable<GHAppInstallation> appInstallations = mockPagedIterable(installation1, installation2);
281281
Mockito.when(app.listInstallations()).thenReturn(appInstallations);
282282

283283
Mockito.when(installation1Repo1.getFullName()).thenReturn("quarkusio/quarkus");
284-
PagedSearchIterable<GHRepository> installation1Repos = MockHelper.mockPagedIterable(installation1Repo1);
284+
PagedSearchIterable<GHRepository> installation1Repos = mockPagedIterable(installation1Repo1);
285285
Mockito.when(installation1.listRepositories())
286286
.thenReturn(installation1Repos);
287287

288288
Mockito.when(installation2Repo1.getFullName()).thenReturn("quarkiverse/quarkus-github-app");
289289
Mockito.when(installation2Repo2.getFullName()).thenReturn("quarkiverse/quarkus-github-api");
290-
PagedSearchIterable<GHRepository> installation2Repos = MockHelper.mockPagedIterable(installation2Repo1,
290+
PagedSearchIterable<GHRepository> installation2Repos = mockPagedIterable(installation2Repo1,
291291
installation2Repo2);
292292
Mockito.when(installation2.listRepositories())
293293
.thenReturn(installation2Repos);

integration-tests/testing-framework/src/test/java/io/quarkiverse/githubapp/it/testingframework/MockHelper.java renamed to testing/src/main/java/io/quarkiverse/githubapp/testing/GitHubAppMockito.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
package io.quarkiverse.githubapp.it.testingframework;
1+
package io.quarkiverse.githubapp.testing;
22

33
import static org.mockito.Mockito.mock;
44
import static org.mockito.Mockito.when;
5+
import static org.mockito.Mockito.withSettings;
56

67
import java.util.Iterator;
78
import java.util.List;
89

910
import org.kohsuke.github.PagedIterator;
1011
import org.kohsuke.github.PagedSearchIterable;
12+
import org.mockito.Answers;
13+
import org.mockito.quality.Strictness;
1114

12-
class MockHelper {
15+
public final class GitHubAppMockito {
16+
17+
private GitHubAppMockito() {
18+
}
19+
20+
public static <T> T mockBuilder(Class<T> builderClass) {
21+
return mock(builderClass, withSettings().defaultAnswer(Answers.RETURNS_SELF));
22+
}
1323

1424
@SafeVarargs
1525
@SuppressWarnings("unchecked")
1626
public static <T> PagedSearchIterable<T> mockPagedIterable(T... contentMocks) {
17-
PagedSearchIterable<T> iterableMock = mock(PagedSearchIterable.class);
27+
PagedSearchIterable<T> iterableMock = mock(PagedSearchIterable.class,
28+
withSettings().stubOnly().strictness(Strictness.LENIENT).defaultAnswer(Answers.RETURNS_SELF));
29+
when(iterableMock.spliterator()).thenAnswer(ignored -> List.of(contentMocks).spliterator());
1830
when(iterableMock.iterator()).thenAnswer(ignored -> {
19-
PagedIterator<T> iteratorMock = mock(PagedIterator.class);
31+
PagedIterator<T> iteratorMock = mock(PagedIterator.class, withSettings().stubOnly().strictness(Strictness.LENIENT));
2032
Iterator<T> actualIterator = List.of(contentMocks).iterator();
2133
when(iteratorMock.next()).thenAnswer(ignored2 -> actualIterator.next());
2234
when(iteratorMock.hasNext()).thenAnswer(ignored2 -> actualIterator.hasNext());

0 commit comments

Comments
 (0)