Download Unit Testing with JUnit and EasyMock: A Comprehensive Guide and more Study notes Computer Science in PDF only on Docsity!
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
17 – Advanced Testing: EasyMock
CMPSCI 220 (291A)
Programming Methodology
Spring 2009
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Deeper into Unit testing
Idea of Unit Testing is to test a single module
Might be a class, or just a method
Generally involves a series of tests, even forjust one method
Quick review of JUnit
Issue: Testing a highly embedded module
One solution:
mocking
A sample tool:
EasyMock
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
What to put in a test method?
Need to call method under test, of course!
Make a JUnit assertion about the result
assertTrue(boolean), assertFalse(boolean)
assertEquals(int,int), etc.
assertNull(expr), assertNotNull(expr)
assertSame(obj1,obj2),assertNotSame(o1,o2)
assertArrayEquals (int[],int[]), etc.
fail(message)
… there are a few more
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Testing pure functions: easy
Determine interesting argument values
Determine result for each argument list
Make a test for each case
Calls functions with arguments
Checks result
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Consider testing a Listimplementation
Could be ArrayList, LinkedList, etc.
Has a large number of operations
Get, set, add, remove, …
Agreggate: addAll, removeAll, …
Each has special / boundary cases
Empty list; operations at/near ends; etc.
Make sure exceptions thrown when should be
What types T to test?
What about iterators?
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Consider testing an AVL treeimplementation
There are all the black box tests
But must also test
All the single rotations
All the double rotations
At various levels in a tree
Similar considerations apply to B-trees, etc.
May want to add special constructors
Create tree of desired shape
May want custom validators
Make sure tree obeys all the rules (order, levels, etc.)
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Testing a GUI
Automation demands no user input whiletests run
Need to
inject
“fake” input events
Might come from a recorded trace …
Which needs a trace and playback facility
Might be synthesized
Need to
capture
and check output events
User need not watch and validate visually
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Biggest challenges
Testing real-time systems
Need to inject events at particular times
Need to check when responses occur
Timeout is too primitive for most cases
Testing embedded / robotic systems
Can be dangerous to test in real installation
Need to “fake” a whole framework
Framework needs to react like the real thing
Physics, etc. – can be very complex itself
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Some of our answers
Time not too hard to deal with
Fake calls to update(milliseconds), render()
Initialization?
Don’t want game to
run,
but
Need Golden T stuff initialized enough
Need to fake input (keyboard, mouse)
Need to observe internal events
Need to observe output events
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
Some needs
Example: initResources() of GameMenu
Should have sprites for <, >, Play, and Quit
Should have certain levels in the levels array
Sprites need to have been added to the playfield
Background should be set, should be 800x
Testing the levels logic requires
A testing directory populated with levels
A way to point the game menu to that directory
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
EasyMock: A mocking package forJava and JUnit
http://easymock.org
Creating a mock object:PlayField mockField = createMock(PlayField.
class
ImageBackground mockBack = createMock(ImageBackground.
class
Then:
Indicate
expectations:
“record” what
ought
to happen
Run things
Check that they
actually
happened that way
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
EasyMock: Expectations
Indicating some expected calls:expect(mockBack.getHeight());expect(mockBack.getWidth());
In actuality, need also to say what to return:expect(mockBack.getHeight()).andStubReturn(600);expect(mockBack.getWidth()).andStubReturn(800);
Order can matter:createStrictMock(PlayField.
class
); // demands exact order
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
EasyMock: Winding up
Some things are checked as you go
But also need to
verify
that all steps have occurred:
verify(mock1, mock2, …);
Generally invoke on same mocks as for replay
U
U
NIVERSITY OFNIVERSITY OF
M
M
ASSACHUSETTS ASSACHUSETTS
, A
, A
MHERST MHERST •
Department of Computer Science Department of Computer Science
EasyMock: Kinds of things you can“expect” / check
Argument checking:
anyBoolean(), anyByte(), etc., including anyObject()
isA(class), isNull(), notNull(), same(object)
Arithmetic: eq(v), geq(v), leq(v), lt(v), gt(v), not(v)
eq(v) and not(v) also work for Object types
String: contains(sub), startsWith(prefix), endsWith(suffix),matches(regexp)
Comparison: value compares as desired with given comparator
Can combine: and(chk1,chk2), or(chk1,chk2), not(chk)