-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HermesMock with ClassRule and JUnit 5 Extension (#977)
- Loading branch information
1 parent
91b1f34
commit 73d0013
Showing
8 changed files
with
155 additions
and
15 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
59 changes: 59 additions & 0 deletions
59
hermes-mock/src/main/java/pl/allegro/tech/hermes/mock/HermesMockExtension.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,59 @@ | ||
package pl.allegro.tech.hermes.mock; | ||
|
||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
public class HermesMockExtension implements ParameterResolver, AfterAllCallback, AfterEachCallback { | ||
|
||
private final HermesMock hermesMock; | ||
|
||
public HermesMockExtension(int port) { | ||
this.hermesMock = new HermesMock.Builder().withPort(port).build(); | ||
this.hermesMock.start(); | ||
} | ||
|
||
public HermesMockExtension(HermesMock hermesMock) { | ||
this.hermesMock = hermesMock; | ||
this.hermesMock.start(); | ||
} | ||
|
||
public HermesMockDefine define() { | ||
return hermesMock.define(); | ||
} | ||
|
||
public HermesMockExpect expect() { | ||
return hermesMock.expect(); | ||
} | ||
|
||
public HermesMockQuery query() { | ||
return hermesMock.query(); | ||
} | ||
|
||
public void resetReceivedRequest() { | ||
hermesMock.resetReceivedRequest(); | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext extensionContext) throws Exception { | ||
hermesMock.stop(); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext extensionContext) throws Exception { | ||
hermesMock.resetReceivedRequest(); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterContext.getParameter().getType().isAssignableFrom(HermesMock.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return hermesMock; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
hermes-mock/src/test/groovy/pl/allegro/tech/hermes/mock/HermesMockExtensionTest.groovy
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,29 @@ | ||
package pl.allegro.tech.hermes.mock | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import pl.allegro.tech.hermes.test.helper.endpoint.HermesPublisher | ||
import spock.lang.Specification | ||
|
||
class HermesMockExtensionTest extends Specification { | ||
|
||
@RegisterExtension | ||
HermesMockExtension hermes = new HermesMockExtension(56789) | ||
|
||
HermesPublisher publisher = new HermesPublisher("http://localhost:56789") | ||
|
||
def 'simple publish-expect test'() { | ||
given: | ||
def topicName = "my-test-topic" | ||
hermes.define().jsonTopic(topicName) | ||
|
||
when: | ||
publish(topicName, TestMessage.random().asJson()) | ||
|
||
then: | ||
hermes.expect().singleMessageOnTopic(topicName) | ||
} | ||
|
||
def publish(String topic, String body) { | ||
publisher.publish(topic, body) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
hermes-mock/src/test/groovy/pl/allegro/tech/hermes/mock/HermesMockRuleTest.groovy
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,28 @@ | ||
package pl.allegro.tech.hermes.mock | ||
|
||
import org.junit.Rule | ||
import pl.allegro.tech.hermes.test.helper.endpoint.HermesPublisher | ||
import spock.lang.Specification | ||
|
||
class HermesMockRuleTest extends Specification { | ||
@Rule | ||
HermesMockRule hermes = new HermesMockRule(56789) | ||
|
||
HermesPublisher publisher = new HermesPublisher("http://localhost:56789") | ||
|
||
def 'simple publish-expect test'() { | ||
given: | ||
def topicName = "my-test-topic" | ||
hermes.define().jsonTopic(topicName) | ||
|
||
when: | ||
publish(topicName, TestMessage.random().asJson()) | ||
|
||
then: | ||
hermes.expect().singleMessageOnTopic(topicName) | ||
} | ||
|
||
def publish(String topic, String body) { | ||
publisher.publish(topic, body) | ||
} | ||
} |
14 changes: 10 additions & 4 deletions
14
hermes-mock/src/test/groovy/pl/allegro/tech/hermes/mock/HermesMockTest.groovy
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