public final class FnTestingRule extends Object implements org.junit.rules.TestRule
Rule for fn Java FDK functions.
This interface facilitates:
fn servicegivenEvent()FnResult instances.
public class MyFunctionTest {
{@literal @}Rule
public final FnTestingRule testing = FnTestingRule.createDefault();
{@literal @}Test
public void myTest() {
// Create an event to invoke MyFunction and put it into the event queue
fn.givenEvent()
.addHeader("FOO", "BAR")
.withBody("Body")
.enqueue();
// Run MyFunction#handleRequest using the built event queue from above
fn.thenRun(MyFunction.class, "handleRequest");
// Get the function result and check it for correctness
FnResult result = fn.getOnlyResult();
assertThat(result.getStatus()).isEqualTo(200);
assertThat(result.getBodyAsString()).isEqualTo("expected return value of my function");
}
}| Modifier and Type | Class and Description |
|---|---|
static class |
FnTestingRule.TestCodec |
static class |
FnTestingRule.TestOutput
TestOutput represents an output of a function it wraps OutputEvent and provides buffered access to the function output
|
| Modifier and Type | Method and Description |
|---|---|
void |
addFeature(FnTestingRuleFeature f) |
FnTestingRule |
addSharedClass(Class<?> cls)
Add a class to be forked during the test.
|
FnTestingRule |
addSharedClassPrefix(String prefix)
Add a class or package name to be forked during the test.
|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
static FnTestingRule |
createDefault()
Create an instance of the testing
Rule, with Flows support |
Map<String,String> |
getConfig() |
Map<String,String> |
getEventEnv() |
int |
getLastExitCode()
Get the exit code from the most recent invocation
0 = success
1 = failed
2 = not run due to initialization error
|
FnResult |
getOnlyResult()
Convenience method to get the one and only parsed http response expected on the output of the function
|
List<FnResult> |
getResults()
Parses any pending HTTP responses on the functions output stream and returns the corresponding FnResult instances
|
List<String> |
getSharedPrefixes() |
byte[] |
getStdErr()
Get the StdErr stream returned by the function as a byte array
|
String |
getStdErrAsString()
Gets the StdErr stream returned by the function as a String
|
FnEventBuilderJUnit4 |
givenEvent()
Create an HTTP event builder for the function
|
FnTestingRule |
setConfig(String key,
String value)
Add a config variable to the function for the test
|
void |
thenRun(Class<?> cls,
String method)
Runs the function runtime with the specified class and method (and waits for Flow stages to finish
if the test spawns any flows)
|
void |
thenRun(String cls,
String method)
Runs the function runtime with the specified class and method (and waits for Flow stages to finish
if the test spawns any Flow)
|
public void addFeature(FnTestingRuleFeature f)
public static FnTestingRule createDefault()
Rule, with Flows supportpublic FnTestingRule setConfig(String key, String value)
Config names will be translated to upper case with hyphens and spaces translated to _. Clashing config keys will be overwritten.
key - the configuration keyvalue - the configuration valuepublic FnTestingRule addSharedClassPrefix(String prefix)
prefix - A class name or package prefix, such as "com.example.foo."public FnTestingRule addSharedClass(Class<?> cls)
cls - A classpublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRulepublic FnEventBuilderJUnit4 givenEvent()
public void thenRun(Class<?> cls, String method)
cls - class to thenRunmethod - the method namepublic void thenRun(String cls, String method)
cls - class to thenRunmethod - the method namepublic int getLastExitCode()
public byte[] getStdErr()
public String getStdErrAsString()
public List<FnResult> getResults()
FnResults) from the function runtime outputpublic FnResult getOnlyResult()
IllegalStateException - if zero or more than one responses were producedCopyright © 2021. All rights reserved.