Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skuzzle committed Dec 22, 2021
1 parent a83fd5a commit 1239915
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.kohsuke.github.GitHub;
import org.mockito.Mockito;

public class MockGitHubBuilder {

private final List<ThrowingConsumer<GitHub>> mockConsumers = new ArrayList<>();
private final Map<String, ThrowingConsumer<GitHub>> perRepoConsumers = new HashMap<>();

private MockGitHubBuilder() {
}
Expand All @@ -21,18 +21,20 @@ public static MockGitHubBuilder mockGitHub() {
}

public MockGitHubBuilder withRepository(MockRepositoryBuilder repository) {
this.mockConsumers.add(gh -> when(gh.getRepository(repository.getFullname())).thenReturn(repository.build()));
perRepoConsumers.put(repository.fullname(),
gh -> when(gh.getRepository(repository.fullname())).thenReturn(repository.build()));
return this;
}

public MockGitHubBuilder withInaccessibleRepository(MockRepositoryBuilder repository) {
this.mockConsumers.add(gh -> when(gh.getRepository(repository.getFullname())).thenThrow(IOException.class));
perRepoConsumers.put(repository.fullname(),
gh -> when(gh.getRepository(repository.fullname())).thenThrow(IOException.class));
return this;
}

public GitHub build() {
final GitHub mock = Mockito.mock(GitHub.class);
mockConsumers.forEach(consumer -> consumer.acceptUnsafe(mock));
perRepoConsumers.values().forEach(consumer -> consumer.acceptUnsafe(mock));
return mock;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private MockRepositoryBuilder(String owner, String name) {
}
}

public String getFullname() {
public String fullname() {
return owner + "/" + name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

public final class MockRepositoryGitHubAuthentication implements GitHubAuthentication {

private static final long serialVersionUID = 7101792420516373175L;
private final MockGitHubBuilder modify;
private boolean anonymous = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -41,7 +40,7 @@ private void expectStargazerCount(GitHubAuthentication authentication, String ow
}

@Test
void metrics_should_be_update_asynchronously() throws Exception {
void metrics_should_be_updated_asynchronously() throws Exception {
final MockRepositoryBuilder mockedRepo = MockRepositoryBuilder.withName("skuzzle", "test")
.withStargazerCount(1337);
final GitHubAuthentication authentication = successfulAuthenticationForRepository(mockedRepo);
Expand All @@ -63,7 +62,6 @@ void metrics_should_be_update_asynchronously() throws Exception {
}

@Test
@Disabled("modify() is not idempotent")
void registration_should_be_terminated_on_scrape_error() throws Exception {
final MockRepositoryBuilder mockedRepo = MockRepositoryBuilder.withName("skuzzle", "test")
.withStargazerCount(1337);
Expand Down

0 comments on commit 1239915

Please sign in to comment.