View
665
Download
0
Category
Tags:
Preview:
Citation preview
Generation_P Consulting LtdGeneration_P Consulting LtdSoftware Solutions for the Travel IndustrySoftware Solutions for the Travel Industry
TDDTDDMake your life betterMake your life better
Antanina ShafranskayaAntanina ShafranskayaAntanina.Shafranskaya@generation_p.com
TDD: way to better life
Test before code!
TDD principals: think first
trivial first
test <=> what ; implementation <=> how
different levels of tests
red – green – refactor
red: behavior (responsibility oriented design)
green: coding
refactor: design, refactor
ping – pong
who has keyboard is a King!
through-test driving
TDD: Pair programming
TDD: Testing best practices
TDD: Test naming
TDD: Test naming
TDD: Exception processing
TDD: given-when-then
TDD: multiple asserts
TDD: Whole test system
unit testsintegration
acceptanceauto gui
manual
TDD: Unit tests
One test – one behavior
Independent: isolated class
Reliable
Fast
TDD: Integration and Acceptance Tests
integration: component communication with some isolation
acceptance: full system test, no isolation or mocks
client requirements
TDD: Mocks
fake – some little implementation
stub – return some predefined result
mock – object with programmed expectation
spy – stub with flow verification
TDD: Mockito
Easy to:
stub methods verify behavior
verify sequence of calls
Cannot:
mock final classes, methods
mock static
TDD: Mockito
@RunWith(MockitoJUnitRunner.class) annotation at the class-level of the test case
@Mock annotation at mocking objects init tested class in method annotated with @Before
when(mock.methodCall()).thenReturn(someResult); doThrow(exception).when(object).voidMethodCall();
verify(mock, times).methodCall() verifyZeroInteractions(mocks) inOrder(mock)
TDD: Mockito
TDD: PowerMock
Easy way to mock: static methods
final methods or classes
private methods
construction of new objects
partial mocking
TDD: PowerMock
@RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
@PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case.
PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class.
PowerMock.mockStaticPartial(ClassThatContainsStaticMethod.class, "methodName", ArgumentClass.class);
PowerMock.replay(ClassThatContainsStaticMethod.class) to change the class to replay mode.
PowerMock.verify(ClassThatContainsStaticMethod.class) to change the class to verify mode.
example: http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/easymock/junit4-test/src/test/java/samples/junit4/singleton/MockStaticTest.java
What to read?
Robert C. Martin “Clean Code: A Handbook of Agile Software Craftsmanship”
Joshua Kerievsky “Refactoring to Patterns” Steve Freeman, Nat Pryce “Growing Object-
Oriented Software, Guided by Tests”
Questions?
Thank you and good luck!Thank you and good luck!
References
http://www.slideshare.net/agileee/clean-tests
http://www.slideshare.net/wakaleo/junit-kung-fu-getting-more-out-of-your-unit-tests
http://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.html
http://code.google.com/p/powermock/
Recommended