Skip to content

Commit

Permalink
fix: removed mockito dependency on core for #2767
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kronegg committed Oct 22, 2023
1 parent 373eb11 commit db31633
Show file tree
Hide file tree
Showing 4 changed files with 579 additions and 268 deletions.
7 changes: 0 additions & 7 deletions cucumber-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<xmlunit.version>2.9.1</xmlunit.version>
<hamcrest.version>2.2</hamcrest.version>
<hamcrest-json.version>0.2</hamcrest-json.version>
<mockito.version>5.6.0</mockito.version>
<vertx.version>4.4.6</vertx.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
</properties>
Expand Down Expand Up @@ -139,12 +138,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
Expand Down
114 changes: 80 additions & 34 deletions cucumber-core/src/test/java/io/cucumber/core/runner/HookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.cucumber.core.backend.Glue;
import io.cucumber.core.backend.HookDefinition;
import io.cucumber.core.backend.ObjectFactory;
import io.cucumber.core.backend.Snippet;
import io.cucumber.core.backend.TestCaseState;
import io.cucumber.core.eventbus.EventBus;
import io.cucumber.core.feature.TestFeatureParser;
import io.cucumber.core.gherkin.Feature;
Expand All @@ -12,21 +14,18 @@
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.core.snippets.TestSnippet;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;

import java.net.URI;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class HookTest {

Expand All @@ -44,43 +43,23 @@ class HookTest {
*/
@Test
void after_hooks_execute_before_objects_are_disposed() {
Backend backend = mock(Backend.class);
when(backend.getSnippet()).thenReturn(new TestSnippet());
final List<String> eventListener = new ArrayList<>();
final HookDefinition hook = new MockHookDefinition("", "hook-location", eventListener);
Backend backend = new StubBackend(hook, eventListener);
ObjectFactory objectFactory = new StubObjectFactory();
final HookDefinition hook = mock(HookDefinition.class);
when(hook.getLocation()).thenReturn("hook-location");
when(hook.getTagExpression()).thenReturn("");

doAnswer(invocation -> {
Glue glue = invocation.getArgument(0);
glue.addBeforeHook(hook);
return null;
}).when(backend).loadGlue(any(Glue.class), ArgumentMatchers.anyList());

Runner runner = new Runner(bus, Collections.singleton(backend), objectFactory, runtimeOptions);

runner.runPickle(pickle);

InOrder inOrder = inOrder(hook, backend);
inOrder.verify(backend).buildWorld();
inOrder.verify(hook).execute(ArgumentMatchers.any());
inOrder.verify(backend).disposeWorld();
assertLinesMatch(eventListener, List.of("buildWorld", "execute", "disposeWorld"));
}

@Test
void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
Backend backend = mock(Backend.class);
when(backend.getSnippet()).thenReturn(new TestSnippet());
final List<String> eventListener = new ArrayList<>();
final HookDefinition hook = new MockHookDefinition("(", "hook-location", eventListener);
Backend backend = new StubBackend(hook, eventListener);
ObjectFactory objectFactory = new StubObjectFactory();
final HookDefinition hook = mock(HookDefinition.class);
when(hook.getLocation()).thenReturn("hook-location");
when(hook.getTagExpression()).thenReturn("(");

doAnswer(invocation -> {
Glue glue = invocation.getArgument(0);
glue.addBeforeHook(hook);
return null;
}).when(backend).loadGlue(any(Glue.class), ArgumentMatchers.anyList());

RuntimeException e = assertThrows(RuntimeException.class,
() -> new Runner(bus, Collections.singleton(backend), objectFactory,
Expand Down Expand Up @@ -111,4 +90,71 @@ public void stop() {

}
}

private final static class StubBackend implements Backend {
private final HookDefinition beforeHook;
private final List<String> eventListener;

public StubBackend(HookDefinition beforeHook, List<String> eventListener) {
this.beforeHook = beforeHook;
this.eventListener = eventListener;
}

@Override
public void loadGlue(Glue glue, List<URI> gluePaths) {
glue.addBeforeHook(beforeHook);
}

@Override
public void buildWorld() {
eventListener.add("buildWorld");
}

@Override
public void disposeWorld() {
eventListener.add("disposeWorld");
}

@Override
public Snippet getSnippet() {
return new TestSnippet();
}
}

private static final class MockHookDefinition implements HookDefinition {
private final String tagExpression;
private final String location;
private final List<String> eventListener;

public MockHookDefinition(String tagExpression, String location, List<String> eventListener) {
this.tagExpression = tagExpression;
this.location = location;
this.eventListener = eventListener;
}

@Override
public void execute(TestCaseState state) {
eventListener.add("execute");
}

@Override
public String getTagExpression() {
return tagExpression;
}

@Override
public int getOrder() {
return 0;
}

@Override
public boolean isDefinedAt(StackTraceElement stackTraceElement) {
return false;
}

@Override
public String getLocation() {
return location;
}
}
}
Loading

0 comments on commit db31633

Please sign in to comment.