hurdle run through ejb testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf ·...

43
Hurdle run through EJB testing Jakub www.marchwicki.pl / @kubem

Upload: others

Post on 09-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Hurdle run through EJB testing

Jakub www.marchwicki.pl / @kubem

Page 2: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

This is not only about testing

Neither about EJBNor TDD

Just somewhere in between

Page 3: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Who is this guy?!

Page 4: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Business intelligence and data modellingInteligent marketing

Data mining

I’m part of Automotive Group Working on Operation Data Store

Realtime view for Data Warehouse

Page 5: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

So let’s start with EJB

Is it a a plane? Is it a bird?

Is it a framework?

Page 6: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

JEE takes all these technologies

Puts it togetherIn a single server

Big serverAnd we are enterprise (from now on)

Page 7: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

According to the spec

You are now about toDevelop

AssembleDeploy

Runan Enterprise Solution

Page 8: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Emmm…

- Are we testing anything here, sir?- Nah… Test are for testers, n00b!

Page 9: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

TDD

It’s a Tester Driven Development… bitch please!

Page 10: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!
Page 11: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!
Page 12: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

But seriously – Test Pyramid

Page 13: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Tests…?

But tests slows me downThey need maintenanceThis is too simple to test

And I don’t make mistakes afterallThis is legacy code, there are no tests

Build takes time… more time

http://www.slideshare.net/sebrose/common-objections-to-tdd-and-their-refutations

Page 14: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!
Page 15: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Start from the bottom

Unit tests don’t:talk to the database

communicate over networktouch the file system

need to be run independently (tests can run simultaneously)

need special environment configuration

Page 16: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

No tests in EJB?

If these are integretion tests…It means…

There are no tests in EJB…Woohoo!

Quick win!

Page 17: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Ekhmmm…

Is EJB the only framework?How do other tackle testing?

Page 18: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!
Page 19: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

You know when it’s Spring

Rod Johnson says it’s all about testing

Page 20: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Dependency Injection

@ContextConfiguration@RunWith(SpringJUnit4ClassRunner.class)public class ExampleConfigurationTests {

@AutowiredApplicationContext ctx;

@AutowiredMyService service;

@Testpublic void servletTest() throws Exception {

//.. yada yada}

}

Page 21: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Database tests

<beans><jdbc:embedded-database id="dataSource" type="HSQL">

<jdbc:script location="import.sql"/></jdbc:embedded-database>

</beans>

Page 22: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Spring + Web@AutowiredApplicationContext ctx;

@AutowiredTaskController controller;

@Testpublic void servletTest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest("GET", "/tasks"); MockHttpServletResponse resp = new MockHttpServletResponse(); HandlerAdapter handlerAdapter = ctx.getBean(AnnotationMethodHandlerAdapter.class); final ModelAndView model = handlerAdapter.handle(req, resp, controller);

assertViewName(model, "tasks"); assertAndReturnModelAttributeOfType(model, "task", Task.class); assertAndReturnModelAttributeOfType(model, "tasks", List.class);

//.. create testTask object

assertModelAttributeValue(model, "tasks", Arrays.asList(testTask)); assertModelAttributeValue(model, "task", new Task());}

Page 23: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Spring + Web (2)

MockMvcBuilders.annotationConfigSetup(TestConfiguration.class).build() .perform(get("/resources/Spring.js"))

.andExpect(content().type("application/octet-stream"))

.andExpect(content().string(containsString("Spring={};")));

Page 24: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

RestEASY

MockHttpRequest request = MockHttpRequest.get("/user/1234");MockHttpResponse response = new MockHttpResponse();

Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();dispatcher.invoke(request, response);assertEquals(response.getStatus(), HttpServletResponse.SC_METHOD_NOT_ALLOWED);

URI uri = UriBuilder.fromPath("/user/{arg1}/delete").build(clientId);

MockHttpRequest request = MockHttpRequest.get(uri.toString());MockHttpResponse response = new MockHttpResponse();

dispatcher.invoke(request, response);assertEquals(response.getStatus(), HttpServletResponse.SC_OK);assertTrue(response.getContentAsString().contains("def"),

"Response should contain 'def' result.");assertTrue(response.getContentAsString().contains("xx"),

"Response should contain 'xx' result.");assertTrue(response.getContentAsString().contains("123"),

"Response should contain '123' result.");

Page 25: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

There are frameworks…

… and frameworksSome things are built with testing in mind

Page 26: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

But we are to talk about EJB

… available since 2006 (EJB 3.0)

Vendors deliver a platformAnd they don’t really care about testing

Just until now… 6 f**kn yearsEJB 3.1 is also live

Still noone cares about tests (nearly noone)

Page 27: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Back to The Pyramid

Page 28: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Let’s have a look

Welcome the sample application

https://github.com/kubamarchwicki/geecon-ejbtest-workshop

Page 29: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Mate! It’s IDE time!

Page 30: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Mocks are tactical

Page 31: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Fully blown integration

There is a hassle with maintaing DIHello… calling dr. Container

It’s your job afterall

Page 32: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Are there other options?

Embed a container bootsrtap whole application

test end2end

Page 33: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Mate! It’s IDE time!

Page 34: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!
Page 35: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else - Open EJB

yet another embedded container

Page 36: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else - Open EJB

PROSIt just works!

Selective classpath scanning!

Page 37: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else - Open EJB

PROSIt just works!

Selective classpath scanning!

CONSDifferent than your production container

Page 38: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else (2) - Arquillian

wooow, so there is a test framework for EJB

Page 39: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else (2) - Arquillian

PROSCloser to actual production environmentIndependent from the actual container

Page 40: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

What’s else (2) - Arquillian

PROSCloser to actual production environmentIndependent from the actual container

CONSJust graduated

Our container’s adapter is still in alpha

Page 41: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Arquillian seems nice

Manage lifecycle for youShrinkwrap creates an archive

Adapters link between any container and arquillian

Remote testing / embedded testing

Page 42: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

Mate! It’s IDE time!

Page 43: Hurdle run through EJB testings3-eu-west-1.amazonaws.com/presentations2012/18_presentation.pdf · Test are for testers, n00b! TDD It’s a Tester Driven Development … bitch please!

???Jakub www.marchwicki.pl

@kubem