Upload
atakan-aral
View
87
Download
1
Embed Size (px)
DESCRIPTION
BLG 411E – Software Engineering Recitation Session 3 Test-Driven Development
Citation preview
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
BLG 411E – Software EngineeringRecitation Session 3
Test-Driven Development
Atakan Aral, Bilge Süheyla Akkoca
11.11.2014
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Outline
1 Introduction
2 Unit TestsJUnit
3 Auto TestsCucumber
4 Mock ObjectsMockito
5 References
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
TDDDefinition
A software development process.Based on text-first concept in Extreme Programming.Relies on the repetition of a very short developmentcycle.Also called Red-Greed-Refactor
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
TDDLife-cycle
1 Write a testBefore implementation
2 Run the testShould fail
3 Write the implementation codeJust enough to pass the testNot perfect
4 Run all testsAll should pass
5 RefactorOptimize code without introducing new featuresShould not make any tests failShould not include new tests
6 Repeat
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
TDDFeatures
All about speed: short unit tests (JUnit), imperfect butfast code.Forces us to think before coding.Allows us to develop fast without being afraid to breaksomething.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 canbe replaced easily).
A note
Tools that we will introduce today do not belong to TDD. Youcan use them with any process.
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
TDDBehavior-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 thebusiness outcomes.Single notation and ubiquitous language to improvecommunication (Gherkin language).Scenario’s are used instead of tests to avoidtechnicalities.Test can be generated automatically (cucumber).
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Unit TestingDefinition
A white-box testing method for individual units of code.A unit is the smallest testable part of an application.
Function in procedural programmingInterface, class or method in object-orientedprogramming.
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.
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
JUnit
JUnit is a test framework which uses annotations toidentify 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 certainconditions.
fail, assertTrue, assertFalse, assertEquals, assertNull,assertNotNull, assertSame, assertNotSame
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Test AutomationDefinition
Manual testing usually takes too much time andresources.Tests can be automatically generated from BDD styledescriptions.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.
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Cucumber
Cucumber is a BDD tool that executes plain-textfunctional 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 anException, the step is successful (green).If the step is not matched to any code, the step ispending (yellow).If the step is matched and the code raises anException, the step is failed (red).
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Test DoublesA Vocabulary
Dummy No implementation, just to fill the parameter listStub Minimal implementation, returns hard-coded valuesSpy Similar to stub, also records invoked members
Fake Have a simple implementation with someshortcuts, e.g. in-memory DB instead of real one
Mock Dynamically created by a mock library, can beconfigured to return values, expect particularmembers to be invoked, etc.
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
References
Test DoublesStub vs. Mock
public void t e s t O r d e r S e n d s M a i l I f U n f i l l e d ( ) { / / StubOrder order = new Order (TALISKER , 51 ) ;Mai lServ iceStub mai le r = new Mai lServ iceStub ( ) ;order . se tMa i l e r ( ma i le r ) ;order . f i l l ( warehouse ) ;asser tEquals (1 , ma i le r . numberSent ( ) ) ;
}
public void t e s t O r d e r S e n d s M a i l I f U n f i l l e d ( ) { / / MockOrder order = new Order (TALISKER , 51 ) ;Mock warehouse = mock ( Warehouse . class ) ;Mock mai le r = mock ( Mai lServ ice . class ) ;order . se tMa i l e r ( ( Mai lServ ice ) ma i le r . proxy ( ) ) ;
ma i le r . expects ( once ( ) ) . method ( " send " ) ;warehouse . expects ( once ( ) ) . method ( " hasInventory " )
. withAnyArguments ( )
. w i l l ( re tu rnVa lue ( fa lse ) ) ;
order . f i l l ( ( Warehouse ) warehouse . proxy ( ) ) ;}
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
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);
BLG 411ESoftware
Engineering
Recitation 3
Introduction
Unit TestsJUnit
Auto TestsCucumber
Mock ObjectsMockito
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. ISBN978-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/