This document provides an overview of test-driven development (TDD) techniques including unit testing with JUnit, behavior-driven development (BDD) with Cucumber, test automation, and using mock objects with Mockito. It defines TDD and its processes, describes tools like JUnit, Cucumber and Mockito, and provides references for further reading on testing practices.
2. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Outline
1 Introduction
2 Unit Tests
JUnit
3 Auto Tests
Cucumber
4 Mock Objects
Mockito
5 References
3. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
TDD
Definition
A software development process.
Based on text-first concept in Extreme Programming.
Relies on the repetition of a very short development
cycle.
Also called Red-Greed-Refactor
4. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
TDD
Life-cycle
1 Write a test
Before implementation
2 Run the test
Should fail
3 Write the implementation code
Just enough to pass the test
Not perfect
4 Run all tests
All should pass
5 Refactor
Optimize code without introducing new features
Should not make any tests fail
Should not include new tests
6 Repeat
5. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
TDD
Features
All about speed: short unit tests (JUnit), imperfect but
fast code.
Forces us to think before coding.
Allows us to develop fast without being afraid to break
something.
Allows us to refactor with confidence.
External dependencies should be mocked (mockito).
So that we can test without other parts implemented.
So out tests run faster (For instance, a DB connection).
This forces us to separate concerns (DB instance can
be replaced easily).
A note
Tools that we will introduce today do not belong to TDD. You
can use them with any process.
6. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
TDD
Behavior-Driven Development (BDD)
An extension to TDD.
In TDD, the tests may target class and methods (state)
instead of their actual purpose (behaviour).
Purpose of the user story is clearly related to the
business outcomes.
Single notation and ubiquitous language to improve
communication (Gherkin language).
Scenario’s are used instead of tests to avoid
technicalities.
Test can be generated automatically (cucumber).
7. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Unit Testing
Definition
A white-box testing method for individual units of code.
A unit is the smallest testable part of an application.
Function in procedural programming
Interface, class or method in object-oriented
programming.
Should be easy to write and fast to execute.
Each unit test should be independent of others.
Helps to identify problems early and to refactor easily.
8. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
JUnit
JUnit is a test framework which uses annotations to
identify methods that specify a test.
@Test, @Test (expected = Exception.class),
@Test(timeout=100), @Before, @After, @BeforeClass,
@AfterClass, @Ignore
JUnit provides static methods to test for certain
conditions.
fail, assertTrue, assertFalse, assertEquals, assertNull,
assertNotNull, assertSame, assertNotSame
9. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Test Automation
Definition
Manual testing usually takes too much time and
resources.
Tests can be automatically generated from BDD style
descriptions.
Tests can be fully automated as a part of build process.
Watchers can be used to detect changes in the code,
recompile if necessary, and run the tests.
10. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Cucumber
Cucumber is a BDD tool that executes plain-text
functional descriptions as automated tests.
Step definitions in Gherkin (Given, When, Then, And,
But) are matched to a piece of code.
If the step is matched and the code does not raise an
Exception, the step is successful (green).
If the step is not matched to any code, the step is
pending (yellow).
If the step is matched and the code raises an
Exception, the step is failed (red).
11. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Test Doubles
A Vocabulary
Dummy No implementation, just to fill the parameter list
Stub Minimal implementation, returns hard-coded values
Spy Similar to stub, also records invoked members
Fake Have a simple implementation with some
shortcuts, e.g. in-memory DB instead of real one
Mock Dynamically created by a mock library, can be
configured to return values, expect particular
members to be invoked, etc.
12. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Test Doubles
Stub vs. Mock
public void t e s tOr d e rSe n d sMa i l I fUn f i l l e d ( ) { / / Stub
Order order = new Order (TALISKER, 51) ;
Mai lServiceStub mai ler = new Mai lServiceStub ( ) ;
order . setMai ler ( mai ler ) ;
order . f i l l ( warehouse ) ;
asser tEquals (1 , mai ler . numberSent ( ) ) ;
}
public void t e s tOr d e rSe n d sMa i l I fUn f i l l e d ( ) { / / Mock
Order order = new Order (TALISKER, 51) ;
Mock warehouse = mock (Warehouse . class ) ;
Mock mai ler = mock ( Mai lService . class ) ;
order . setMai ler ( ( Mai lService ) mai ler . proxy ( ) ) ;
mai ler . expects ( once ( ) ) . method ( " send " ) ;
warehouse . expects ( once ( ) ) . method ( " hasInventory " )
. withAnyArguments ( )
. w i l l ( returnValue ( false ) ) ;
order . f i l l ( (Warehouse ) warehouse . proxy ( ) ) ;
}
13. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
Mockito
Behavior can be defined using when, thenReturn,
thenThrow, thenAnswer methods.
myClass myMock = mock(myClass.class);
when(myMock.myMethod(5))
.thenReturn("five");
Behavior can be verified using verify method.
verify(myMock).myMethod(12);
verify(myMock).myMethod(anyInt());
verify(myMock)
.myMethod(argThat(isValid()));
verify(myMock, times(2)).myMethod(any());
verify(myMock, atMost(3)).myMethod(5);
verifyNoMoreInteractions(myMock);
inOrder.verify(myMock, atLeast(2))
.myMethod(5);
inOrder.verify(myMock, times(1))
.myMethod(4);
14. BLG 411E
Software
Engineering
Recitation 3
Introduction
Unit Tests
JUnit
Auto Tests
Cucumber
Mock Objects
Mockito
References
References and Further Reading
http://technologyconversations.com/2014/09/30/
test-driven-development-tdd/
http://technologyconversations.com/2013/12/20/
test-driven-development-tdd-example-walkthrough/
http://dannorth.net/introducing-bdd/
Meszaros, Gerard (2007). xUnit Test Patterns:
Refactoring Test Code. Addison-Wesley. ISBN
978-0-13-149505-0.
http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
http://junit.org/
https://github.com/cucumber/cucumber/wiki
https://code.google.com/p/mockito/