unit testing j2ee from jruby - the hippy hacker

62
Unit Testing J2EE from JRuby Evan Light http://evan.tiggerpalace.com

Upload: others

Post on 11-Sep-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit Testing J2EE from JRuby - The Hippy Hacker

Unit Testing J2EE from JRuby

Evan Lighthttp://evan.tiggerpalace.com

Page 2: Unit Testing J2EE from JRuby - The Hippy Hacker

Who I am

• Professional developer since 1996

• Java since 1999

• J2EE since 2000

• Ruby since 2006

Page 3: Unit Testing J2EE from JRuby - The Hippy Hacker

Some yutz with Keynote and a

remote control

Page 4: Unit Testing J2EE from JRuby - The Hippy Hacker

Pop Quiz

Page 5: Unit Testing J2EE from JRuby - The Hippy Hacker

But why test?

Page 6: Unit Testing J2EE from JRuby - The Hippy Hacker

Find bugs

Prove out design

Simplify refactoring

Document behavior

Page 7: Unit Testing J2EE from JRuby - The Hippy Hacker

Document behavior?

Page 8: Unit Testing J2EE from JRuby - The Hippy Hacker

Time for a visitto the eye doctor

Page 9: Unit Testing J2EE from JRuby - The Hippy Hacker

#1

From the JUnit Cookbook

Page 10: Unit Testing J2EE from JRuby - The Hippy Hacker

#2

Page 11: Unit Testing J2EE from JRuby - The Hippy Hacker

Why Ruby?

Page 12: Unit Testing J2EE from JRuby - The Hippy Hacker

Why not?

Page 13: Unit Testing J2EE from JRuby - The Hippy Hacker

Why do we learn new programming

languages?

Page 14: Unit Testing J2EE from JRuby - The Hippy Hacker

Better runtime performance

Greater developer productivity

Better user experience

Expand technical outlook

Page 15: Unit Testing J2EE from JRuby - The Hippy Hacker

Why?

Page 16: Unit Testing J2EE from JRuby - The Hippy Hacker

To earn money!!!

Page 17: Unit Testing J2EE from JRuby - The Hippy Hacker

Story Time, Part I(a.k.a How I found Ruby)

Page 18: Unit Testing J2EE from JRuby - The Hippy Hacker

• Secure enterprise document mgmt system

• 6 person team

• Cannot add people

• Cannot reduce scope

• Cannot reduce quality

The Setup

Page 19: Unit Testing J2EE from JRuby - The Hippy Hacker

• 6 person-year schedule estimate

• 6 people / 6 person-years = 1 year

• Customer already waited 1 year!

The J2EE Estimate

Page 20: Unit Testing J2EE from JRuby - The Hippy Hacker

How can we do better?

Page 21: Unit Testing J2EE from JRuby - The Hippy Hacker

Increase productivity

Page 22: Unit Testing J2EE from JRuby - The Hippy Hacker

Enter Ruby on Rails

Page 23: Unit Testing J2EE from JRuby - The Hippy Hacker

The Outcome

3 people x 3 months =

50% of project

Page 24: Unit Testing J2EE from JRuby - The Hippy Hacker

JRuby

Page 25: Unit Testing J2EE from JRuby - The Hippy Hacker

Ruby

... and then some

Page 26: Unit Testing J2EE from JRuby - The Hippy Hacker

Ruby objects are Java objects

Java garbage collection

Java threading

Ruby on Rails apps as WARs

... and Java Integration

Page 27: Unit Testing J2EE from JRuby - The Hippy Hacker

But u want to put ur Ruby in my Java?!

Page 28: Unit Testing J2EE from JRuby - The Hippy Hacker

Not exactly...

Page 29: Unit Testing J2EE from JRuby - The Hippy Hacker

We’re here to talk about J2EE testing

(And it only took him 31 slides to say that)

Page 30: Unit Testing J2EE from JRuby - The Hippy Hacker

My “favorite”

EJBs

Page 31: Unit Testing J2EE from JRuby - The Hippy Hacker

EJB 3.0

Container DI

Encourages difficult-to-test idioms

Private fields

Private static final fields

Page 32: Unit Testing J2EE from JRuby - The Hippy Hacker
Page 33: Unit Testing J2EE from JRuby - The Hippy Hacker

How do you test that?!

Page 34: Unit Testing J2EE from JRuby - The Hippy Hacker

Solution #1Don’t write it

Page 35: Unit Testing J2EE from JRuby - The Hippy Hacker

What if I inherited it?

Page 36: Unit Testing J2EE from JRuby - The Hippy Hacker

Solution #2Work around it

Page 37: Unit Testing J2EE from JRuby - The Hippy Hacker

You could...... write a pre-compiler and integrate it into your ANT/Maven tasks, that would use Java reflection to introspect on each Java class under test. The precompiler could generate code for a setter and getter wrapper for each non-public field and a wrapper for each non-public method that, in turn, tell the Java SecurityManager that it is kosher to use Java reflection to access/invoke the respective non-public field/method and then do their getting/setting/invoking via reflection. You could even write this as part of an Eclipse-plugin that generates the code earlier so that you could have access to these wrappers while you’re writing your tests in the first place. Hey, that may even give you type-checking while you’re writing your tests which would be awfully nice. But wouldn’t the resulting test code be a little awkward to write and not particularly readable for developers down the line?

Page 38: Unit Testing J2EE from JRuby - The Hippy Hacker

Solution #3:JRuby

Page 39: Unit Testing J2EE from JRuby - The Hippy Hacker

With a little help from jrsplenda

Page 40: Unit Testing J2EE from JRuby - The Hippy Hacker

This just works

Page 41: Unit Testing J2EE from JRuby - The Hippy Hacker

So how do you test this?

Page 42: Unit Testing J2EE from JRuby - The Hippy Hacker

One step at a time

Page 43: Unit Testing J2EE from JRuby - The Hippy Hacker

jrsplenda

JRuby helpers to simplify testing Java code

Hosted on GitHub

http://github.com/elight/jrsplenda/tree/master

Designed for Mocha (hence the name)

Page 44: Unit Testing J2EE from JRuby - The Hippy Hacker

Field and Method helpers

At runtime, generates wrappers to:

Set non-public Fields

Invoke non-public Methods

JRuby provides non-public Field getters

Page 45: Unit Testing J2EE from JRuby - The Hippy Hacker

Caveats

Cannot wrap a final Field

As of JRuby 1.1.2, non-public field getters

Provided for Java instance Fields

Not provided for Java class Fields

Page 46: Unit Testing J2EE from JRuby - The Hippy Hacker

Caveats (cont’d)

May cause:

Diarrhea

Upset stomach

Page 47: Unit Testing J2EE from JRuby - The Hippy Hacker

Test setup of DI

Page 48: Unit Testing J2EE from JRuby - The Hippy Hacker

Mock objectsat 50k feet

Special kind of stub

Set expectations on a mock

Inject mock into path of code to be tested

Test fails if expectation is not met

Good read: Mock Roles, Not Objects

Page 49: Unit Testing J2EE from JRuby - The Hippy Hacker

jrsplenda mock helper

Borrowed from Ola Bini’s JtestR

Create mock Java objects within JRuby

Set expectations in JRuby

DI mocks from JRuby into Java

Exercise Java code from JRuby

Page 50: Unit Testing J2EE from JRuby - The Hippy Hacker

In future releasesof jrsplenda

Partial mocks

Until implemented, tests must include additional stubbing

Support for other Ruby mocking frameworks

Page 51: Unit Testing J2EE from JRuby - The Hippy Hacker

Test setup of mocking

Page 52: Unit Testing J2EE from JRuby - The Hippy Hacker

We’ve made Java testing simpler

Page 53: Unit Testing J2EE from JRuby - The Hippy Hacker

Now let’s make it more expressive

Page 54: Unit Testing J2EE from JRuby - The Hippy Hacker

Remember this?

Page 55: Unit Testing J2EE from JRuby - The Hippy Hacker

RSpec

http://rspec.info/

Ruby Example-driven (Testing) DSL

Couples documentation with behavior

Page 56: Unit Testing J2EE from JRuby - The Hippy Hacker

Walkthrough

Page 57: Unit Testing J2EE from JRuby - The Hippy Hacker

Putting it all together

Page 58: Unit Testing J2EE from JRuby - The Hippy Hacker
Page 59: Unit Testing J2EE from JRuby - The Hippy Hacker

Specification (beginning)

Page 60: Unit Testing J2EE from JRuby - The Hippy Hacker

Example setup

Page 61: Unit Testing J2EE from JRuby - The Hippy Hacker

The examples

Page 62: Unit Testing J2EE from JRuby - The Hippy Hacker

Creative Commons License

By PhotoGraham on Flickr