SlideShare a Scribd company logo
Hurdle run through EJB testing

Jakub 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 Automotive Group
    Working on Operation Data Store
   Realtime view for Data Warehouse
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 enterprise (from now on)
According to the spec

  You are now about to
         Develop
        Assemble
         Deploy
           Run
  an Enterprise Solution
Emmm…

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

It’s a Tester Driven Development
          … bitch please!
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
But seriously – Test Pyramid
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 time




http://www.slideshare.net/sebrose/common-objections-to-tdd-and-their-refutations
GeeCON 2012 hurdle run through ejb testing
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
No tests in EJB?

If these are integretion tests…
           It means…
   There are no tests in EJB…
            Woohoo!
           Quick win!
Ekhmmm…

 Is EJB the only framework?
How do other tackle testing?
GeeCON 2012 hurdle run through ejb testing
You know when it’s Spring

Rod Johnson says it’s all about testing
Dependency Injection


@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleConfigurationTests {
    @Autowired
    ApplicationContext ctx;

    @Autowired
    MyService service;

    @Test
    public void servletTest() throws Exception {

        //.. yada yada
    }

}
Database tests



<beans>
    <jdbc:embedded-database id="dataSource" type="HSQL">
         <jdbc:script location="import.sql"/>
    </jdbc:embedded-database>
</beans>
Spring + Web
@Autowired
ApplicationContext ctx;

@Autowired
TaskController controller;

@Test
public 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());
}
Spring + Web (2)

MockMvcBuilders.annotationConfigSetup(TestConfiguration.class)
    .build()
    .perform(get("/resources/Spring.js"))
    .andExpect(content().type("application/octet-stream"))
    .andExpect(content().string(containsString("Spring={};")));
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.");
There are frameworks…

           … and frameworks
Some things are built with testing in mind
But we are to talk about EJB

    … available since 2006 (EJB 3.0)

      Vendors deliver a platform
And 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)
Back to The Pyramid
Let’s have a look

             Welcome the sample application




https://github.com/kubamarchwicki/geecon-ejbtest-workshop
Mate! It’s IDE time!
Mocks are tactical
Fully blown integration

There 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!
GeeCON 2012 hurdle run through ejb testing
What’s else - Open EJB


yet 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!



                 CONS
Different than your production container
What’s else (2) - Arquillian

   wooow, so there is a test framework for EJB
What’s else (2) - Arquillian

                  PROS
Closer to actual production environment
 Independent from the actual container
What’s else (2) - Arquillian

                  PROS
Closer to actual production environment
 Independent from the actual container



                 CONS
           Just graduated
Our container’s adapter is still in alpha
Arquillian seems nice

      Manage lifecycle for you
      Shrinkwrap creates an archive
Adapters link between any container and arquillian
 Remote testing / embedded testing
Mate! It’s IDE time!
Jakub www.marchwicki.pl
       @kubem

More Related Content

PDF
Testacular
PPTX
Principles and patterns for test driven development
ZIP
Unit Testing in Java
PDF
Jest: Frontend Testing leicht gemacht @EnterJS2018
PDF
Need(le) for Speed - Effective Unit Testing for Java EE
KEY
Testing w-mocks
PPTX
Automated php unit testing in drupal 8
PDF
Unit-testing and E2E testing in JS
Testacular
Principles and patterns for test driven development
Unit Testing in Java
Jest: Frontend Testing leicht gemacht @EnterJS2018
Need(le) for Speed - Effective Unit Testing for Java EE
Testing w-mocks
Automated php unit testing in drupal 8
Unit-testing and E2E testing in JS

What's hot (20)

PPTX
Test driving-qml
PDF
Unit testing legacy code
PPTX
Working Effectively with Legacy Code
PDF
Unit Test + Functional Programming = Love
PDF
Working With Legacy Code
PDF
Unit Testing your React / Redux app (@BucharestJS)
PPTX
DotNet unit testing training
PDF
Efficient JavaScript Unit Testing, May 2012
PPT
Unit testing with java
PPTX
Php unit for drupal 8
PPTX
Test driving QML
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
Mutation Testing
DOCX
Test driven development and unit testing with examples in C++
PDF
Test all the things! Automated testing with Drupal 8
PPT
Getting Unstuck: Working with Legacy Code and Data
KEY
Jellyfish, JSCONF 2011
PPTX
Testing React Applications
ODP
Writing useful automated tests for the single page applications you build
PPT
JavaScript Unit Testing
Test driving-qml
Unit testing legacy code
Working Effectively with Legacy Code
Unit Test + Functional Programming = Love
Working With Legacy Code
Unit Testing your React / Redux app (@BucharestJS)
DotNet unit testing training
Efficient JavaScript Unit Testing, May 2012
Unit testing with java
Php unit for drupal 8
Test driving QML
3 WAYS TO TEST YOUR COLDFUSION API
Mutation Testing
Test driven development and unit testing with examples in C++
Test all the things! Automated testing with Drupal 8
Getting Unstuck: Working with Legacy Code and Data
Jellyfish, JSCONF 2011
Testing React Applications
Writing useful automated tests for the single page applications you build
JavaScript Unit Testing
Ad

Similar to GeeCON 2012 hurdle run through ejb testing (20)

ODP
Building JBoss AS 7 for Fedora
PPTX
Whitebox testing of Spring Boot applications
PPTX
In Memory Unit Testing with Apache DbUnit
PPT
Sadiq786
PDF
軟體測試是在測試什麼?
PDF
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
PPTX
Lessons Learned in Software Development: QA Infrastructure – Maintaining Rob...
PPT
EJB 3.0 Java Persistence with Oracle TopLink
PPTX
OpenDaylight Developer Experience 2.0
PPTX
Ten Man-Years of JavaFX: Real World Project Experiences
PDF
The Many Ways to Test Your React App
PPTX
Js tacktalk team dev js testing performance
PDF
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
PDF
TDD super mondays-june-2014
KEY
Agile JavaScript Testing
ODP
TDD in the Web with Python and Django
PPT
Effective Test Driven Database Development
PDF
Integration testing - A&BP CC
PDF
Gallio Crafting A Toolchain
Building JBoss AS 7 for Fedora
Whitebox testing of Spring Boot applications
In Memory Unit Testing with Apache DbUnit
Sadiq786
軟體測試是在測試什麼?
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Lessons Learned in Software Development: QA Infrastructure – Maintaining Rob...
EJB 3.0 Java Persistence with Oracle TopLink
OpenDaylight Developer Experience 2.0
Ten Man-Years of JavaFX: Real World Project Experiences
The Many Ways to Test Your React App
Js tacktalk team dev js testing performance
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
TDD super mondays-june-2014
Agile JavaScript Testing
TDD in the Web with Python and Django
Effective Test Driven Database Development
Integration testing - A&BP CC
Gallio Crafting A Toolchain
Ad

More from Jakub Marchwicki (8)

PPTX
Test with Spock like the first officer
PPTX
Design principles 4 hackers - tech3camp (28142014)
PPTX
JEE.next()
PPTX
[PL] Jak programować aby nie zwariować
PPT
GeeCON 2013 - EJB application guided by tests
PPTX
[PL] Jak programować aby nie zwariować?
PPT
[PL] Metadane - dane o danych
PDF
[PL] O klasycznej, programistycznej elegancji
Test with Spock like the first officer
Design principles 4 hackers - tech3camp (28142014)
JEE.next()
[PL] Jak programować aby nie zwariować
GeeCON 2013 - EJB application guided by tests
[PL] Jak programować aby nie zwariować?
[PL] Metadane - dane o danych
[PL] O klasycznej, programistycznej elegancji

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
MYSQL Presentation for SQL database connectivity
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
cuic standard and advanced reporting.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
MYSQL Presentation for SQL database connectivity
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
cuic standard and advanced reporting.pdf
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4

GeeCON 2012 hurdle run through ejb testing

  • 1. Hurdle run through EJB testing Jakub www.marchwicki.pl / @kubem
  • 2. This is not only about testing Neither about EJB Nor TDD Just somewhere in between
  • 3. WHO IS THIS GUY?!
  • 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. So let’s start with EJB Is it a a plane? Is it a bird? Is it a framework?
  • 6. JEE takes all these technologies Puts it together In a single server Big server And we are enterprise (from now on)
  • 7. According to the spec You are now about to Develop Assemble Deploy Run an Enterprise Solution
  • 8. Emmm… - Are we testing anything here, sir? - Nah… Test are for testers, n00b!
  • 9. TDD It’s a Tester Driven Development … bitch please!
  • 12. But seriously – Test Pyramid
  • 13. 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 time http://www.slideshare.net/sebrose/common-objections-to-tdd-and-their-refutations
  • 15. 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
  • 16. No tests in EJB? If these are integretion tests… It means… There are no tests in EJB… Woohoo! Quick win!
  • 17. Ekhmmm… Is EJB the only framework? How do other tackle testing?
  • 19. You know when it’s Spring Rod Johnson says it’s all about testing
  • 20. Dependency Injection @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class ExampleConfigurationTests { @Autowired ApplicationContext ctx; @Autowired MyService service; @Test public void servletTest() throws Exception { //.. yada yada } }
  • 21. Database tests <beans> <jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="import.sql"/> </jdbc:embedded-database> </beans>
  • 22. Spring + Web @Autowired ApplicationContext ctx; @Autowired TaskController controller; @Test public 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()); }
  • 23. Spring + Web (2) MockMvcBuilders.annotationConfigSetup(TestConfiguration.class) .build() .perform(get("/resources/Spring.js")) .andExpect(content().type("application/octet-stream")) .andExpect(content().string(containsString("Spring={};")));
  • 24. 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.");
  • 25. There are frameworks… … and frameworks Some things are built with testing in mind
  • 26. But we are to talk about EJB … available since 2006 (EJB 3.0) Vendors deliver a platform And 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)
  • 27. Back to The Pyramid
  • 28. Let’s have a look Welcome the sample application https://github.com/kubamarchwicki/geecon-ejbtest-workshop
  • 31. Fully blown integration There is a hassle with maintaing DI Hello… calling dr. Container It’s your job afterall
  • 32. Are there other options? Embed a container bootsrtap whole application test end2end
  • 35. What’s else - Open EJB yet another embedded container
  • 36. What’s else - Open EJB PROS It just works! Selective classpath scanning!
  • 37. What’s else - Open EJB PROS It just works! Selective classpath scanning! CONS Different than your production container
  • 38. What’s else (2) - Arquillian wooow, so there is a test framework for EJB
  • 39. What’s else (2) - Arquillian PROS Closer to actual production environment Independent from the actual container
  • 40. What’s else (2) - Arquillian PROS Closer to actual production environment Independent from the actual container CONS Just graduated Our container’s adapter is still in alpha
  • 41. Arquillian seems nice Manage lifecycle for you Shrinkwrap creates an archive Adapters link between any container and arquillian Remote testing / embedded testing