Gradle JUnit HelloWorld example
Gradle supports both Java and Groovy projects. When build Groovy project , the best test framework is Spock, when work with Java project, the test framework is JUnit. Even the Spock is actually compatible with JUnit, like Gradle compatible with Maven.
Step 1. Create directory structure
For a typical Java project, the directory layout would be like this
src\main src\main\java\ src\test src\test\java
Step 2. Create java class and test class
Add a java class under src\main\java\HelloWorld.java
class HelloWorld { public String Hello ( String str ) { System.out.println("Hello" + str ); return str; } }
And the JUnit test class
import org.junit.Test; import static org.junit.Assert.*; public class RunTest { @Test public void RunTest() { HelloWorld h = new HelloWorld(); String str = "World"; assertEquals(str, h.Hello(str)); } }
Step 3. Gradle build file
The build file is simple
apply plugin: 'java' repositories { mavenCentral() } dependencies { testCompile 'junit:junit:4.8.2' }
The build process will execute all the tests in test directory. Run the build command
gradle build
Gradle
Gradle Programming