Categories
Java

How to rapidly test alternative ideas in Java during development

Problem

Java is a compiled language and in any non-trivial project you use multiple libraries which are neatly assembled for you when you run the application with your build system like gradle. This makes it hard and slow to test alternative ideas as you have to compile and run the project each time which is inconvenient and slow even in the best configuration.

Solution

Groovy is an interpreted language (can be compiled too for performance) and is a super-set of Java. It is ideally suited for testing alternative approaches while development. However, how do you include your runtime classpath so that it can access all your classes as well as third party libraries that you use?

There is a simple solution. You can add a task to your gradle build file. Here is a sample build file (build.gradle):

plugins {
    ...
    id 'groovy'
}
mainClassName = 'com.taragana.App'
dependencies {
    ...
    compile 'org.codehaus.groovy:groovy-all:2.4.14'
}   
repositories {
    jcenter()
}
task(console, dependsOn: 'classes', type: JavaExec) {
   main = 'groovy.ui.Console'
   classpath = sourceSets.main.runtimeClasspath
}

This build file contains the task console which can be run with:

gradle console

In the console you can import and access any of your existing class files as well as third party libraries that you included in the application.

Value Addition

This is also an instant test environment that you can use to test your tests before codifying them in test classes. This saves tremendous amount of time not only in coding but also in developing test cases.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.