## Summary of Changes in version 4.4 ##
JUnit is designed to efficiently capture developers' intentions about
their code, and quickly check their code matches those intentions.
Over the last year, we've been talking about what things developers
would like to say about their code that have been difficult in the
past, and how we can make them easier.
### assertThat ###
Two years ago, Joe Walnes built a [new assertion mechanism][walnes] on top of what was
then [JMock 1][]. The method name was `assertThat`, and the syntax looked like this:
[walnes]: http://joe.truemesh.com/blog/000511.html
[JMock 1]: http://www.jmock.org/download.html
assertThat(x, is(3));
assertThat(x, is(not(4)));
assertThat(responseString, either(containsString("color")).or(containsString("colour")));
assertThat(myList, hasItem("3"));
More generally:
assertThat([value], [matcher statement]);
Advantages of this assertion syntax include:
- More readable and typeable: this syntax allows you to think in terms of subject, verb, object
(assert "x is 3") rather than `assertEquals`, which uses verb, object, subject (assert "equals 3 x")
- Combinations: any matcher statement `s` can be negated (`not(s)`), combined (`either(s).or(t)`),
mapped to a collection (`each(s)`), or used in custom combinations (`afterFiveSeconds(s)`)
- Readable failure messages. Compare
assertTrue(responseString.contains("color") || responseString.contains("colour"));
// ==> failure message:
// java.lang.AssertionError:
assertThat(responseString, anyOf(containsString("color"), containsString("colour")));
// ==> failure message:
// java.lang.AssertionError:
// Expected: (a string containing "color" or a string containing "colour")
// got: "Please choose a font"
- Custom Matchers. By implementing the `Matcher` interface yourself, you can get all of the
above benefits for your own custom assertions.
- For a more thorough description of these points, see [Joe Walnes's
original post][walnes].
We have decided to include this API directly in JUnit.
It's an extensible and readable syntax, and it enables
new features, like [assumptions][] and [theories][].
[assumptions]: #assumptions
[theories]: #theories
Some notes:
- The old assert methods are never, ever, going away. Developers may
continue using the old `assertEquals`, `assertTrue`, and so on.
- The second parameter of an `assertThat` statement is a `Matcher`.
We include the Matchers we want as static imports, like this:
import static org.hamcrest.CoreMatchers.is;
or:
import static org.hamcrest.CoreMatchers.*;
- Manually importing `Matcher` methods can be frustrating. [Eclipse 3.3][] includes the ability to
define
"Favorite" classes to import static methods from, which makes it easier
(Search for "Favorites" in the Preferences dialog).
We expect that support for static imports will improve in all Java IDEs in the future.
[Eclipse 3.3]: http://www.eclipse.org/downloads/
- To allow compatibility with a wide variety of possible matchers,
we have decided to include the classes from hamcrest-core,
from the [Hamcrest][] project. This is the first time that
third-party classes have been included in JUnit.
[Hamcrest]: http://code.google.com/p/hamcrest/
- JUnit currently ships with a few matchers, defined in
`org.hamcrest.CoreMatchers` and `org.junit.matchers.JUnitMatchers`.
To use many, many more, consider downloading the [full hamcrest package][].
[full hamcrest package]: http://hamcrest.googlecode.com/files/hamcrest-all-1.1.jar
- JUnit contains special support for comparing string and array
values, giving specific information on how they differ. This is not
yet available using the `assertThat` syntax, but we hope to bring
the two assert methods into closer alignment in future releases.
<a name="assumptions" />
### Assumptions ###
Ideally, the developer writing a test has control of all of the forces that might cause a test to fail.
If this isn't immediately possible, making dependencies explicit can often improve a design.
For example, if a test fails when run in a different locale than the developer intended,
it can be fixed by explicitly passing a locale to the domain code.
However, sometimes this is not desirable or possible.
It's good to be able to run a test against the code as it is currently written,
implicit assumptions and all, or to write a test that exposes a known bug.
For these situations, JUnit now includes the ability to express "assumptions":
import static org.junit.Assume.*
@Test public void filenameIncludesUsername() {
assumeThat(File.separatorChar, is('/'));
assertThat(new User("optimus").configFileName(), is("configfiles/optimus.cfg"));
}
@Test public void correctBehaviorWhenFilenameIsNull() {
assumeTrue(bugFixed("13356")); // bugFixed is not included in JUnit
assertThat(parse(null), is(new NullDocument()));
}
With this release, a failed assumption will lead to the test being marked as passing,
regardless of what the code below the assumption may assert.
In the future, this may change, and a failed assumption may lead to the test being ignored:
however, third-party runners do not currently allow this option.
We have included `assumeTrue` for convenience, but thanks to the
inclusion of Hamcrest, we do not need to create `assumeEquals`,
`assumeSame`, and other analogues to the `assert*` methods. All of
those functionalities are subsumed in `assumeThat`, with the appropriate
matcher.
A failing assumption in a `@Before` or `@BeforeClass` method will have the same effect
as a failing assumption in each `@Test` method of the class.
<a name="theories" />
### Theories ###
More flexible and expressive assertions, combined with the ability to
state assumptions clearly, lead to a new kind of statement of intent,
which we call a "Theory". A test captures the intended behavior in
one particular scenario. A theory captures some aspect of the
intended behavior in possibly
infinite numbers of potential scenarios. 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 run `filenameIncludesUsername` on
every compatible `DataPoint` 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.
The support for Theories has been absorbed from the [Popper][]
project, and [more complete documentation][popper-docs] can be found
there.
[Popper]: http://popper.tigris.org
[popper-docs]: http://popper.tigris.org/tutorial.html
Defining general statements in this way can jog the developer's memory
about other potential data points and tests, also allows [automated
tools][junit-factory] to [search][my-blog] for new, unexpected data
points that expose bugs.
[junit-factory]: http://www.junitfactory.org
[my-blog]: http://shareandenjoy.saff.net/2007/04/popper-and-junitfactory.html
### Other changes ###
This release contains other bug fixes and new features. Among them:
- Annotated descriptions
Runner UIs, Filters, and Sorters operate on Descriptions of test
methods and test classes. These Descriptions now include the
annotations on the original Java source element, allowing for richer
display of test results,
没有合适的资源?快使用搜索试试~ 我知道了~
junit4.4.zip
共526个文件
class:309个
java:116个
html:76个
需积分: 50 16 下载量 50 浏览量
2008-09-22
13:03:07
上传
评论
收藏 794KB ZIP 举报
温馨提示
用junit做测试的基本步骤是先写TestCase,然后在用 TestSuite(测试集)去管理和运行测试
资源推荐
资源详情
资源评论
收起资源包目录
junit4.4.zip (526个子文件)
AssertionTest.class 12KB
AnnotationTest.class 9KB
TimeoutTest.class 5KB
MoneyTest.class 5KB
ForwardCompatibilityTest.class 5KB
SuiteTest.class 5KB
TestCaseTest.class 5KB
MoneyTest.class 5KB
BaseDescription.class 5KB
MoneyBag.class 4KB
AssumptionTest.class 4KB
ParameterizedTestTest.class 4KB
CoreMatchers.class 4KB
ComparisonCompactorTest.class 4KB
SingleMethodTest.class 4KB
TestMethodTest.class 4KB
TextFeedbackTest.class 3KB
BothTest.class 3KB
SuiteTest.class 3KB
AssertTest.class 3KB
InitializationErrorForwardCompatibilityTest.class 3KB
AnnotatedDescriptionTest.class 3KB
Money.class 3KB
ForwardCompatibilityPrintingTest.class 3KB
ListTest.class 3KB
TextRunnerTest.class 3KB
ParameterizedTestMethodTest.class 3KB
DescribedAs.class 3KB
JUnitCoreTest.class 2KB
TextListenerTest.class 2KB
ParameterizedTestMethodTest$EverythingWrong.class 2KB
SuiteMethodTest.class 2KB
TestMethodTest$EverythingWrong.class 2KB
SortableTest$TestClassRunnerIsSortable.class 2KB
AllTests.class 2KB
ExtensionTest.class 2KB
ExpectedTest.class 2KB
ListTest.class 2KB
AllOf.class 2KB
AnyOf.class 2KB
IsEqual.class 2KB
StackFilterTest.class 2KB
RunWithTest.class 2KB
TestListenerTest.class 2KB
TestListenerTest.class 2KB
CommandLineTest.class 2KB
Is.class 2KB
SortableTest$TestClassRunnerIsSortableWithSuiteMethod.class 2KB
RepeatedTestTest.class 2KB
SuiteDescriptionTest.class 2KB
AssumptionViolatedExceptionTest.class 2KB
OldTestClassRunnerTest.class 2KB
InitializationErrorForwardCompatibilityTest$ErrorRememberingListener.class 2KB
ActiveTestTest.class 2KB
RunnerTest.class 2KB
IsNot.class 2KB
AllTestsTest.class 2KB
IsAnything.class 2KB
ForwardCompatibilityTest$1.class 2KB
OldTestClassAdaptingListenerTest.class 2KB
IsNull.class 2KB
EnclosedTest.class 2KB
StringDescription.class 2KB
TestClassMethodsRunnerTest.class 2KB
ParameterizedTestTest$FibonacciTest.class 2KB
FloatAssertTest.class 2KB
ListenerTest.class 1KB
SimpleTest.class 1KB
ForwardCompatibilityTest$BeforeClassTest.class 1KB
TestImplementorTest$DoubleTestCase.class 1KB
AllTests.class 1KB
DoublePrecisionAssertTest.class 1KB
FailedConstructionTest.class 1KB
IsInstanceOf.class 1KB
AnnotationTest$SubInheritance.class 1KB
IsSame.class 1KB
AllTests.class 1KB
UserStopTest.class 1KB
PreJUnit4TestCaseRunnerTest.class 1KB
SelfDescribingValueIterator.class 1KB
ParameterizedTestTest$BeforeAndAfter.class 1KB
RunWithTest$ExampleRunner.class 1KB
SortableTest$UnsortableRunnersAreHandledWithoutCrashing.class 1KB
InitializationErrorForwardCompatibilityTest$CantInitialize.class 1KB
SingleMethodTest$ParameterizedOneTimeSetup.class 1KB
AnnotationTest$OrderTest.class 1KB
TextRunnerSingleMethodTest.class 1KB
SimpleTest.class 1KB
ArrayIterator.class 1KB
SortableTest.class 1KB
AnnotationTest$SuperInheritance.class 1KB
TestImplementorTest.class 1KB
SortableTest$UnsortableRunnersAreHandledWithoutCrashing$UnsortableRunner.class 1KB
PreJUnit4TestCaseRunnerTest$1.class 1KB
AnnotationTest$RunAllAfters.class 1KB
AnnotationTest$RunAllAfterClasses.class 1KB
SuiteTest$AllWithBeforeAndAfterClass.class 1KB
ForwardCompatibilityTest$MarkerRunner.class 1KB
ParameterizedTestTest$WrongElementType.class 1KB
ParameterizedTestTest$IncorrectTest.class 1KB
共 526 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
sknife123
- 粉丝: 3
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功