DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > GrUnit: inline tests in Groovy

GrUnit: inline tests in Groovy

Alex Tkachman user avatar by
Alex Tkachman
·
Apr. 01, 10 · Java Zone · Interview
Like (0)
Save
Tweet
10.30K Views

Join the DZone community and get the full member experience.

Join For Free

I love tests. I need tests. But I need easier way to write tests. Inline tests is a way to write test directly in the Groovy source code. This article is direct continualtion of GrUnit: Groovy way of unit testing and almost everything described there is applicable to inline tests.

So what is inline test?

Inline test is snippet of code inside class/constructor/method annotation, which leads to creation of test class right from source code. There are two main reasons why is it useful. First of all creation of tests require some ceremony (to create test class in separate test directory, create test methods) an we are lazy (at least I am). But may be more important is that when we develop API of the class it is often very convinient to write some test cases (and we don't want to switch to another file for that)

Let me show you one example, which tells more than thousand words

@GrUnit({
@Field Calculator calc
setUp { calc = [] }
})
@Typed
class Calculator {
private ArrayList<Double> list = []

@GrUnit({
assertEquals(10d, calc.push(10d).list[-1])
})
Calculator push(double v) {
list.push(v)
this
}

@GrUnit({
shouldFail { calc.peek() }
})
double peek() {
list[-1]
}

@GrUnit({
testFailIfEmpty{ shouldFail { calc.pop() } }
testPushPop {
assertEquals(10d, calc.push(10d).pop())
assertEquals(0, calc.list)
}
})
double pop() {
list.pop()
}

@GrUnit({
assertEquals (4d, calc.push(4d).peek())
testPlusNull{ assertEquals (10d, calc.push(6).plus().peek()) }
testPlusNonNull {assertEquals (10d, calc.plus(6).peek())}
})
Calculator plus(Double value = null) {
push((value == null ? pop () : value) + pop ())
}
}

If you had time to read my previous article about GrUnit then most probably you can imagine what's going on - we generate static inner class extending GroovyTestCase and create test method as specified by @GrUnit annotation. Please refer to mentioned article for details about setUp () and accumulated tests.

Here is screenshort of IntelliJ after running tests - all together we have created 7 tests (and btw, nice documentation right in the code)

 There is small trick here. Careful reader can notice on the screenshot groovy.util.test.GrUnitTestSuite. Unfortunately IDE knows nothing about our generated test classes, so we need to do a little bit of work to create run configuration for our tests. Easiest way to do it is to specify groovy.util.test.GrUnitTestSuite as test class and provide VM parameter -Dgroovy.test.dir="out/production/Examples" which instruct where to find compiled test classes

Another important thing to notice: compiled classes for inline tests will be placed together with compiled sources. So after compilation You might decide to move everyting related to inline tests to class directory of test. Any build system normally capable to do that. File mask "*$GrUnitTest*.class" will do the job

GrUnit is very young tool but we plan to use it actively in Groovy++. I can also imagine that if proved to be useful at some point it can be migrated in to Groovy Core

Thank you for reading and till next time

unit test Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Configure Git in Eclipse IDE
  • What Is URL Rewriting? | Java Servlets
  • Migrating From Heroku To Render
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo