Top Banner
@AylTesters Aylesbury Testers Meetup May 2016 © S COOL JVM TOOLS TO HELP YOU TEST Schalk W. Cronjé
55

Cool Jvm Tools to Help you Test - Aylesbury Testers Version

Jan 08, 2017

Download

Software

Schalk Cronjé
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

@AylTesters Aylesbury Testers Meetup May 2016 © Schalk W. Cronjé

COOL JVM TOOLS TO HELPYOU TEST

Schalk W. Cronjé

Page 2: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

1

ABOUT ME

Email:

Twitter / Ello : @ysb33r

[email protected]

Page 3: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

2

CLARITY OF INTENT

 

Readability

 

Expressiveness

Page 4: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

3

LET’S REVIEW OUR TESTS

Page 5: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 1

LET’S GET UNIT TESTINGclass CalculatorSpec extends Specification { def "A calculator must be able to add numbers"() {

given: 'That I have a calculator' def calc = new Calculator()

when: "I add 2, 3 and -20" def answer = calc.plus 2,3,-20

then: "The answer should be -15" answer == -15 } }

Page 6: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 2

WHEN THIS TEST FAILS

Page 7: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 3

WHEN THIS TEST FAILS

Page 8: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 4

SPOCK FRAMEWORK

Built on top of JUnit 4.x.

Use it with all your JUnit tools!

Use for more than just unit tests.

Mocks & stubs included

Easily test Java, Groovy, Kotlin, Scala etc.

Page 9: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 5

DATA-DRIVEN TESTS @Unroll def "A calculator must be able to multiply"() {

given: 'That I have a multiplying calculator' def calc = new Calculator()

expect: "The answer to be #answer" answer == calc.multiply (a,b,c)

where: "The operation is #a * #b * #c" a | b | c || answer 3 | 7 | 5 || 105 1 | 3 | 0 || 0 2 | -1| 1 || -2 }

Page 10: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 6

ANOTHER COOL FAILURE REPORT

Page 11: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 7

HANDLING EXCEPTIONSdef "Dividing by zero should throw an exception"() { given: 'That I have a dividing calculator' def calc = new Calculator()

when: "I divide by zero" calc.divide 1,0

then: "I expect an error" thrown(ArithmeticException) }

Page 12: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 8

MOCK OUT INTERFACESpublic interface RemoteCalculator { public Number plus(Number... args); }

def "Remote calculator"() { given: "A remote calculator is available" def calc = Mock(RemoteCalculator)

when: "Multiple items are sent for addition" calc.plus 1,2,3,4,5

then: "The calculator is only called once" 1 * calc.plus(_) }

Page 13: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 9

SPOCK FRAMEWORK

Spock Framework:

Spock Reports

http://spockframework.github.io/spock/docs/1.0/index.html

https://github.com/renatoathaydes/spock-reports

Page 14: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

4 . 10

WHAT IS THIS EVIL?

Underlying language is (Apache) Groovy

You don’t need to be a Groovy expert to be a power user ofmany of these tools.

Groovy doesn’t need ; in most cases

Groovy does more with less punctuation, making it an idealchoice for a DSL

In most cases lines that do not end on an operator isconsidered a completed statement.

Page 15: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 1

GROOVY VS JAVA

In Groovy:

All class members are public by default

No need to create getters/setters for public �elds

Both static & dynamic typing supported

def means Object

Page 16: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 2

CALLING METHODSclass Foo { void bar( def a,def b ) {} }

def foo = new Foo()

foo.bar( '123',456 ) foo.bar '123', 456

foo.with { bar '123', 456}

Page 17: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 3

CALLING METHODS WITH CLOSURESclass Foo { void bar( def a,Closure b ) {}}

def foo = new Foo()

foo.bar( '123',{ println it } )

foo.bar ('123') { println it }

foo.bar '123', { println it }

Page 18: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 4

MAPS IN GROOVY

Hashmaps in Groovy are simple to use

def myMap = [ plugin : 'java' ]

Maps are easy to pass inline to functions

project.apply( plugin : 'java' )

Which can also be written as

project.with { apply plugin : 'java' }

Page 19: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 5

LISTS IN GROOVY

Lists in Groovy are simple too

def myList = [ 'clone', 'http://github.com/ysb33r/GradleLectures' ]

This makes it possible write a method call as

args 'clone', 'http://github.com/ysb33r/GradleLectures'

Page 20: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 6

CLOSURE DELEGATION IN GROOVY

When a symbol cannot be resolved within a closure,Groovy will look elsewhere

In Groovy speak this is called a Delegate.

This can be programmatically controlled via theClosure.delegate property.

Page 21: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 7

CLOSURE DELEGATION IN GROOVYclass Foo { def target }

class Bar { Foo foo = new Foo() void doSomething( Closure c ) { c.delegate = foo c() } }

Bar bar = new Bar() bar.doSomething { target = 10 }

Page 22: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 8

MORE CLOSURE MAGIC

If a Groovy class has a method call(Closure), the objectcan be passed a closure directly.

class Foo { def call( Closure c) { /* ... */ } }

Foo foo = new Foo() foo {

println 'Hello, world'

}

// This avoids ugly syntax foo.call({ println 'Hello, world' })

Page 23: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

5 . 9

6 . 1

QUICK HTTP TESTING

Page 24: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

QUICK HTTP TESTING

Quickly point to a remote or in-process server

Build payload in a simplistic manner

Execute the verb

Check the response in a readable manner

Page 25: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

6 . 2

QUICK HTTP TESTINGdef "The echo path should return what is send to it"() { given: "A simple text request" requestSpec { pay -> pay.body.type(MediaType.PLAIN_TEXT_UTF8).text(' ') }

when: "The data is posted" post '/'

then: "It should be echoed back" response.statusCode == 200 response.body.text == 'You said: ' }

Page 26: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

6 . 3

7 . 1

 

Page 27: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

7 . 2

RATPACK FRAMEWORK

Set of Java libraries for building modern HTTP applications.

Built on Java 8, Netty and reactive principles.

Groovy syntax for those who prefer it.

Page 28: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

7 . 3

RATPACK’S TESTHTTPCLIENT

Can be used standalone

Use maven coordinates:

io.ratpack:ratpack-test:1.2.0

Page 29: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

RATPACK’S TESTHTTPCLIENTApplicationUnderTest app = new ApplicationUnderTest() { @Override URI getAddress() { "http://127.0.0.1:${PORT}".toURI() } }

@Delegate TestHttpClient client = TestHttpClient.testHttpClient(app)

def "The echo path should return what is send to it"() { given: "A simple text request" requestSpec { pay -> pay.body.type(MediaType.PLAIN_TEXT_UTF8).text(' ') }

when: "The data is posted" post '/'

then: "It should be echoed back" response.statusCode == 200 response.body.text == 'You said: ' }

Page 30: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

7 . 4

OFFLINE API TESTING

Sometimes you simply have to use a live site for testing

Mocking & stubbing simply not good enough

Need live data

Do not want to overload service with unnecessary testdata

Want to test of�ine

Page 31: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

8 . 1

OFFLINE API TESTING@Betamax(tape='files')

def "Source jar must be posted"() {

given: 'The list of files is requested from the repository'

def list = get path : "${REPO_ROOT}/bintray-gradle-plugin/files"

expect: 'The bintray source jar should be included'

list?.find {

it?.path ==

'org.ysb33r.gradle/bintray/0.0.5/bintray-0.0.5-sources.jar'

}

}

Page 32: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

8 . 2

9 . 1

 

Page 33: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

BETAMAXAllows record & playpack

Mocks external HTTP resources

Web services

REST APIs

Stores recordings in YAML

Easy to edit

Commit to source control

Remember to sanitize auth credentials!

Page 34: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

9 . 2

DEMO

Page 35: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

9 . 3

9 . 4

CREATE RECORDER@Shared ProxyConfiguration configuration = ProxyConfiguration.builder(). tapeRoot(new File('/path/to/tapes')). ignoreLocalhost(false). sslEnabled(true). build()

@Rule RecorderRule recorder = new RecorderRule(configuration)

Page 36: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

 

http://betamax.software

https://github.com/betamaxteam/betamax

Page 37: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

9 . 5

WEBSITE TESTINGOne of the most predominant area of testing of this decade

Test-driven webpage development is less effort in long run

Selenium is the leading tool

Selenium tests can be overly verbose

Page 38: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

10 . 1

TRIVIAL TEST EXAMPLE

def "Learn about testing checkboxes"() { when: "I go to that the-internet site" go "${webroot}/checkboxes"

then: 'I am expecting Checkbox page' $('h3').text() == 'Checkboxes'

and: 'The checkbox states are no & yes' $(By.id('checkboxes')).$('input')*.@checked == ['','true'] }

Page 39: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

10 . 2

11 . 1

 

Page 40: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

GEBIntegrates with

Spock Framework

JUnit

TestNG

Cucumber-JVM

Makes Selenium readable

Anything you can do in Selenium you can do in Geb

Page 41: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

11 . 2

TRIVIAL TEST EXAMPLE COMPLETEclass CheckboxExampleSpec extends GebSpec { def "Learn about testing checkboxes"() { when: "I go to that the-internet site" go "${webroot}/checkboxes"

then: 'I am expecting Checkbox page' $('h3').text() == 'Checkboxes'

and: 'The checkbox states are no & yes' $(By.id('checkboxes')).$('input')*.@checked == ['','true'] } }

Page 42: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

11 . 3

GRABBING SCREENSHOTSclass CheckboxReportingSpec extends GebReportingSpec { def 'Learn about testing checkboxes'() { when: 'I go to that the-internet site' go "${webroot}/checkboxes" report 'checkbox-screen'

then: 'I am expecting Checkbox page' $('h3').text() == 'Checkboxes'

and: 'The checkbox states are no & yes' $(By.id('checkboxes')).$('input')*.@checked == ['','true'] } }

Keep record during test

Stores HTML & PNG.

Page 43: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

11 . 4

GRABBING SCREENSHOTS

Page 44: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

11 . 5

 

@GemFramework

http://gebish.org

Page 45: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

11 . 6

TIE IT TOGETHERAutomation is very important for effectiveness and time-to-market

Ability to run all tests locally, under CI and under specialenvironments

Modern testers needs to know about deployment

Page 46: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

12

13 . 1

 

Page 47: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

13 . 2

GRADLEVery modern, next-generation build & deployment pipelinetool

DSL is based on Groovy

Vast collection of plugins

Page 48: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

GRADLEapply plugin : 'groovy'

repositories { jcenter() }

dependencies { testCompile 'org.spockframework:spock-core:1.0-groovy-2.4' testCompile "org.gebish:geb-spock:0.13.0" testCompile "org.seleniumhq.selenium:selenium-api:2.53.0" testRuntime "org.seleniumhq.selenium:selenium-support:2.53.0" testRuntime "org.seleniumhq.selenium:selenium-firefox-driver:2.53.0" }

Page 49: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

13 . 3

GRADLEMake it easy to integrate development and complex testing

Deploy local & to remote sites via plugins

Use Docker local & remote

"I do not have means to test on my machine" becomes lessof an argument.

Page 50: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

13 . 4

 

Website:

Plugins Portal:

User Forum:

@gradle

@DailyGradle

http://gradle.org

http://plugins.gradle.org

http://discuss.gradle.org

Page 51: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

13 . 5

14

DEMO

Page 52: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

 

http://leanpub.com/idiomaticgradle

Page 53: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

15

OTHER NOTEWORTHY TOOLSAccuREST ( )

REST APIs with customer-driven contracts

UniTEE ( )

Multi-component test framework

Arquillian ( )

J2EE testing within app servers

JSelenide ( )

Alternative to Geb

https://github.com/Codearte/accurest

http://www.autocognite.com/

http://arquillian.org/

http://selenide.org/

Page 54: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

16

ABOUT THIS PRESENTATIONWritten in Asciidoctor (1.5.3.2)

Styled by asciidoctor-revealjs extension

Built using:

Gradle

gradle-asciidoctor-plugin

gradle-vfs-plugin

All code snippets tested as part of build

https://github.com/ysb33r/FastTrackJvm/tree/AylTestersMay2016/prezzo

Page 55: Cool Jvm Tools to Help you Test - Aylesbury Testers Version

17

THANK YOUSchalk Cronjé

[email protected]

@ysb33r