1. junit4 annotation Summary
2. Test method summary
3. basic use of assertthat statements
This statement replaces the original asserted statement and presents the asserted statement in a more readable form.
During use, note the following: (some versions of eclipse cannot be imported automatically)
Import static org. JUnit. Assert. assertthat;
Import static org. hamcrest. corematchers .*;
Common statements are as follows:
The allof statement indicates that all tests in parentheses must pass and the assertion is true.
The any statement indicates that the result is the type of a class and its subclass.
The anyof statement indicates that the assertion passes as long as any statement in the brackets is true.
The explain to statement indicates that the two values are "equal ".
The is statement represents a judgment action in most cases, for example, the preceding examples. However, when you pass in a parameter such as "XXX. Class", you can also replace instanceof. In addition, you can replace similar.
The not statement is "reverse ".
The notnullvalue statement is used to judge whether it is null. It is opposite to the nullvalue statement.
The sameinstance statement indicates "=", that is, whether to point to the same object instance.In addition, other common statements are as follows:
Assertthat (testedstring, inclutoignoringcase (expectedstring);/** assert toignoringwhitespace match asserted that the tested string testedstring * is equal to expectedstring when any space at the beginning and end is ignored. * Note: spaces in the string cannot be ignored */assertthat (testedstring, inclutoignoringwhitespace (expectedstring);/** containsstring matching character asserted that the tested string testedstring contains the sub-string substring **/assertthat (testedstring, containsstring (substring);/** endswith match asserted the tested string testedstring ended with a sub-string suffix */assertthat (testedstring, endswith (suffix )); /** startswith match to assert that the tested string testedstring starts with a sub-string prefix */assertthat (testedstring, startswith (prefix )); /** the closeto matched character asserted that the number of Float testeddouble values under test is between 20.0 and 0. */assertthat (testeddouble, closeto (20.0, 0.5) within the 5 range;/** greaterthan matches the asserted value testednumber greater than 16.0 */assertthat (testednumber, greaterthan (16.0);/** the lessthan match asserted that the tested value testednumber is smaller than 16.0 */assertthat (testednumber, lessthan (16.0 )); /** greaterthanorequalto match to assert that the tested value testednumber is greater than or equal to 16.0 */assertthat (testednumber, greaterthanorequalto (16.0 )); /** lessthanorequalto match to assert that the testednumber tested is less than or equal to 16.0 */assertthat (testednumber, lessthanorequalto (16.0 )); /** hasentry match asserted that the tested map object MapObject contains an entry item whose key value is "key" and whose element value is "value" */assertthat (MapObject, hasentry ("key", "value");/** the hasitem match indicates that the iterableobject of the tested iteration object contains element items, and the test passes */assertthat (iterableobject, hasitem (element);/** haskey match to assert that the tested map object MapObject contains the key value "key" */assertthat (MapObject, haskey ("key ")); /** hasvalue matching operator asserted that the tested map object MapObject contains the element value */assertthat (MapObject, hasvalue (value ));Example of assertthat:
import org.hamcrest.Description;import org.hamcrest.Matcher;import org.hamcrest.StringDescription;import org.junit.Test;import static org.hamcrest.CoreMatchers.*;import static org.junit.Assert.assertThat;public class HamcrestExamples { @Test public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception { assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello")))); } @Test public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception { assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class))))); } @Test public void anyExampleChecksThatClassIsOfSameType() throws Exception { assertThat("Hello", is(any(String.class))); } @Test public void anyExampleShowsStringIsAlsoAnObject() throws Exception { assertThat("Hello", is(any(Object.class))); } @Test public void anyOfExampleReturnsTrueIfOneMatches() throws Exception { assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye")))); } @Test public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception { assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye"))))); } @Test public void anythingExampleAlwaysReturnsTrue() throws Exception { assertThat("Hello", is(anything())); } // Feels very esoteric and not for typical usage used to override the description @Test public void describedAsExample() throws Exception { Matcher< ?> matcher = describedAs("My Description", anything()); Description description = new StringDescription().appendDescriptionOf(matcher); assertThat("My Description", is(description.toString())); } @Test public void equalToExampleAddingTwoPlusTwo() throws Exception { assertThat(2 + 2, is(equalTo(4))); } @Test public void instanceOfExampleForString() throws Exception { assertThat("Hello", is(instanceOf(String.class))); } @Test public void isExampleShortCutForIsInstanceOfClass() throws Exception { assertThat("Hello", is(String.class)); assertThat("Hello", instanceOf(String.class)); } @Test public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception { assertThat("Hello", is(is(is(notNullValue())))); } @Test public void isExampleShortCutForIsEqualTo() throws Exception { assertThat("Hello", is("Hello")); assertThat("Hello", equalTo("Hello")); } @Test public void notExampleJustInvertsExpression() throws Exception { assertThat("Hello", is(not(instanceOf(Integer.class)))); } @Test public void notNullValueExampleForString() throws Exception { assertThat("Hello", is(notNullValue())); } @Test public void notNullValueExampleForAClass() throws Exception { assertThat("Hello", is(notNullValue(Object.class))); } @Test public void nullValueExampleWithANull() throws Exception { assertThat(null, is(nullValue())); } @Test public void nullValueExampleWithANullType() throws Exception { Integer nothing = null; assertThat(nothing, is(nullValue(Integer.class))); } @Test public void sameInstanceExample() throws Exception { Object object = new Object(); Object sameObject = object; assertThat(object, is(sameInstance(sameObject))); }}
4. Use test suite to add the class to be tested in @ suite. suiteclasses (), for example, dice4test. Class. Public class alltests is left empty for compilation by the compiler.
In addition, use the eclemma plug-in to measure the test code line coverage and branch coverage in eclipse.
References:
1. http://edgibbs.com/junit-4-with-hamcrest/
2. www.vogella.de/articles/JUnit/article.html