Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Java with Groovy

Testing Java with Groovy

What makes Groovy particularly appealing with respect to other scripting languages is its seamless integration with the Java platform. Because it's based on the Java, Groovy presents an incredibly short learning curve for the Java developer. And once that learning curve has straightened out, Groovy can offer an unparalleled rapid development platform. The fact is, Groovy's relaxed Java-like syntax, its reuse of standard Java libraries, and its rapid build-and-run cycle make it an ideal candidate for rapidly developing unit tests.

Tiago Fernandez

May 24, 2012
Tweet

More Decks by Tiago Fernandez

Other Decks in Programming

Transcript

  1. def numbers = [4, 8, 15, 16, 23, 42] numbers

    << 99 def vowels = ['a', 'e', 'i', 'o', 'u'].asImmutable() vowels << 'x' // UnsupportedOperationException def colors = [ red: [255, 0, 0], green: [0, 255, 0], blue: [0, 0, 255] ]
  2. def frameworks = ['Grails', 'Gaelyk', 'Griffon'] def languages = [

    Groovy: 'groovy.codehaus.org', Ruby: 'ruby-lang.org' ] frameworks.each { value -> println "Framework: $value" } languages.each { key, value -> println "$key's URL: http://$value/" }
  3. def pets = ['cat', 'dog', 'fish', 'hamster', 'parrot'] def result1

    = pets.collect { it[0].toUpperCase() } assert result1 == ['C', 'D', 'F', 'H', 'P'] def result2 = pets.findAll { it.size() <= 3 } assert result2 == ['cat', 'dog'] def result3 = pets.grep(~/^f.*/) assert result3 == ['fish']
  4. class LetterTemplate { String from, to, message String getLetter() {

    """ ${new Date()} Dear $to, $message Best regards, $from """ } } def template = new LetterTemplate( from: 'Tiago', to: 'Cynthia', message: '...') println template.letter
  5. import groovy.xml.MarkupBuilder new MarkupBuilder().html { head { title 'Programming Groovy,

    book by @venkat_s' } body { div(width: '100') { p(class: 'para') { span 'Best book to get started with Groovy!' span 'ISBN: 1934356093' } } } }
  6. // More assertions class FooTest extends GroovyTestCase { void setUp()

    { ... } void tearDown() { ... } void test_should_do_something() { ... } } // No inheritance import org.junit.* class BarTest { @Before void setUp() { ... } @After void tearDown() { ... } @Test void should_do_something() { ... } }
  7. def a = 10, b = 30 assert Math.max(a, b)

    == 20 Assertion failed: assert Math.max(a, b) == 20 | | | | 30 10 30 false
  8. assertArrayEquals(Object[] expected, Object[] value) assertLength(int length, char[] array) assertLength(int length,

    int[] array) assertLength(int length, Object[] array) assertContains(char expected, char[] array) assertContains(int expected, int[] array) assertToString(Object value, String expected) assertInspect(Object value, String expected) assertScript(final String script) shouldFail(Closure code) shouldFail(Class clazz, Closure code)
  9. class ArithmeticTest extends GroovyTestCase { void test_should_not_divide_by_zero() { shouldFail(ArithmeticException) {

    println 1/0 } } } class ArithmeticTest { final shouldFail = new GroovyTestCase().&shouldFail @Test void should_not_divide_by_zero() { shouldFail(ArithmeticException) { println 1/0 } } }
  10. // Production code (Java) public class SocialService { private final

    CassandraStorage storage; } public class CassandraStorage { private final Cluster cluster; } // Test code (Groovy) void tearDown() { def cluster = socialService.storage.cluster assert cluster.dropKeyspace('Social') != null }
  11. def task = { println "Running..." } as Runnable new

    Thread(task).start() def messageSender = { from, to, message -> sentMessages << new Message(from, to, message) } as MessageSender def httpRequest = [ isUserInRole: { roleName -> roleName == "test_role“ } ] as HttpServletRequest
  12. Available  to  Groovy  classes  only: ๏ Expandos ๏ Categories ๏

    StubFor  &  MockFor StringUtils.metaClass.static.lowerCase = { str -> str.toUpperCase() } // won’t work