Package io.mats3.test.junit
Class Rule_MatsEndpoint<R,I>
java.lang.Object
io.mats3.test.abstractunit.AbstractMatsTestEndpoint<R,I>
io.mats3.test.junit.Rule_MatsEndpoint<R,I>
- Type Parameters:
R
- The reply class of the message generated by this endpoint. (Reply Class)I
- The incoming message class for this endpoint. (Request Class)
- All Implemented Interfaces:
org.junit.rules.TestRule
public class Rule_MatsEndpoint<R,I>
extends AbstractMatsTestEndpoint<R,I>
implements org.junit.rules.TestRule
Rule to create a single staged endpoint whose reply/processor can be changed throughout its life, i.e. per test (e.g.
answer "Sorry, no can do." for the first test, and then "Yes, we can!" for the next test). Useful for mocking
endpoints in tests where you need predictable replies, and may also be used to verify that an endpoint was
not invoked.
The endpoint processor can be changed on demand using setProcessLambda(ProcessSingleLambda)
Must be annotated with @Rule
. Do not use with @ClassRule
Retrieve the endpoint's received(incoming) message/messages by calling on of the following methods:
AbstractMatsTestEndpoint.waitForRequest()
- Wait for a message(singular) using the default timeoutAbstractMatsTestEndpoint.waitForRequest(long)
- Wait for a message(singular) with user specified timeoutAbstractMatsTestEndpoint.waitForRequests(int)
- Wait for X messages using the default timeoutAbstractMatsTestEndpoint.waitForRequests(int, long)
- Wait for X messages with user specified timeout
AbstractMatsTestEndpoint.verifyNotInvoked()
to ensure that the endpoint was not in fact invoked during the test.
If no process lambda is specified for the endpoint it will act as a terminator, thus it does not generate a reply.
@Rule public Rule_MatsEndpoint<String, String> _world = Rule_MatsEndpoint.single(endpointFactory, "World", String.class, String.class, (context, in) -> in + "World");Should one want to utilize this test endpoint approach in a test which brings up a Spring context which contains a
MatsFactory
one can utilize the @SpringInjectRulesAndExtensions
(in 'mats-spring-test') which
will inject/autowire this class automatically by providing the MatsFactory
located in said Spring context.-
Field Summary
Fields inherited from class io.mats3.test.abstractunit.AbstractMatsTestEndpoint
_matsFactory, _processLambda
-
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statement
apply
(org.junit.runners.model.Statement base, org.junit.runner.Description description) Note: Shamelessly inspired from: How to combine @Rule and @ClassRule in JUnit 4.12static <R,
I> Rule_MatsEndpoint<R, I> Convenience variant ofcreate(endpointId, replyClass, incomingClass)
taking aRule_Mats
as first argument for fetching theMatsFactory
, for use in "pure Java" environments (read as: non-Spring).static <R,
I> Rule_MatsEndpoint<R, I> Creates a JUnit Rule for a single-staged endpoint whose processor is not defined at start.setMatsFactory
(MatsFactory matsFactory) Sets the internalMatsFactory
to be utilized for the creation of this endpoint.setProcessLambda
(MatsEndpoint.ProcessSingleLambda<R, I> processLambda) Specify the processing lambda to be executed by the endpoint aka the endpoint logic.Methods inherited from class io.mats3.test.abstractunit.AbstractMatsTestEndpoint
after, before, idThis, verifyNotInvoked, waitForRequest, waitForRequest, waitForRequests, waitForRequests
-
Method Details
-
setMatsFactory
Sets the internalMatsFactory
to be utilized for the creation of this endpoint.If not utilized explicitly can also be injected/autowired through the use of the test execution listener
@SpringInjectRulesAndExtensions
should this Rule be utilized in a test where a Spring context is in play.- Specified by:
setMatsFactory
in classAbstractMatsTestEndpoint<R,
I> - Parameters:
matsFactory
- to set.- Returns:
- this instance of the object.
-
setProcessLambda
Description copied from class:AbstractMatsTestEndpoint
Specify the processing lambda to be executed by the endpoint aka the endpoint logic. This is typically invoked either as part of the directly inside a test method to setup the behavior for that specific test or once through the initial setup when creating the test endpoint.- Specified by:
setProcessLambda
in classAbstractMatsTestEndpoint<R,
I> - Parameters:
processLambda
- which the endpoint should execute on an incoming request.
-
create
public static <R,I> Rule_MatsEndpoint<R,I> create(String endpointId, Class<R> replyMsgClass, Class<I> incomingMsgClass) Creates a JUnit Rule for a single-staged endpoint whose processor is not defined at start. Sets it up on JUnit lifecycle 'before' and tears it down on 'after'. Notice that aMatsFactory
must be set before it is usable! In a Spring environment, you should probably employ the@SpringInjectRulesAndExtensions
to make this happen automagically. In a "pure Java" environment, consider the convenience overloadcreate(Mats_Rule, endpointId, replyClass, incomingClass)
to easily supply the corresponding@ClassRule
Rule_Mats
for fetching theMatsFactory
. Do notice that you need to invokesetProcessLambda(ProcessSingleLambda)
- typically inside the@Test
method - before sending messages to it, as there is no default.- Parameters:
endpointId
- of the endpoint.replyMsgClass
- the class of the reply message generated by this endpoint.incomingMsgClass
- the incoming message class for this endpoint.- Returns:
Rule_MatsEndpoint
-
create
public static <R,I> Rule_MatsEndpoint<R,I> create(Rule_Mats matsRule, String endpointId, Class<R> replyMsgClass, Class<I> incomingMsgClass) Convenience variant ofcreate(endpointId, replyClass, incomingClass)
taking aRule_Mats
as first argument for fetching theMatsFactory
, for use in "pure Java" environments (read as: non-Spring). -
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description) Note: Shamelessly inspired from: How to combine @Rule and @ClassRule in JUnit 4.12- Specified by:
apply
in interfaceorg.junit.rules.TestRule
-