public class DiffRepository extends Object
Loads files containing test input and output into memory. If there are differences, writes out a log file containing the actual output.
Typical usage is as follows. A test case class defines a method
package com.acme.test; public class MyTest extends TestCase { public DiffRepository getDiffRepos() { return DiffRepository.lookup(MyTest.class); } @Test void testToUpper() { getDiffRepos().assertEquals("${result}", "${string}"); } @Test void testToLower() { getDiffRepos().assertEquals("Multi-line\nstring", "${string}"); } }
There is an accompanying reference file named after the class,
src/test/resources/com/acme/test/MyTest.xml:
<Root> <TestCase name="testToUpper"> <Resource name="string"> <![CDATA[String to be converted to upper case]]> </Resource> <Resource name="result"> <![CDATA[STRING TO BE CONVERTED TO UPPER CASE]]> </Resource> </TestCase> <TestCase name="testToLower"> <Resource name="result"> <![CDATA[multi-line string]]> </Resource> </TestCase> </Root>
If any of the test cases fails, a log file is generated, called
build/diffrepo/test/com/acme/test/MyTest_actual.xml,
containing the actual output.
The log file is otherwise identical to the reference log, so once the log file has been verified, it can simply be copied over to become the new reference log:
cp build/diffrepo/test/com/acme/test/MyTest_actual.xml
src/test/resources/com/acme/test/MyTest.xml
If a resource or test case does not exist, DiffRepository
creates them in the log file. Because DiffRepository is so forgiving, it is
very easy to create new tests and test cases.
The lookup(java.lang.Class<?>) method ensures that all test cases share the same
instance of the repository. This is important more than one test case fails.
The shared instance ensures that the generated
build/diffrepo/test/com/acme/test/MyTest_actual.xml
file contains the actual for both test cases.
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
DiffRepository.Filter
Callback to filter strings before returning them.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
amend(String expected,
String actual) |
void |
assertEquals(String tag,
String expected,
String actual) |
static DiffRepository |
castNonNull(@Nullable DiffRepository diffRepos)
Returns the diff repository, checking that it is not null.
|
void |
checkActualAndReferenceFiles() |
String |
expand(String tag,
String text)
Expands a string containing one or more variables.
|
static DiffRepository |
lookup(Class<?> clazz)
Finds the repository instance for a given class, with no base
repository or filter.
|
static DiffRepository |
lookup(Class<?> clazz,
DiffRepository baseRepository,
DiffRepository.Filter filter,
int indent)
Finds the repository instance for a given class.
|
void |
set(String resourceName,
String value)
Sets the value of a given resource of the current test case.
|
public void checkActualAndReferenceFiles()
public static DiffRepository castNonNull(@Nullable DiffRepository diffRepos)
If it is null, throws IllegalArgumentException with a message
informing people that they need to change their test configuration.
public String expand(String tag, String text)
public void set(String resourceName, String value)
resourceName - Name of the resource, e.g. "sql"value - Value of the resourcepublic static DiffRepository lookup(Class<?> clazz)
clazz - Test case classpublic static DiffRepository lookup(Class<?> clazz, DiffRepository baseRepository, DiffRepository.Filter filter, int indent)
It is important that all test cases in a class share the same repository instance. This ensures that, if two or more test cases fail, the log file will contains the actual results of both test cases.
The baseRepository parameter is useful if the test is an
extension to a previous test. If the test class has a base class which
also has a repository, specify the repository here. DiffRepository will
look for resources in the base class if it cannot find them in this
repository. If test resources from test cases in the base class are
missing or incorrect, it will not write them to the log file -- you
probably need to fix the base test.
Use the filter parameter if you expect the test to
return results slightly different than in the repository. This happens
if the behavior of a derived test is slightly different than a base
test. If you do not specify a filter, no filtering will happen.
clazz - Test case classbaseRepository - Base repositoryfilter - Filters each string returned by the repositoryindent - Indent of the XML file (usually 2)Copyright © 2012-2023 Apache Software Foundation. All Rights Reserved.