-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36e0ef0
commit fad415e
Showing
3 changed files
with
240 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
cucumber-spring/src/test/java/io/cucumber/spring/TestTestContextAdaptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package io.cucumber.spring; | ||
|
||
import io.cucumber.core.backend.CucumberBackendException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestContextManager; | ||
import org.springframework.test.context.TestExecutionListener; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.inOrder; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class TestTestContextAdaptorTest { | ||
|
||
@Mock | ||
TestExecutionListener listener; | ||
|
||
@AfterEach | ||
void verifyNoMoroInteractions(){ | ||
Mockito.verifyNoMoreInteractions(listener); | ||
} | ||
|
||
@Test | ||
void invokesAllLiveCycleHooks() throws Exception { | ||
TestContextManager manager = new TestContextManager(SpringContextConfiguration.class); | ||
TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(SpringContextConfiguration.class)); | ||
manager.registerTestExecutionListeners(listener); | ||
InOrder inOrder = inOrder(listener); | ||
|
||
adaptor.start(); | ||
inOrder.verify(listener).beforeTestClass(any()); | ||
inOrder.verify(listener).prepareTestInstance(any()); | ||
inOrder.verify(listener).beforeTestMethod(any()); | ||
inOrder.verify(listener).beforeTestExecution(any()); | ||
|
||
|
||
adaptor.stop(); | ||
inOrder.verify(listener).afterTestExecution(any()); | ||
inOrder.verify(listener).afterTestMethod(any()); | ||
inOrder.verify(listener).afterTestClass(any()); | ||
} | ||
|
||
@Test | ||
void invokesAfterClassIfBeforeClassFailed() throws Exception { | ||
TestContextManager manager = new TestContextManager(SpringContextConfiguration.class); | ||
TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(SpringContextConfiguration.class)); | ||
manager.registerTestExecutionListeners(listener); | ||
InOrder inOrder = inOrder(listener); | ||
|
||
doThrow(new RuntimeException()).when(listener).beforeTestClass(any()); | ||
|
||
assertThrows(CucumberBackendException.class, adaptor::start); | ||
inOrder.verify(listener).beforeTestClass(any()); | ||
|
||
adaptor.stop(); | ||
inOrder.verify(listener).afterTestClass(any()); | ||
} | ||
|
||
|
||
@Test | ||
void invokesAfterClassIfPrepareTestInstanceFailed() throws Exception { | ||
TestContextManager manager = new TestContextManager(SpringContextConfiguration.class); | ||
TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(SpringContextConfiguration.class)); | ||
manager.registerTestExecutionListeners(listener); | ||
InOrder inOrder = inOrder(listener); | ||
|
||
doThrow(new RuntimeException()).when(listener).prepareTestInstance(any()); | ||
|
||
assertThrows(CucumberBackendException.class, adaptor::start); | ||
inOrder.verify(listener).beforeTestClass(any()); | ||
|
||
adaptor.stop(); | ||
inOrder.verify(listener).afterTestClass(any()); | ||
} | ||
|
||
|
||
@Test | ||
void invokesAfterMethodIfBeforeMethodThrows() throws Exception { | ||
TestContextManager manager = new TestContextManager(SpringContextConfiguration.class); | ||
TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(SpringContextConfiguration.class)); | ||
manager.registerTestExecutionListeners(listener); | ||
InOrder inOrder = inOrder(listener); | ||
|
||
doThrow(new RuntimeException()).when(listener).beforeTestMethod(any()); | ||
|
||
assertThrows(CucumberBackendException.class, adaptor::start); | ||
inOrder.verify(listener).beforeTestClass(any()); | ||
inOrder.verify(listener).prepareTestInstance(any()); | ||
inOrder.verify(listener).beforeTestMethod(any()); | ||
|
||
adaptor.stop(); | ||
inOrder.verify(listener).afterTestMethod(any()); | ||
inOrder.verify(listener).afterTestClass(any()); | ||
} | ||
|
||
@Test | ||
void invokesAfterTestExecutionIfBeforeTestExecutionThrows() throws Exception { | ||
TestContextManager manager = new TestContextManager(SpringContextConfiguration.class); | ||
TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(SpringContextConfiguration.class)); | ||
manager.registerTestExecutionListeners(listener); | ||
InOrder inOrder = inOrder(listener); | ||
|
||
doThrow(new RuntimeException()).when(listener).beforeTestExecution(any()); | ||
|
||
assertThrows(CucumberBackendException.class, adaptor::start); | ||
inOrder.verify(listener).beforeTestClass(any()); | ||
inOrder.verify(listener).prepareTestInstance(any()); | ||
inOrder.verify(listener).beforeTestMethod(any()); | ||
|
||
adaptor.stop(); | ||
inOrder.verify(listener).afterTestExecution(any()); | ||
inOrder.verify(listener).afterTestMethod(any()); | ||
inOrder.verify(listener).afterTestClass(any()); | ||
} | ||
|
||
@CucumberContextConfiguration | ||
@ContextConfiguration("classpath:cucumber.xml") | ||
public static class SpringContextConfiguration { | ||
|
||
} | ||
|
||
} |