Testing
Gradle has built-in support for running tests for our Java projects. When we add the Java plugin to our project, we get new tasks to compile and run tests. We also get the dependency configurations testCompile
and testRuntime
. We use these dependencies to set the classpath for running the tests in our code base.
Let's write a simple JUnit test for a sample Java class. The implementation of gradle.sample.Sample
has the method
getWelcomeMessage()
, where we read a text from a property file and then return the value. The following example contains the code for the Sample
class:
// File: src/main/java/gradle/sample/Sample.java package gradle.sample; import java.util.ResourceBundle; /** * Read welcome message from external properties file * <code>messages.properties</code>. */ public class Sample { public Sample() { } /** * Get <code>messages.properties</code> file and read * value for <em>welcome</em> key. * ...