Hurdle run through EJB testingJakub www.marchwicki.pl / @kubem
This is not only about testing        Neither about EJB             Nor TDD   Just somewhere in between
WHO IS THIS GUY?!
Business intelligence and data modelling          Inteligent marketing               Data mining     I’m part of Automotiv...
So let’s start with EJB      Is it a a plane?         Is it a bird?    Is it a framework?
JEE takes all these technologies           Puts it together          In a single server              Big server And we are...
According to the spec  You are now about to         Develop        Assemble         Deploy           Run  an Enterprise So...
Emmm…- Are we testing anything here, sir? - Nah… Test are for testers, n00b!
TDDIt’s a Tester Driven Development          … bitch please!
But seriously – Test Pyramid
Tests…?                   But tests slows me down                   They need maintenance                   This is too si...
Start from the bottom          Unit tests don’t:        talk to the database     communicate over network        touch the...
No tests in EJB?If these are integretion tests…           It means…   There are no tests in EJB…            Woohoo!       ...
Ekhmmm… Is EJB the only framework?How do other tackle testing?
You know when it’s SpringRod Johnson says it’s all about testing
Dependency Injection@ContextConfiguration@RunWith(SpringJUnit4ClassRunner.class)public class ExampleConfigurationTests {  ...
Database tests<beans>    <jdbc:embedded-database id="dataSource" type="HSQL">         <jdbc:script location="import.sql"/>...
Spring + Web@AutowiredApplicationContext ctx;@AutowiredTaskController controller;@Testpublic void servletTest() throws Exc...
Spring + Web (2)MockMvcBuilders.annotationConfigSetup(TestConfiguration.class)    .build()    .perform(get("/resources/Spr...
RestEASYMockHttpRequest request = MockHttpRequest.get("/user/1234");MockHttpResponse response = new MockHttpResponse();Dis...
There are frameworks…           … and frameworksSome things are built with testing in mind
But we are to talk about EJB    … available since 2006 (EJB 3.0)      Vendors deliver a platformAnd they don’t really care...
Back to The Pyramid
Let’s have a look             Welcome the sample applicationhttps://github.com/kubamarchwicki/geecon-ejbtest-workshop
Mate! It’s IDE time!
Mocks are tactical
Fully blown integrationThere is a hassle with maintaing DI   Hello… calling dr. Container       It’s your job afterall
Are there other options?     Embed a container bootsrtap whole application        test end2end
Mate! It’s IDE time!
What’s else - Open EJByet another embedded container
What’s else - Open EJB             PROS        It just works!Selective classpath scanning!
What’s else - Open EJB                  PROS             It just works!     Selective classpath scanning!                 ...
What’s else (2) - Arquillian   wooow, so there is a test framework for EJB
What’s else (2) - Arquillian                  PROSCloser to actual production environment Independent from the actual cont...
What’s else (2) - Arquillian                  PROSCloser to actual production environment Independent from the actual cont...
Arquillian seems nice      Manage lifecycle for you      Shrinkwrap creates an archiveAdapters link between any container ...
Mate! It’s IDE time!
Jakub www.marchwicki.pl       @kubem
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
Upcoming SlideShare
Loading in …5
×

GeeCON 2012 hurdle run through ejb testing

820 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

GeeCON 2012 hurdle run through ejb testing

  1. 1. Hurdle run through EJB testingJakub www.marchwicki.pl / @kubem
  2. 2. This is not only about testing Neither about EJB Nor TDD Just somewhere in between
  3. 3. WHO IS THIS GUY?!
  4. 4. Business intelligence and data modelling Inteligent marketing Data mining I’m part of Automotive Group Working on Operation Data Store Realtime view for Data Warehouse
  5. 5. So let’s start with EJB Is it a a plane? Is it a bird? Is it a framework?
  6. 6. JEE takes all these technologies Puts it together In a single server Big server And we are enterprise (from now on)
  7. 7. According to the spec You are now about to Develop Assemble Deploy Run an Enterprise Solution
  8. 8. Emmm…- Are we testing anything here, sir? - Nah… Test are for testers, n00b!
  9. 9. TDDIt’s a Tester Driven Development … bitch please!
  10. 10. But seriously – Test Pyramid
  11. 11. Tests…? But tests slows me down They need maintenance This is too simple to test And I don’t make mistakes afterall This is legacy code, there are no tests Build takes time… more timehttp://www.slideshare.net/sebrose/common-objections-to-tdd-and-their-refutations
  12. 12. Start from the bottom Unit tests don’t: talk to the database communicate over network touch the file system need to be run independently (tests can run simultaneously)need special environment configuration
  13. 13. No tests in EJB?If these are integretion tests… It means… There are no tests in EJB… Woohoo! Quick win!
  14. 14. Ekhmmm… Is EJB the only framework?How do other tackle testing?
  15. 15. You know when it’s SpringRod Johnson says it’s all about testing
  16. 16. Dependency Injection@ContextConfiguration@RunWith(SpringJUnit4ClassRunner.class)public class ExampleConfigurationTests { @Autowired ApplicationContext ctx; @Autowired MyService service; @Test public void servletTest() throws Exception { //.. yada yada }}
  17. 17. Database tests<beans> <jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="import.sql"/> </jdbc:embedded-database></beans>
  18. 18. 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());}
  19. 19. Spring + Web (2)MockMvcBuilders.annotationConfigSetup(TestConfiguration.class) .build() .perform(get("/resources/Spring.js")) .andExpect(content().type("application/octet-stream")) .andExpect(content().string(containsString("Spring={};")));
  20. 20. RestEASYMockHttpRequest 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.");
  21. 21. There are frameworks… … and frameworksSome things are built with testing in mind
  22. 22. 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 years EJB 3.1 is also live Still noone cares about tests (nearly noone)
  23. 23. Back to The Pyramid
  24. 24. Let’s have a look Welcome the sample applicationhttps://github.com/kubamarchwicki/geecon-ejbtest-workshop
  25. 25. Mate! It’s IDE time!
  26. 26. Mocks are tactical
  27. 27. Fully blown integrationThere is a hassle with maintaing DI Hello… calling dr. Container It’s your job afterall
  28. 28. Are there other options? Embed a container bootsrtap whole application test end2end
  29. 29. Mate! It’s IDE time!
  30. 30. What’s else - Open EJByet another embedded container
  31. 31. What’s else - Open EJB PROS It just works!Selective classpath scanning!
  32. 32. What’s else - Open EJB PROS It just works! Selective classpath scanning! CONSDifferent than your production container
  33. 33. What’s else (2) - Arquillian wooow, so there is a test framework for EJB
  34. 34. What’s else (2) - Arquillian PROSCloser to actual production environment Independent from the actual container
  35. 35. What’s else (2) - Arquillian PROSCloser to actual production environment Independent from the actual container CONS Just graduatedOur container’s adapter is still in alpha
  36. 36. Arquillian seems nice Manage lifecycle for you Shrinkwrap creates an archiveAdapters link between any container and arquillian Remote testing / embedded testing
  37. 37. Mate! It’s IDE time!
  38. 38. Jakub www.marchwicki.pl @kubem

×