Skip to content

Commit

Permalink
Uses invalid token syntax for testing over mocking method (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored Apr 1, 2024
1 parent 4549214 commit b12319a
Showing 1 changed file with 2 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.File;
import java.io.IOException;

import hudson.FilePath;
import hudson.model.Run;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
Expand Down Expand Up @@ -89,24 +82,14 @@ public void matches_OnMismatchedTemplate_ReturnsFalse() {
}

@Test
@PrepareForTest({TokenMacro.class, FilePath.class})
public void matches_OnInvalidTemplate_ReturnsFalse() {
public void matches_OnInvalidTemplateSyntax_ReturnsFalse() {

// given
String template = "myTemplate";
String template = "myTemplate$$";
String value = "myValue";
TokenMacroCondition condition = new TokenMacroCondition(template, value);

Run run = mockRun();
mockStatic(TokenMacro.class);
mockStatic(FilePath.class);

try {
when(TokenMacro.expandAll(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any()))
.thenThrow(new MacroEvaluationException("ups!"));
} catch (MacroEvaluationException | IOException | InterruptedException e) {
throw new IllegalArgumentException(e);
}

// when
boolean matched = condition.matches(run, null);
Expand Down

0 comments on commit b12319a

Please sign in to comment.