Upload
phamhanh
View
246
Download
0
Embed Size (px)
Citation preview
Yay! An Ad!Java Testing with JUnit
www.ociweb.com/training/Java-Testing-with-JUnit
July 9 - July 18T/Th, 5:30 – 8:30
Mention the JUG and get 10% off!
Thursday, June 6, 13
JUnit StatusRelease Date4.11 Nov 14, 20124.10 Sep 29, 20124.9 Aug 22, 20114.8 Nov 30, 20094.7 Jul 27, 20094.6 Apr 13, 20094.5 Aug 8, 20084.4 Jul 19, 20074.3 Mar 20, 20074.2 Nov 16, 20064.1 May 2, 20064.0 Feb 16, 20063.8.2 Mar 3, 20063.8.1 Sep 4, 2002
Thursday, June 6, 13
JUnit Annotations• @Before
• @BeforeClass
• @After
• @AfterClass
• @Ignore
• @Test
• @Test(timeout=500)
• @Test(expected=<Exception>.class)
Thursday, June 6, 13
Everyone Uses • @Before fka setUp()
• @After fka tearDown()
• @Test fka test<something>()
Thursday, June 6, 13
Tonight• Assumptions
• Categories
• Parameterized Testing
• Parameter
• Theory
• Rules
• Hamcrest Matchers
Thursday, June 6, 13
Everyone Uses Asserts• assertEquals(...) / assertArrayEquals(...)
• assertNull(...) / assertNonNull(...)
• assertSame(...) / assertNotSame(...)
• assertTrue(...) / assertFalse(...)
• fail(“Not implemented yet.”);
Thursday, June 6, 13
Not a whole lot of folk useassertThat([java.lang.string Reason],
T actual, org.hamcrest.matcher<T> matcher)
(more on Hamcrest in just a bit)
Thursday, June 6, 13
Hardly anyone uses • Assume:
• assumeNoException(...)
• assumeNotNull (...)
• assumeThat(...) // used a lot in @Theory
• assumeTrue(...)
Unlike Asserts, if an assumption is wrong, the test is halted and ignored.
Thursday, June 6, 13
@Category
• Canonical example is fast and slow
• Browser: Firefox, Chrome, Opera, others
• Database: DB2, Oracle, &c...
Gives the ability to group tests.- exclude a group- include a group
Thursday, June 6, 13
Parameter.class• Pros:
• Quick to write
• No need to establish relationships
• Cons:
• Must detail all data
Thursday, June 6, 13
Parameter.class• Static Method to Generate Test
Data
• Single Constructor that stores test data
• Test(s)
Thursday, June 6, 13
Theory.class• Cons:
• Not (typically) quick to write
• Must understand data relationships
• Pros?:
• Don’t need to detail all data
Thursday, June 6, 13
Rules• Add information to test results.
• Access information about a test before it is run
• Modify a test before running it.
• Modify test results.
Thursday, June 6, 13
Hamcrest Matchers• hamcrest.org (/JavaHamcrest/javadoc/1.3/)
• General purpose matching library for Java, Python, Ruby, Objective-C, PHP, and Erlang (What? No Haskell?)
• Provides readability
• code
• error messages
Thursday, June 6, 13
Hamcrest Cheat Sheet
http://www.marcphilipp.de/downloads/posts/2013-01-02-hamcrest-quick-reference/Hamcrest-1.3.pdf
Thursday, June 6, 13
Matcher Types• General Purpose:
is(T), not(T), any(T), isIn(T), instanceOf(Class)
• Strings:
startsWith(S), endsWith(S)equalToIgnoringCase(S), equalToIgnoringWhiteSpace(S)isEmptyString(), isEmptyOrNullString()
Thursday, June 6, 13
More matchers• Iterables:
contains(E ...) // (oh, boy, howdy...)
containsInAnyOrder(T ...)
hasItem(T) // Yay!
hasItems(T ...)
Thursday, June 6, 13