aotb2014: agile testing on the java platform

61
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/peterpilgrim2000/

Upload: peter-pilgrim

Post on 11-Nov-2014

589 views

Category:

Technology


2 download

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, Cornwall

TRANSCRIPT

Page 1: AOTB2014: Agile Testing on the Java Platform

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/  

Page 2: AOTB2014: Agile Testing on the Java Platform

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  

Page 3: AOTB2014: Agile Testing on the Java Platform

Xenonique

About Me •  Scala and Java EE developer independent

contractor •  Digital transformation GOV.UK •  Java Champion •  Working on the JVM since 1998

3  

Page 4: AOTB2014: Agile Testing on the Java Platform

Xenonique 4  

Java EE 7 Developer Handbook September  2013  Packt  Publishing    Digital Java EE 7 Web APPS December  2014  

Page 5: AOTB2014: Agile Testing on the Java Platform

Xenonique 5  

•  TDD •  Java Technology •  Creative

Development in Principle

•  Scala Technology

Page 6: AOTB2014: Agile Testing on the Java Platform

Xenonique 6  

Page 7: AOTB2014: Agile Testing on the Java Platform

Xenonique 7  

Page 8: AOTB2014: Agile Testing on the Java Platform

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  

Page 9: AOTB2014: Agile Testing on the Java Platform

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

Page 10: AOTB2014: Agile Testing on the Java Platform

Xenonique 10  

Page 11: AOTB2014: Agile Testing on the Java Platform

Xenonique 11  

JUnit  

the  mother    of  all  unit  tesFng  frameworks  

Page 12: AOTB2014: Agile Testing on the Java Platform

Xenonique 12  

#1 Java unit testing framework on the JVM. Other alternate languages have variants or interoperability.

Page 13: AOTB2014: Agile Testing on the Java Platform

Xenonique 13  

2007 JUnit 4.4

XMLUnit, DBTest, HTMLUnit, PerfTest, EasyMock, Test NG, Cactus, SoapUI

Page 14: AOTB2014: Agile Testing on the Java Platform

Xenonique 14  

2014 - JUnit 4.12?

Mockito, Concordion, Spring, JMock, Arquillian, Fit/Fitnesse, JBehave, ScalaTest, Spock

Page 15: AOTB2014: Agile Testing on the Java Platform

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  

Page 16: AOTB2014: Agile Testing on the Java Platform

Xenonique

JUnit Test Example #2

public class ForexTradingDeskTest { @Before public void setUp() { desk.acquire(); } @After public void tearDown() {

desk.release(); } /* ... */ }

16  

Page 17: AOTB2014: Agile Testing on the Java Platform

Xenonique

Java Test Example #3

@Test public void shouldExecuteTrade() { Trade trade = ForexTrade("GBP","EUR", 10000); desk.execute(trade); assertThat( trade.getStatus(), is(Executed)); }

17  

Page 18: AOTB2014: Agile Testing on the Java Platform

Xenonique 18  

Page 19: AOTB2014: Agile Testing on the Java Platform

Xenonique 19  

Page 20: AOTB2014: Agile Testing on the Java Platform

Xenonique 20  

These tools were built for the mother language of the Java Virtual Machine.

Page 21: AOTB2014: Agile Testing on the Java Platform

Xenonique 21  

Sadly, these tools do not work too with:

Page 22: AOTB2014: Agile Testing on the Java Platform

Xenonique 22  

Page 23: AOTB2014: Agile Testing on the Java Platform

Xenonique 23  

Three development concepts for agile digital applications

Efficiency   Design     Human  Nature  

Page 24: AOTB2014: Agile Testing on the Java Platform

Xenonique 24  

#1 Understanding the Efficiency of our Java application development

Page 25: AOTB2014: Agile Testing on the Java Platform

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/

Page 26: AOTB2014: Agile Testing on the Java Platform

Xenonique 26  

Hard-­‐coded  dependencies  and  unassailable  fixtures  are  the  issues.  It  does  not  maNer  which  technology.  

Page 27: AOTB2014: Agile Testing on the Java Platform

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.

Page 28: AOTB2014: Agile Testing on the Java 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

Page 29: AOTB2014: Agile Testing on the Java Platform

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

Page 30: AOTB2014: Agile Testing on the Java Platform

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.

Page 31: AOTB2014: Agile Testing on the Java Platform

Xenonique 31  

#2 The age-old issue of how do we Design? Time, space and cost are constants

Page 32: AOTB2014: Agile Testing on the Java Platform

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

Page 33: AOTB2014: Agile Testing on the Java Platform

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    

Page 34: AOTB2014: Agile Testing on the Java Platform

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  

Page 35: AOTB2014: Agile Testing on the Java Platform

Xenonique 35  

#2  Design  –    Problem  solving  versus  art  Balance  crea@vity  whilst  respecFng  produc@vity  and  testability  

Page 36: AOTB2014: Agile Testing on the Java Platform

Xenonique 36  

#2  Design  –  search  the  Internet  for  a  video  

Bret  Victor,  Inven@ng  on  Principle  

Page 37: AOTB2014: Agile Testing on the Java Platform

Xenonique 37  

#3 Human Nature It’s always about the folk. How long did it take to learn this simple truth in computer science?

Page 38: AOTB2014: Agile Testing on the Java Platform

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

Page 39: AOTB2014: Agile Testing on the Java Platform

Xenonique 39  

#3 Human Nature Developers require UX education. We cannot afford to not invest in learning. Digital transformation is open for all people.

Page 40: AOTB2014: Agile Testing on the Java Platform

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  

Page 41: AOTB2014: Agile Testing on the Java Platform

Xenonique 41  

#3 Human Nature Good developers are generally good writers. Great developers can create AM #1: “Interactions and individuals over processes and tools”

Page 42: AOTB2014: Agile Testing on the Java Platform

Xenonique 42  

Page 43: AOTB2014: Agile Testing on the Java Platform

Xenonique

Scala Test #1

import collection.mutable.Stack import org.scalatest._ class StackSpec extends FlatSpec with Matchers { /* ... */

}

43  

Page 44: AOTB2014: Agile Testing on the Java Platform

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  

Page 45: AOTB2014: Agile Testing on the Java Platform

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  

Page 46: AOTB2014: Agile Testing on the Java Platform

Xenonique

How to avoid null pointers in Scala Programming?

val greeting1: Option[String] = Some("AOTB14") val greeting2: Option[String] = None

46  

Page 47: AOTB2014: Agile Testing on the Java Platform

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  

Page 48: AOTB2014: Agile Testing on the Java Platform

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  

Page 49: AOTB2014: Agile Testing on the Java Platform

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  

Page 50: AOTB2014: Agile Testing on the Java Platform

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  

Page 51: AOTB2014: Agile Testing on the Java Platform

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

Page 52: AOTB2014: Agile Testing on the Java Platform

Xenonique 52  

•  TDD •  Java Technology •  Creative

Development in Principle

•  Scala Technology

Page 53: AOTB2014: Agile Testing on the Java Platform

Xenonique 53  

Page 54: AOTB2014: Agile Testing on the Java Platform

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  

Page 55: AOTB2014: Agile Testing on the Java Platform

Xenonique 55  

@peter_pilgrim  

uk.linkedin.com/in/peterpilgrim

2000/  

Page 56: AOTB2014: Agile Testing on the Java Platform

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  

Page 57: AOTB2014: Agile Testing on the Java Platform

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  

Page 58: AOTB2014: Agile Testing on the Java Platform

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  

Page 59: AOTB2014: Agile Testing on the Java Platform

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  

Page 60: AOTB2014: Agile Testing on the Java Platform

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  

Page 61: AOTB2014: Agile Testing on the Java Platform

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