类 Theories
- java.lang.Object
- org.junit.runner.Runner
- org.junit.runners.ParentRunner<FrameworkMethod>
- org.junit.runners.BlockJUnit4ClassRunner
- org.junit.experimental.theories.Theories
- 所有已实现的接口:
Describable
,Filterable
,Sortable
public class Theories extends BlockJUnit4ClassRunner
The Theories runner allows to test a certain functionality against a subset of an infinite set of data points.A Theory is a piece of functionality (a method) that is executed against several data inputs called data points. To make a test method a theory you mark it with @Theory. To create a data point you create a public field in your test class and mark it with @DataPoint. The Theories runner then executes your test method as many times as the number of data points declared, providing a different data point as the input argument on each invocation.
A Theory differs from standard test method in that it captures some aspect of the intended behavior in possibly infinite numbers of scenarios which corresponds to the number of data points declared. Using assumptions and assertions properly together with covering multiple scenarios with different data points can make your tests more flexible and bring them closer to scientific theories (hence the name).
For example:
@RunWith(Theories.class) public class UserTest { @DataPoint public static String GOOD_USERNAME = "optimus"; @DataPoint public static String USERNAME_WITH_SLASH = "optimus/prime"; @Theory public void filenameIncludesUsername(String username) { assumeThat(username, not(containsString("/"))); assertThat(new User(username).configFileName(), containsString(username)); } }
This makes it clear that the user's filename should be included in the config file name, only if it doesn't contain a slash. Another test or theory might define what happens when a username does contain a slash.UserTest
will attempt to runfilenameIncludesUsername
on every compatible data point defined in the class. If any of the assumptions fail, the data point is silently ignored. If all of the assumptions pass, but an assertion fails, the test fails.Defining general statements as theories allows data point reuse across a bunch of functionality tests and also allows automated tools to search for new, unexpected data points that expose bugs.
The support for Theories has been absorbed from the Popper project, and more complete documentation can be found from that projects archived documentation.
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
Theories.TheoryAnchor
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected void
collectInitializationErrors(List<Throwable> errors)
Adds toerrors
a throwable for each problem noted with the test class (available fromParentRunner.getTestClass()
).protected List<FrameworkMethod>
computeTestMethods()
Returns the methods that run tests.Statement
methodBlock(FrameworkMethod method)
Returns a Statement that, when executed, either returns normally ifmethod
passes, or throws an exception ifmethod
fails.protected void
validateConstructor(List<Throwable> errors)
Adds toerrors
if the test class has more than one constructor, or if the constructor takes parameters.protected void
validateTestMethods(List<Throwable> errors)
Adds toerrors
for each method annotated with@Test
that is not a public, void instance method with no arguments.从类继承的方法 org.junit.runners.BlockJUnit4ClassRunner
createTest, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, possiblyExpectingExceptions, rules, runChild, testName, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateZeroArgConstructor, withAfters, withBefores, withPotentialTimeout
从类继承的方法 org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses
构造器详细资料
Theories
public Theories(Class<?> klass) throws InitializationError
方法详细资料
collectInitializationErrors
protected void collectInitializationErrors(List<Throwable> errors)
从类复制的说明:ParentRunner
Adds toerrors
a throwable for each problem noted with the test class (available fromParentRunner.getTestClass()
). Default implementation adds an error for each method annotated with@BeforeClass
or@AfterClass
that is notpublic static void
with no arguments.
validateConstructor
protected void validateConstructor(List<Throwable> errors)
从类复制的说明:BlockJUnit4ClassRunner
Adds toerrors
if the test class has more than one constructor, or if the constructor takes parameters. Override if a subclass requires different validation rules.
validateTestMethods
protected void validateTestMethods(List<Throwable> errors)
从类复制的说明:BlockJUnit4ClassRunner
Adds toerrors
for each method annotated with@Test
that is not a public, void instance method with no arguments.
computeTestMethods
protected List<FrameworkMethod> computeTestMethods()
从类复制的说明:BlockJUnit4ClassRunner
Returns the methods that run tests. Default implementation returns all methods annotated with@Test
on this class and superclasses that are not overridden.
methodBlock
public Statement methodBlock(FrameworkMethod method)
从类复制的说明:BlockJUnit4ClassRunner
Returns a Statement that, when executed, either returns normally ifmethod
passes, or throws an exception ifmethod
fails. Here is an outline of the default implementation:- Invoke
method
on the result ofcreateTest()
, and throw any exceptions thrown by either operation. - HOWEVER, if
method
's@Test
annotation has theexpecting
attribute, return normally only if the previous step threw an exception of the correct type, and throw an exception otherwise. - HOWEVER, if
method
's@Test
annotation has thetimeout
attribute, throw an exception if the previous step takes more than the specified number of milliseconds. - ALWAYS run all non-overridden
@Before
methods on this class and superclasses before any of the previous steps; if any throws an Exception, stop execution and pass the exception on. - ALWAYS run all non-overridden
@After
methods on this class and superclasses after any of the previous steps; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into aMultipleFailureException
. - ALWAYS allow
@Rule
fields to modify the execution of the above steps. ARule
may prevent all execution of the above steps, or add additional behavior before and after, or modify thrown exceptions. For more information, seeTestRule
- Invoke