Simple but powerful BDD framework for JUnit tests
‘cola’ - English, noun - Carbonated soft drink
‘cola’ - Portuguese, noun - Glue or adhesive
COLA Tests are different from all other BDD framework as it allows for developers to keep using any JUnit Runner without having to do any complex configuration or setup!
Just add COLA Tests to your build environment and you are ready to fly!
Full Gherkin syntax support.
Why
But dude, what is BDD and why should I care?
BDD should be fun
Most BDD frameworks available for java either limit the test to their specific JUnitRunner or can be quite complex to setup.
COLA Tests bring some more than needed sparkling to BDD in JUnit tests. There is nothing particularly special about the COLA Tests framework except the way it actually works.
The process is the following:
- Create a Gherkin feature
- Create a JUnit POJO with annotated methods for @Given, @When and @Then
- Inject JUnit tests using colac (command line), cola-maven-plugin or cola-gradle-plugin
Example Gherkin Feature file
Feature: An example feature
Scenario: Should consume cucumbers
Given there are 10 cucumbers
When I eat 2 cucumbers
Then I should have 8 cucumbers
Example COLA JUnit Test
@Features("my-feature-file")
public class ColaTest {
private Integer value;
@Given("there are 10 cucumbers")
public void given() {
value = 10;
}
@When("I eat 2 cucumbers")
public void when() {
value -= 2;
}
@Then("I should have 8 cucumbers")
public void then() {
assertThat(value, equalTo(8));
}
}
setup details
Setup
COLA Tests can be compiled in the command line using the Cola Tests Compiler, in maven projects through the cola-maven-plugin and in gradle projects through cola-gradle-plugin.
Command Line Compiler
The COLA Tests command line compiler is available as an executable über-jar.
Download: colac latest
Don’t forget to add respective cola-tests library to the JUnit test classpath.
First check the cola-tests version:
#java -jar colac.jar --version
cola-tests-compiler 0.0.3
Then download the respective cola-tests version from Maven Central Repository and add it to your junit tests class path.
Maven
The material presented here is purely introductory. Please refer to maven plugin documentation for more information.
- Add cola-tests as a dependency to the maven pom.xml
<dependency>
<groupId>com.github.bmsantos</groupId>
<artifactId>cola-tests</artifactId>
<version>0.0.3</version>
</dependency>
- Add cola-maven-plugin as a maven plugin to the maven pom.xml
<plugin>
<groupId>com.github.bmsantos</groupId>
<artifactId>cola-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<ideBaseClass>com.github.bmsantos.maven.cola.BaseColaTest</ideBaseClass>
<ideBaseClassTest>iWillBeRemoved</ideBaseClassTest>
<includes>
<include>**/*Test.class</include>
</includes>
<excludes>
<exclude>**/ExcludedTest.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>compile-cola-tests</id>
<phase>process-test-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The following configuration options are required if running the tests from an IDE:
-
ideBaseClass : Common base class to all tests
-
ideBaseClassTest : The name of the JUnit test method to be removed.
This is required because IDEs requires the presence of a JUnit test method in order to execute the test class.
Gradle
- Add build dependencies cola-tests and cola-gradle-plugin to the project build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.bmsantos:cola-tests:0.0.3'
classpath 'com.github.bmsantos:cola-gradle-plugin:0.0.3'
}
}
- Add test dependency cola-tests and optional slf4j bridge to the project build.gradle
dependencies {
// testCompile 'org.slf4j:slf4j-simple:1.7.7' // Optional - Can use other slf4j bridge.
testCompile 'com.github.bmsantos:cola-tests:0.0.3'
testCompile 'junit:junit:4.+'
}
- Configure Cola Tests in project build.gradle
apply plugin: 'cola'
cola {
ideBaseClass = 'org.gradle.cola.tests.TestBase'
ideBaseClassTest = 'toBeRemoved'
targetDirectory = compileTestJava.destinationDir
includes = ['org/gradle/cola/tests/**']
excludes = ['something/else/**', 'and/some/CompiledFile.class']
}
- Hook Cola Test Compiler task to the project build.gradle. Hooks have to be placed after compiling test classes and before executing them. The following example is based on Gradle Java Plugin testClasses task.
colac.dependsOn testClasses
colac.mustRunAfter testClasses
- Configure JUnit for tests and make test task depend on cola-compiler (colac) task
test {
useJUnit()
dependsOn colac
}
Example Projects
Here are a couple of example configurations for:
- Java: maven-simple, maven-SpringJUnit4ClassRunner, gradle-simple
- Kotlin: maven-simple
usage
Maven Goals
- cola:compile : Compile COLA Tests
The goal follows surefire and failsafe JVM matching properties, test and it.test. When set, the goal will only compile the matching COLA Test classes.
#mvn cola:compile -Dtest=com/path/**/*Test
#mvn cola:compile -Dit.test=com/path/**/*Test
Gradle Tasks
- colac : Compile COLA Tests
The task follows Gradle Java plugin JVM matching property, test.single.
#gradle colac -Dsingle.test=com/path/**/*Test
Command Line
#java -jar colac.jar --help
Usage: java -jar /path/to/cola-tests.jar [options]
Options:
-h, --help
Print this guide
Default: false
-b, --ideBaseClass
IDE base test class if required
-m, --ideBaseClassTest
IDE base test class method to be removed
* -t, --target
Base directory containing compiled java packages and classes (required)
-v, --version
Print out version information
Default: false
updating
Updating
-
Final distributions will be available through Sonatype’s Maven Central Repository.
-
Snapshots releases will be available through Sontaype’s Snapshot server. If using a snapshot version please add the following repo to maven settings.xml of project pom.xml.
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
Or in gradle build.gradle file
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
mavenCentral()
}
ide support
Eclipse (Luna and above)
Maven support throgh m2e plugin
A COLA Tests m2e Connector is also available for a perfect Eclipse experience. The connector provides a JIT like compilation for the modified COLA tests.
Eclipse Luna (4.4) or above is required for the following m2e connector.
Instalation
To install it, add the following installation sites.
Current: 0.0.1
https://github.com/bmsantos/m2eclipse-cola/raw/master/com.github.bmsantos.m2e.cola.p2update/v0.0.1/
Latest: 0.0.2-SNAPSHOT (not for production)
https://github.com/bmsantos/m2eclipse-cola/raw/master/com.github.bmsantos.m2e.cola.p2update/latest/
Please check section Setup Details above for further Eclipse configuration details.
Gradle
TBD
IntelliJ IDEA
Maven
Enable maven cola:compile goal after make and rebuild:
- Select Maven Projects -> Plugins -> cola
- Right click on cola:compile and choose Execute After Make
- Right click on cola:compile and choose Execute After Rebuild
Maven configuration properties ideBaseClass and ideBaseClassTest are also required if not using defaults.
Gradle
TBD