public final class SmithyTestSuite
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
SmithyTestSuite.Error
Thrown when errors are encountered in the test runner.
|
static class |
SmithyTestSuite.Result
Value result of executing the test suite.
|
Modifier and Type | Method and Description |
---|---|
SmithyTestSuite |
addTestCase(SmithyTestCase testCase)
Adds a test case to the test suite.
|
SmithyTestSuite |
addTestCasesFromDirectory(java.nio.file.Path modelDirectory)
Adds test cases by crawling a directory and looking for model files
that end with ".json" and ".smithy".
|
SmithyTestSuite |
addTestCasesFromUrl(java.net.URL url)
Convenience method for supplying a directory using a class loader.
|
static java.util.stream.Stream<java.lang.Object[]> |
defaultParameterizedTestSource(java.lang.Class<?> contextClass)
Factory method used to easily created a JUnit 5
ParameterizedTest
MethodSource based on the given Class . |
SmithyTestSuite.Result |
run()
Executes the test runner.
|
SmithyTestSuite.Result |
run(java.util.concurrent.ExecutorService executorService)
Executes the test runner with a specific
ExecutorService . |
static SmithyTestSuite |
runner()
Creates a new Smithy test suite.
|
SmithyTestSuite |
setModelAssemblerFactory(java.util.function.Supplier<ModelAssembler> modelAssemblerFactory)
Sets a custom
ModelAssembler factory to use to create a
ModelAssembler for each test case. |
java.util.stream.Stream<java.util.concurrent.Callable<SmithyTestCase.Result>> |
testCaseCallables()
Creates a
Stream of Callable objects that can be used
to execute each test case. |
public static SmithyTestSuite runner()
public static java.util.stream.Stream<java.lang.Object[]> defaultParameterizedTestSource(java.lang.Class<?> contextClass)
ParameterizedTest
MethodSource
based on the given Class
.
This method assumes that there is a resource named errorfiles
relative to the given class that contains test cases. It also assumes
validators and traits should be loaded using the ClassLoader
of the given contextClass
, and that model discovery should be
used using the given contextClass
.
Each returns Object[]
contains the filename of the test as
the first argument, followed by a Callable<SmithyTestCase.Result>
as the second argument. All a parameterized test needs to do is call
call
on the provided Callable
to execute the test and
fail if the test case is invalid.
For example, the following can used as a unit test:
import java.util.concurrent.Callable;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.smithy.model.validation.testrunner.SmithyTestCase;
import software.amazon.smithy.model.validation.testrunner.SmithyTestSuite;
public class TestRunnerTest {
\@ParameterizedTest(name = "\{0\}")
\@MethodSource("source")
public void testRunner(String filename, Callable<SmithyTestCase.Result> callable) throws Exception {
callable.call();
}
public static Stream<?> source() {
return SmithyTestSuite.defaultParameterizedTestSource(TestRunnerTest.class);
}
}
contextClass
- The class to use for loading errorfiles and model discovery.MethodSource
return value.public SmithyTestSuite addTestCase(SmithyTestCase testCase)
testCase
- Test case to add.public SmithyTestSuite addTestCasesFromDirectory(java.nio.file.Path modelDirectory)
See SmithyTestCase.fromModelFile(java.lang.String)
for a description of how
the errors file is expected to be formatted.
modelDirectory
- Directory that contains models.SmithyTestCase.fromModelFile(java.lang.String)
public SmithyTestSuite addTestCasesFromUrl(java.net.URL url)
url
- URL that contains models.java.lang.IllegalArgumentException
- if a non-file scheme URL is provided.addTestCasesFromDirectory(java.nio.file.Path)
public SmithyTestSuite setModelAssemblerFactory(java.util.function.Supplier<ModelAssembler> modelAssemblerFactory)
ModelAssembler
factory to use to create a
ModelAssembler
for each test case.
The supplier must return a new instance of a Model assembler each time it is called. Model assemblers are mutated and execute in parallel.
modelAssemblerFactory
- Model assembler factory to use.public java.util.stream.Stream<java.util.concurrent.Callable<SmithyTestCase.Result>> testCaseCallables()
Stream
of Callable
objects that can be used
to execute each test case.
The SmithyTestCase.Result.unwrap()
method must be called on
the result of each callable in order to actually assert that the test
case result is OK.
public SmithyTestSuite.Result run()
java.lang.Error
- if the validation events do not match expectations.public SmithyTestSuite.Result run(java.util.concurrent.ExecutorService executorService)
ExecutorService
.
Tests ideally should use JUnit 5's ParameterizedTest as described
in parameterizedTestSource()
. However, this method can be
used to run tests in parallel in other scenarios (like if you aren't
using JUnit, or not running tests cases during unit tests).
executorService
- Executor service to execute tests with.java.lang.Error
- if the validation events do not match expectations.