Test Infected

68
#testinfected Mert ÇALIŞKAN Feb 2013 @ankarajug #testinfected

description

#testinfected, Java test yöntemlerini sizin için inceliyor. Sunumda testin amaçları, JUnit ile test, mock test yöntemleri, arayüz testi, web servis vb. gibi framework testi, sürekli entegrasyon ve denetleme yöntemleri, test kapsama oranının arttırılması ve ürün kalitesi nasıl arttırılır konuları hakkında detaylı bilgiler edinebilirsiniz.

Transcript of Test Infected

Page 1: Test Infected

#testinfectedMert ÇALIŞKAN

Feb 2013

@ankarajug #testinfected

Page 2: Test Infected

#testinfected is:

@ankarajug #testinfected

Programmers that LOVE Writing Tests...so infected with of testing

Page 3: Test Infected

@ankarajug #testinfected

AnkaraJUG

ankarajug.blogspot.com

facebook.com/ankarajug

twitter.com/ankarajug

first event @ November 2012...

Founding Fathers: Barış BAL Çağatay ÇİVİCİ Mert ÇALIŞKAN

http://bit.ly/ankarajugKAYIT

Page 4: Test Infected

@ankarajug #testinfected

Mert ÇALIŞKAN

Open Source Software Advocate, Founder, Implementor

Member of Apache Software Foundation and OpenLogic ExpertCommunity

Author of PrimeFaces Cookbook from PacktPub

10+ years of experience w/ Java

Coder @ T2.com.tr

tr.linkedin.com/in/mertcaliskan

Page 5: Test Infected

@ankarajug #testinfected

We are hiring..!

Send Resumes to [email protected]

Page 6: Test Infected

@ankarajug #testinfected

What will I talk about?Objectives of Testing

What to Test, How to Test with

Java Testing Frameworks

Unit, Integration, Mock and Functional Testing

Intensive Code Samples w/ github project

Test Automation with,

Concluded with Best Practices and Mottos

Continuous{ IntegrationInspectionDelivery

Page 7: Test Infected

@ankarajug #testinfected

Do you really need to listen to me?

Please DO..!

The test coverage for the projects that I involved with was SOOOO damn low.

So,

Let’s increase these numbers...!!

Page 8: Test Infected

@ankarajug #testinfected

DISCLAIMER

I’m a DEVELOPER.. so, I develop code to test my code...So you will see - coding best practices that I gatheredrather than - silver bullets, documentation procedures & etc.

which is cool, I suppose...

Page 9: Test Infected

@ankarajug #testinfected

How many Test Infected Zombies among us?

Raise your hand..!

Page 10: Test Infected

@ankarajug #testinfected

Do we need to TEST?@ankarajug #testinfected

Page 11: Test Infected

@ankarajug #testinfected

Your Software...after some coding...

Page 12: Test Infected

@ankarajug #testinfected

Your Software...after some coding...

Page 13: Test Infected

@ankarajug #testinfected

Your Software...after some coding...

Page 14: Test Infected

@ankarajug #testinfected

So again, Do we need to TEST?

@ankarajug #testinfected

Page 15: Test Infected

@ankarajug #testinfected

test 1 |tɛst|noun

a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use...

webster says that...

Page 16: Test Infected

@ankarajug #testinfected

test 1 |tɛst|noun

a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use...

webster says that...

Page 17: Test Infected

@ankarajug #testinfected

The Wh- Questions

WHAT

WHERE

WHEN

WHY

HOW

WHO

to test?

should we test it?

should we test it?

do we need to test?

should we test it?

tests it?

Page 18: Test Infected

@ankarajug #testinfected

The Wh- Questions

WHAT

WHERE

WHEN

WHY

HOW

WHO

to test?

should we test it?

should we test it?

do we need to test?

should we test it?

tests it?

Page 19: Test Infected

@ankarajug #testinfected

WHAT to Test?Unit Tests

Integration Tests

Mock Tests

Functional Tests

every method...with its every line of code...

DAO tests, framework tests...(web services, hibernate search and etc)

service layer, mocking each other :)

UI testing, WS testing

Page 20: Test Infected

@ankarajug #testinfected

WHERE should we test it?

Go for 3 different locations other than local if it’s applicable.

Try to make Prod and Staging identical...

LocalStagingProd Test

=

BlackBox WhiteBox SmokeTest

Page 21: Test Infected

@ankarajug #testinfected

HOW should we test it?Unit Tests

Integration Tests

Mock Tests

Functional Tests

jUnit

jUnit along with custom implementations

mockito, hamcrest...

selenium, soapUI

Page 22: Test Infected

@ankarajug #testinfected

BlackBox Testing

you don’t know the internals of the systema.k.a. Acceptance Testing

WhiteBox Testing

Tester knows the design/implementation of the systema.k.a. Unit/Integration Testing

GrayBox Testing Black Box mixed with White Box :)

How to testThe Boxing Approach

Page 23: Test Infected

@ankarajug #testinfected

Unit Testing

In unit testing, you're just verifying that a single unit works according to expectations.

Unit tests are not interactive. Once written, they can be run without further input.

Each test should be responsible for its own actions not interfere with each other.

Page 24: Test Infected

@ankarajug #testinfected

Common Rules forTesting Frameworks

Each unit test must run independently of all other unit tests.

Errors must be detected and reported test by test.

It must be easy to define which unit tests will run.

JUnit

Page 25: Test Infected

@ankarajug #testinfected

JUnitFounders, Kent Beck and Erich Gamma, like ~14 years ago.

Kent Beck the creator of sUnit and tossed the coin on the term, XP.

Major differences between 3.x and 4.x new packaging, annotations and etc...

The Latest and Greatest of jUnit is: 4.11 (use it)

Page 26: Test Infected

@ankarajug #testinfected

The Simplest Test

public class Example {

@Test public void method() { org.junit.Assert.assertTrue(new ArrayList().isEmpty()); }}

Test

Fixture

https://gist.github.com/mulderbaba/5013544

Page 27: Test Infected

@ankarajug #testinfected

The Simplest Testpublic class Example { @Test public void method() { org.junit.Assert.assertTrue(new ArrayList().isEmpty()); }}

What happens above?

- jUnit creates a new instance of Example class.- It invokes the method() which is annotated with Test

Page 28: Test Infected

@ankarajug #testinfected

The Simplest Test

@Test Annotation bundles 2 optional parameters.

expected: the exception that the method should throw.timeout: the amount of time in ms for the test to fail.

public class Example { @Test public void method() { org.junit.Assert.assertTrue(new ArrayList().isEmpty()); }}

Page 29: Test Infected

@ankarajug #testinfected

Preparing and Tearing down for tests

@Before

@After@AfterClass

@BeforeClass

Page 30: Test Infected

@ankarajug #testinfected

public class SampleTest {

@Before public void setup() { // prepare some stuff.. }

@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}

@After public void tearDown() { // tear down all... }}

Sample @Before and @After

Page 31: Test Infected

@ankarajug #testinfected

public class SampleTest {

@BeforeClass public void setup() { // prepare some stuff.. }

@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}

@AfterClass public void tearDown() { // tear down all... }}

@BeforeClass and @AfterClass with Question

Question is: Will it compile?YES..!But,

will it work?

Page 32: Test Infected

@ankarajug #testinfected

public class SampleTest {

@BeforeClass public void setup() { // prepare some stuff.. }

@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}

@AfterClass public void tearDown() { // tear down all... }}

@BeforeClass and @AfterClass with Question

Question is: Will it compile?YES..!But,

will it work?

Page 33: Test Infected

@ankarajug #testinfected

public class SampleTest {

@BeforeClass public void setup() { // prepare some stuff.. }

@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}

@AfterClass public void tearDown() { // tear down all... }}

@BeforeClass and @AfterClass with Question

Question is: Will it compile?YES..!But,will it work?

Page 34: Test Infected

@ankarajug #testinfected

public class BeforeOrderSample { @Before public void finalSetup() { System.out.println("final stuff"); }

@Test public void doTheTest() { System.out.println("test test test"); }

@Before public void initialSetup() { System.out.println("initial stuff"); }}

Multiple methods with @Before or @After annotations?

https://gist.github.com/mulderbaba/5013850

Question is: Will it work?

If so, which method works first?

Page 35: Test Infected

@ankarajug #testinfected

Let’s introduce some inheritance :)

Class A

Class B

https://gist.github.com/mulderbaba/5014153

@Before public void classASetup() { System.out.println("class A setup"); }

@After public void classATearDown() { System.out.println("class A tear down"); }

@Before public void classBSetup() { System.out.println("class B setup"); }

@After public void classBTearDown() { System.out.println("class B tear down"); }

@Test public void doTheTest() { System.out.println("test test test"); }

Page 36: Test Infected

@ankarajug #testinfected

Let’s introduce some inheritance :)

Class A

Class B

https://gist.github.com/mulderbaba/5014153

@Before public void classASetup() { System.out.println("class A setup"); }

@After public void classATearDown() { System.out.println("class A tear down"); }

@Before public void classBSetup() { System.out.println("class B setup"); }

@After public void classBTearDown() { System.out.println("class B tear down"); }

@Test public void doTheTest() { System.out.println("test test test"); }

1

23

4

5

Page 37: Test Infected

@ankarajug #testinfected

Test Methods’ Execution Order

public class TestCasesExecutionOrder {

@Test public void secondTest() { System.out.println("Executing second test"); }

@Test public void firstTest() { System.out.println("Executing first test"); }

@Test public void thirdTest() { System.out.println("Executing third test"); }}

Question is:

What’s the execution order?

https://gist.github.com/mulderbaba/5014568

Page 38: Test Infected

@ankarajug #testinfected

Test Methods’ Execution Order

public class TestCasesExecutionOrder {

@Test public void secondTest() { System.out.println("Executing second test"); }

@Test public void firstTest() { System.out.println("Executing first test"); }

@Test public void thirdTest() { System.out.println("Executing third test"); }}

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

Question is:

What’s the execution order?

https://gist.github.com/mulderbaba/5014568

Page 39: Test Infected

@ankarajug #testinfected

My Way or the Way

surefire

failsafe

For Unit TestingAttached to test phase

For Integration TestsAttached to pre-integration-test phase and others...

Page 40: Test Infected

@ankarajug #testinfected

testinfected demo @ github

2 projects (demo + template) available at

fork it and enjoy it..

demo is for demo as it is named... template is a brand new beginning...template for all the frameworks with testing infrastructure...

JPA

PrimeFaces

HibernateSpring

CXF Hamcrest

jUnit

Mockito

http://github.com/mulderbaba/testinfected

Page 41: Test Infected

@ankarajug #testinfected

Code Walkthrough

Unit Testing

Page 42: Test Infected

@ankarajug #testinfected

Integration Tests

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/applicationContext-test.xml"})@TransactionConfiguration(defaultRollback = false, transactionManager = "transactionManager")

public abstract class BaseIntegrationTestCase {}

What’s with this class?

Page 43: Test Infected

@ankarajug #testinfected

Integration Tests

Framework testing is one of the coolest things...

You can even test Web Services like,

Get Jetty UP

Create a client

Did a WS call

Assert the result

Page 44: Test Infected

@ankarajug #testinfected

Differentiate your DB’s...

PROD DEV TEST

First Rules First...

Use an ORM (JPA, Hibernate, bla bla)

But WHY o’WHY...?

Page 45: Test Infected

@ankarajug #testinfected

First Rules First...

PROD DEV TEST

ORACLE

MYSQL HSQLDB

Always go for in memory DB for Testing environment.

Everything will vanish so you can do anything you want.

And you can easily tests frameworks like Hibernate Search

Differentiate your DB’s...

Page 46: Test Infected

@ankarajug #testinfected

Case Study: Quartz Scheduling Testing

Question is: How would you test it?

@Servicepublic class SomePrettySophisticatedJob { @Autowired private SomePrettySophisticatedService1 service; private SomePrettySophisticatedService2 service; private SomePrettySophisticatedService3 service; @Scheduled(fixedDelay=500) public void execute() { //Some heavy duty coding... service calls, calls, calls, computations and bla bla.. }}

Page 47: Test Infected

@ankarajug #testinfected

Quartz Scheduling Testing

Well you don’t need to test it, it’s a framework remember?and since it’s a well-known one, just use it for scheduling.It will work..trust me..

@Servicepublic class SomePrettySophisticatedJob { @Autowired private GatheredPrettySophisticatedService service; @Scheduled(fixedDelay=500) public void execute() { service.execute(); }}

Page 48: Test Infected

@ankarajug #testinfected

Code Walkthrough

Integration Testing

Page 49: Test Infected

@ankarajug #testinfected

Anatomy of a Mock Test

It’s good for service layer testing.

Set your expectations, do the service layer call, verify the calls and assert the results.

@RunWith(MockitoJUnitRunner.class)public class CalculatorServiceImplTest { @Mock private CalculatorDao dao;

@InjectMocks private CalculatorServiceImpl impl = new CalculatorServiceImpl();

Page 50: Test Infected

@ankarajug #testinfected

Anatomy of a Mock Test

@Testpublic void addInvokedSuccessfully() { impl.add(4, 5);

verify(dao).save(any(CalculationResult.class));}

@Testpublic void addInvokedSuccessfully() {

when(helper.retrieveSomeStuff()). thenReturn(someNiceInstance);

impl.add(4, 5);

verify(dao).save(any(CalculationResult.class));}

Page 51: Test Infected

@ankarajug #testinfected

Anatomy of a Mock Test

@Testpublic void addInvokedSuccessfully() {

when(helper.retrieveSomeStuff()). thenReturn(someNiceInstance);

impl.add(4, 5);

verify(dao).save(any(CalculationResult.class));}

Page 52: Test Infected

@ankarajug #testinfected

Anatomy of a Mock Test

Always use static imports while using Matchers...

Also see other annotations,

@Spy

@Captor

little something something on the topic:

http://www.jroller.com/mert/entry/anatomy_of_a_mock_test

Page 53: Test Infected

@ankarajug #testinfected

Functional Testing

Enough with coding..! let’s see that it just works.. mode on...

UI Testing with Selenium with the IDE

Web Service Testing with SoapUI

Functional Testing

Load Testing

jMeter Performance Testing

Page 54: Test Infected

@ankarajug #testinfected

Code Walkthrough

Mock TestingFunctional Testing

(SoapUI, jMeter, Selenium)

Page 55: Test Infected

@ankarajug #testinfected

Continuous Integration leads to Continuous Delivery

So you have 10.000 tests... Congratz..!

But...They’ll be useless if developers need to run them by hand every time..!

We need a build triggered with each commit, right?

Page 56: Test Infected

@ankarajug #testinfected

Continuous Integration Tools out in the field

Hudson

Jenkins

Page 57: Test Infected

@ankarajug #testinfected

Release Life CycleDemystified

Pre-Alpha

Alpha

Beta

Release Candidate

Release to Manufacturing

(RTM)

General Availability

(GA)} }

Page 58: Test Infected

@ankarajug #testinfected

Release Life CyclePre-Alpha

Alpha

Beta

More of an idea in development... with nightly builds..milestones..

Working draft but probably w/ lots of bugs...do extensive testing on it...!

The prototype..features are complete..! first time available to the outsiders (open/closed beta)

The radiation level is high..! but not relevant of course, since these are the first 2 letters of greek alphabet..

White Box

Black B

ox

Grey Box

feature freeze

testing phase

Page 59: Test Infected

@ankarajug #testinfected

Release Life Cycleall features implemented/testedcall for a “code complete". you have the candidate..!

You are ready to go mass manufacturing.hardware + software bundled together.

You are now live..! you did all the commercial stuff and you are good to go.. just sell it..!!!

Release Candidate

Release to Manufacturing

(RTM)

General Availability

(GA)

code complete

ready to release

ready to advertise

Page 60: Test Infected

@ankarajug #testinfected

Continuous Integration DEMO w/ Jenkins

Page 61: Test Infected

@ankarajug #testinfected

Continuous Inspection

Metric dashboard for,

Test & Code Coverage (cobertura)

Coding Violations (checkstyle)

Triggering at each build so you’ll have incremental reports.

For installation and getting it up and running you need to,

configure its DB,

hook it with Jenkins to make more sense...

Page 62: Test Infected

@ankarajug #testinfected

Continuous Inspection DEMO w/ Sonar

Page 63: Test Infected

@ankarajug #testinfected

Continuous Integration and Inspection DEMO with

BAMBOO and CLOVER

Kerem ÇAĞLARSolveka Yazılım

Page 64: Test Infected

@ankarajug #testinfected

Best Practices

Use Matchers with static imports...Assert.assertEquals(result.getResult().doubleValue(), 9.0);

assertThat(result.getResult(), is(9.0));

Test method names can be long, no worries. They should be understood when read by someone like,retrieveListStatusByNationalIdReturnsOneElementWhenThereIsManuallyAddedPersonWithExemptedInfo()

Page 65: Test Infected

@ankarajug #testinfected

Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead

(Martin Fowler)

Code without automated test simply doesn’t exist.

Once you get them running, make sure they stay running.

If you can’t test it, just trash it.

Some

Page 66: Test Infected

@ankarajug #testinfected

We do not need private methods, get rid of them..!

Run the fast tests first.. group them.. If they fail you’ll get notified early.

Test your app with different localizations (for sorting and etc.)

Increase your coverage for sleeping tight at night.

Keep the bar green to keep the code clean.

You want to test the code, not yourself. Don’t be the keyboard monkey...

Some

Page 67: Test Infected

@ankarajug #testinfected@ankarajug #testinfected

You cannot test EVERYTHING..!

Page 68: Test Infected

@ankarajug #testinfected