aotb2014: agile testing on the java platform
DESCRIPTION
Creative Commons 2.0 License Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales (CC BY-NC-SA 2.0 UK) https://creativecommons.org/licenses/by-nc-sa/2.0/uk/ Share — copy and redistribute the material in any medium or format Adapt — remix, transform, and build upon the material The licensor cannot revoke these freedoms as long as you follow the license terms. This talk about the following: * TDD ** Is TDD Dead? ** David Heinemeier-Hanson and the controversy * Java Technology ** If only JUnit tests were this simple ** Java has a some great static analysis tools ** Unfortunatley, these do not work too well in Scala platform ** Guidelines to write tests * Creative Development in Principle ** Design is a balance ** Inventing your own style ** Avoid lock-in with TDD, use it instead as a design tool * Scala Technology ** Scala Option ** Function objects ** Pattern matching ** Avoid if and then else and null pointers * Legacy ** Final advice This talk was given by Peter Pilgrim, invited speaker to the Agile On The Beach conference on the 5th September, 2014 at Penryn Campus, University of Exeter, CornwallTRANSCRIPT
Agile TESTING Practices ON THE Java
Platform Creative development in principle
Peter Pilgrim Scala and Java EE
Independent contractor
@peter_pilgrim
uk.linkedin.com/in/peterpilgrim
2000/
Xenonique
Creative Commons 2.0 Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales (CC BY-NC-SA 2.0 UK) https://creativecommons.org/licenses/by-nc-sa/2.0/uk/
• Share — copy and redistribute the material in
any medium or format • Adapt — remix, transform, and build upon the
material • The licensor cannot revoke these freedoms as
long as you follow the license terms.
2
Xenonique
About Me • Scala and Java EE developer independent
contractor • Digital transformation GOV.UK • Java Champion • Working on the JVM since 1998
3
Xenonique 4
Java EE 7 Developer Handbook September 2013 Packt Publishing Digital Java EE 7 Web APPS December 2014
Xenonique 5
• TDD • Java Technology • Creative
Development in Principle
• Scala Technology
Xenonique 6
Xenonique 7
Xenonique
Is TDD Dead? • TDD is Dead, Long Live Testing Blog article by David Heinemeier Hanson http://david.heinemeierhansson.com/2014/tdd-is-dead-long-live-testing.html
• Series of 5 Google Hangout Videos @dhh, @kentbeck and @martinfowler http://martinfowler.com/articles/is-tdd-dead/
8
Xenonique 9
“I rarely unit test in the traditional sense of the word, where all dependencies are mocked out, and thousands of tests can close in seconds. It just hasn't been a useful way of dealing with the testing of Rails applications.” D.H.H
Xenonique 10
Xenonique 11
JUnit
the mother of all unit tesFng frameworks
Xenonique 12
#1 Java unit testing framework on the JVM. Other alternate languages have variants or interoperability.
Xenonique 13
2007 JUnit 4.4
XMLUnit, DBTest, HTMLUnit, PerfTest, EasyMock, Test NG, Cactus, SoapUI
Xenonique 14
2014 - JUnit 4.12?
Mockito, Concordion, Spring, JMock, Arquillian, Fit/Fitnesse, JBehave, ScalaTest, Spock
Xenonique
JUnit Test Example #1
import org.junit.*; import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.*; public class ForexTradingDeskTest { TradingDesk desk = new ForexTradingDesk(); /* ... */ }
15
Xenonique
JUnit Test Example #2
public class ForexTradingDeskTest { @Before public void setUp() { desk.acquire(); } @After public void tearDown() {
desk.release(); } /* ... */ }
16
Xenonique
Java Test Example #3
@Test public void shouldExecuteTrade() { Trade trade = ForexTrade("GBP","EUR", 10000); desk.execute(trade); assertThat( trade.getStatus(), is(Executed)); }
17
Xenonique 18
Xenonique 19
Xenonique 20
These tools were built for the mother language of the Java Virtual Machine.
Xenonique 21
Sadly, these tools do not work too with:
Xenonique 22
Xenonique 23
Three development concepts for agile digital applications
Efficiency Design Human Nature
Xenonique 24
#1 Understanding the Efficiency of our Java application development
Xenonique 25
30.7% Github projects* have a dependency on JUnit. Yet we, Java developers, still write too much boilerplate, over-engineer design patterns.
We forget about directing the future. *http://www.takipiblog.com/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/
Xenonique 26
Hard-‐coded dependencies and unassailable fixtures are the issues. It does not maNer which technology.
Xenonique 27
Technology is not the problem. How much do we understand design and architecture?
Java is a programming language. Java is a virtual machine. Java is a platform.
Xenonique 28
#1 Completeness: As digital transformation engineers, what would happen to admitted: we can never have 100% coverage complete with just unit-tests?
What are we measuring? How are we measuring? Impossible to prove all branches in a program
Xenonique 29
#1 Efficiency: Prefer to write tests than none at all Prefer to mock out dependencies Prefer to test the simplest thing possible
Avoid duplication (cut and paste tests) Reduce fixtures and preconditions
Xenonique 30
#1 Efficiency: Prefer integration tests over unit tests. Remove unit tests that over time are always ”green”. Rely on functional and acceptance tests as well.
Hire a developer-in-test into your team Avoid being a unit-test automaton.
Xenonique 31
#2 The age-old issue of how do we Design? Time, space and cost are constants
Xenonique 32
“TEX would have been a complete failure if I had merely specified it and not participated fully in its initial implementation. The process of implementation constantly led me to unanticipated questions and to new insights about how the original specifications could be improved.” Donald Knuth
Xenonique 33
#2 Design Test-‐First may be harmful True design presupposes we have ability to escape restric@ons. It requires to innovate around the architectural boundaries.
Edit -‐> Compile -‐> Test -‐> Refactor
Xenonique 34
#2 Design Our problem is abstrac@on “Debugging is not what you do when you fire up the debugger. It is when you lie back in the chair and look at the ceiling.”
Crea@vity is a thought process
Xenonique 35
#2 Design – Problem solving versus art Balance crea@vity whilst respecFng produc@vity and testability
Xenonique 36
#2 Design – search the Internet for a video
Bret Victor, Inven@ng on Principle
Xenonique 37
#3 Human Nature It’s always about the folk. How long did it take to learn this simple truth in computer science?
Xenonique 38
#3 Human Nature Now it is about everyone on the planet We are serving the digital customers for all the screens of our lives
Xenonique 39
#3 Human Nature Developers require UX education. We cannot afford to not invest in learning. Digital transformation is open for all people.
Xenonique 40
#3 Human Nature
“He said it was more important that the company avoid the appearance of having done something wrong than that we stop before producing an incorrect result. I left the company.“
Jim Coplien, Why Unit Testing is a Waste hNp://www.rbcs-‐us.com/documents/Why-‐Most-‐Unit-‐TesFng-‐is-‐Waste.pdf
Xenonique 41
#3 Human Nature Good developers are generally good writers. Great developers can create AM #1: “Interactions and individuals over processes and tools”
Xenonique 42
Xenonique
Scala Test #1
import collection.mutable.Stack import org.scalatest._ class StackSpec extends FlatSpec with Matchers { /* ... */
}
43
Xenonique
Scala Test #2
class StackSpec extends FlatSpec with Matchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should be (2) stack.pop() should be (1) } /* ... */ }
44
Xenonique
Scala Test #3
class StackSpec extends FlatSpec with Matchers { /* ... */ it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[Int] a [NoSuchElementException] should be thrownBy {
emptyStack.pop() } } }
45
Xenonique
How to avoid null pointers in Scala Programming?
val greeting1: Option[String] = Some("AOTB14") val greeting2: Option[String] = None
46
Xenonique
Pattern Matching
def checker(p : Option[String]): String = { p match { case Some(v) => v case _ => "Unexpected" } } checker(greeting1) // "Hello AOTB14" checker(greeting2) // "Unspecified"
47
Xenonique
Scala’s generic immutable list collection type
val list: List[Int] = List(1,2,3,4) list.foreach { x => println(x) } // 1,2,3,4
48
Xenonique
Scala’s option behaves like collection type #2
greeting1.foreach { x => println(s"Work: ${x}" ) } // Work: Hello AOTB14
greeting2.foreach { x => println(s"Work: ${x}" ) } // *** nothing happens ***
49
Xenonique
Read, Evaluate, Print, Loop • Scala, Clojure, Groovy, Kotlin, Ceylon, Fantom All have a interpretative mode • Java JDK does not (yet) have an official REPL (However try out javarepl.com online) • Fingers crossed Project Kulla in JDK 9 http://blog.gmane.org/gmane.comp.java.openjdk.general
50
Xenonique 51
#DailyWork2014 GOV.UK • Develop Scala applications • Play Framework, RESTful Services, MongoDB • Cucumber testing: acceptance & functional • Collaborative design, pairing and reviews • SCRUM, Sometimes Kanban • Physical storyboards, retros, planning • Heavy dose of UX / User Centric • Government Digital Service
Xenonique 52
• TDD • Java Technology • Creative
Development in Principle
• Scala Technology
Xenonique 53
Xenonique
Recipe for Success
• Be creative and artistic again • Leave a digital legacy of achievement • Be efficient, evolve design and aware of
human nature
• Invent your own principles
54
Xenonique 55
@peter_pilgrim
uk.linkedin.com/in/peterpilgrim
2000/
Xenonique
List of Creative Commons (2.0) photography attributions
https://flic.kr/p/ayqvZp https://www.flickr.com/photos/throughpaintedeyes/ Ryan Seyeau Follow 1000 Faces of Canada # 0084 - Zombie Walk - Explore! Shot during the annual Zombie Walk in Ottawa. https://flic.kr/p/Pp93n https://www.flickr.com/photos/spacesuitcatalyst/ William A. Clark Follow Measurement Measure twice, cut once. https://flic.kr/p/81dXuT https://www.flickr.com/photos/froboy/ Avi Schwab Follow Equipment used for measuring planes of crystals https://flic.kr/p/2QHt7Q https://www.flickr.com/photos/rosefirerising/ rosefirerising Follow Pierre Curie, Piezo-Electroscope
56
Xenonique
List of Creative Commons (2.0) photography attributions
https://www.flickr.com/photos/code_martial/ https://flic.kr/p/7jmU5n Tahir Hashmi Follow Developer Death 10 Programmers and a UX Designer died all of a sudden in a war room situation. They were apparently working too hard. https://www.flickr.com/photos/8268882@N06/ https://flic.kr/p/duwd1M Peter Pilgrim IMG_1481 European JUG leaders meeting in Devoxx 2013 https://www.flickr.com/photos/unicefethiopia/ https://flic.kr/p/dv4ooi UNICEF Ethiopia Follow Hamer mother and child South Omo Zone, SNNPR Hamer mother and child South Omo Zone, SNNPR. ©UNICEF Ethiopia/2005/Getachew
57
Xenonique
List of Creative Commons (2.0) photography attributions
https://www.flickr.com/photos/15216811@N06/ https://flic.kr/p/baULpM N i c o l a Follow Tree - IMG_1242 https://flic.kr/p/dwCQ7t https://www.flickr.com/photos/90585146@N08/ marsmetn tallahassee Follow IDA .. Integro-Differential Analyzer (Sept., 1952) ...item 2.. Richard Dreyfuss: Technology Has 'Killed Time' -- "In geopolitics, the removal of time is fatal." -- "And you will give up, the protection of Republican Democracy." (July 19 2010) ... https://flic.kr/p/6ZDbiZ https://www.flickr.com/photos/ingmar/ Ingmar Zahorsky Follow Traffic Jam Accidents are common on the narrow streets going through the mountains of Nepal. When such an accident occurs, traffic is often halted for up to 3-4 hours. https://flic.kr/p/opvVsg https://www.flickr.com/photos/gregbeatty/ Greg Beatty Follow Three Pillars Nikon D800 16-35MM F4.0 https://flic.kr/p/FAskC https://www.flickr.com/photos/mari1008/ mari_1008 Follow traffic jam -B Date: April,2th Address: Jiangshu Rd,Shanghai,China. Around 8:30 AM today, A little accident on YuYuan Rd and JiangSu Rd caused traffic jam.
58
Xenonique
List of Creative Commons (2.0) photography attributions
https://flic.kr/p/bpss6D https://www.flickr.com/photos/76029035@N02/ Victor1558 Follow Creative_Photoshop_HD_Wallpapers_laba.ws https://flic.kr/p/4SHJzX https://www.flickr.com/photos/20745656@N00/ Maryellen McFadden Follow Japanese Graphic Design Poster designed for a Charles Renee Macintosh exhibit by Shinnoske, Inc. and I don't have the date. https://flic.kr/p/gLPhEk https://www.flickr.com/photos/stevecorey/ Steve Corey Follow Treasure Hunt, a short story (5 images)
59
Xenonique
List of Creative Commons (2.0) photography attributions
https://flic.kr/p/4JcerK https://www.flickr.com/photos/16949884@N00/ Bömmel Follow Ice Crystal Ice is nice https://flic.kr/p/dSsXiZ https://www.flickr.com/photos/jeremyhall/ Jeremy Hall Follow Called to Serve (suit and tie) https://flic.kr/p/3enERy https://www.flickr.com/photos/paolocampioni/ Paolo Campioni Follow scala Scendo (stair case spiral)
60
Xenonique
List of Creative Commons (2.0) photography attributions
https://flic.kr/p/m62gmK https://www.flickr.com/photos/lata/ -p Follow Abstraction I. From my platycerium. (Guide: Voronoi diagram) https://flic.kr/p/6WSFR4 https://www.flickr.com/photos/62337512@N00/ anthony kelly Follow big ben big ben and underground sign https://flic.kr/p/4riATM https://www.flickr.com/photos/stevecadman/ Steve Cadman Follow Marsham Street Marsham Street (The Home Office), Westminster, London (2000-5) by Terry Farrell and Partners
61