Java Testing
Concepts
- Characterization test
- Characterization Testing static methods using different techniques
- Mocks aren’t Stubs
JUnit
Method Level Annotations
@BeforeClass
- Marks the method to execute before the test case class begins@AfterClass
- Marks the method to execute after the test case class completes@Before
- Marks the method to execute before the test case begins@After
- Marks the method to execute after the test case completes@Test
- Marks the method as a test case@Test(exception = NullPointerException.class)
- Marks the method to expect the given exception@Test(timeout=1)
- Marks the maximum time in milliseconds the test case can take to run@Ignore("Comment")
- Mark a method to ignore from testing
Class Level Annotations
- Runner - is a class which runs tests. JUnit provides a bunch of runners out-of-the-box:
- JUnit4 - starts the test case as a Junit4 test case
- Parameterized - runs the same test method with different parameters
- Suite - executes all the @Test annotated methods in a class
@RunWith(runner.class)
- Custom runner classes can be provided with this annotation@SuiteClasses(value={foo1.class, foo2.class})
- Suite is a container to combine more than one test cases for grouping and invocation
- When there are 2 test methods in a class, JUnit4 uses a Suite by default.
- Runner invokes the Suite. Ordering or execution is up to the Suite implementation.
Parameterized Tests
Parameterized.class
is a Runner class provided by JUnit4- Same test method is tested with different parameters returned by the method defined by
@Parameters
. - Signature of parmeters method should be
@Parameters public static Collection
. Collection elements should be arrays of same length - Variables used in the test method should be defined as instance variables in the test class and there should be public constructor setting all these instance variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Parameterized.class is a Runner class provided by JUnit4 Same test method is tested with different parameters returned by the method defined by@Parameters. Signature of parmeters method should be@Parameters public static Collection. Collection elements should be arrays of same length Variables used in the test method should be defined as instance variables in the test class and there should be public constructor setting all these instance variables.
Mockito
- On mock object, all methods that return value returns null by default
- Mockito follows
Arrange - Act - Assert
unlike other mock frameworks which followExpect-Run-Verify
pattern. - What is not possible in Mockito compared to others?
- Testing static methods (check PowerMockito)
- Mocking final classes
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
To learn more about Argument Matchers: http://docs.mockito.googlecode.com/hg/latest/org/mockito/Matchers.html
Hamcrest
Hamcrest is a Google library shipped with JUnit 4.x to make the assertions simpler.
Method | Description | ||
---|---|---|---|
anything |
Matches absolutely anything. Useful in some cases where you want to make the assert statement more readable. | ||
is |
Is used only to improve the readability of your statements. | ||
allOf |
Checks to see if all contained matchers match (just like the && operator). | ||
anyOf |
Checks to see if any of the contained matchers match (like the | operator). | |
not |
Traverses the meaning of the contained matchers (just like the ! operator in Java). | ||
instanceOf,isCompatibleType |
Match whether objects are of compatible type (are instances of one another). | ||
sameInstance |
Tests object identity. | ||
notNullValue,nullValue |
Tests for null values (or non-null values). | ||
hasProperty |
Tests whether a JavaBean has a certain property. | ||
hasEntry,hasKey,hasValue |
Tests whether a given Map has a given entry, key, or value. | ||
hasItem,hasItems |
Tests a given collection for the presence of an item or items. | ||
closeTo,greaterThan,greaterThanOrEqual,lessThan,lessThanOrEqual |
Test whether given numbers are close to, greater than, greater than or equal to, less than, or less than or equal to a given value. | ||
equalToIgnoringCase |
Tests whether a given string equals another one, ignoring the case. | ||
equalToIgnoringWhiteSpace |
Tests whether a given string equals another one, by ignoring the white spaces. | ||
containsString,endsWith,startWith |
Test whether the given string contains, starts with, or ends with a certain string |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Spock Framework
Spock is a testing and specification framework for Java and Groovy applications.
- Specification - compare to TestCase/GroovyTestCase. Instructs JUnit to run Sputnik (custom JUnitRunner)
- Every member field in test class is reinitialized for every test unless marked as @Shared.
- Feature - compare to test method or @Test
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 |
|
Bibliography
- Books
- xUnit Patterns
- BDD
- Apache JMeter - Load Testing tool
- http://tedyoung.me/2011/01/23/junit-runtime-tests-custom-runners/
- http://stackoverflow.com/questions/13489388/how-junit-rule-works
- http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/
- http://stackoverflow.com/questions/4055022/mark-unit-test-as-an-expected-failure-in-junit/8092927#8092927
- http://java.dzone.com/articles/dry-use-junit-rule-instead