Skip to content

Commit

Permalink
🚧 Gradle 6 - Test Fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
gilday committed Dec 20, 2019
1 parent 94be519 commit dd72ecd
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gradle-6/fibonacci-recursive/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
`java-library`
}

dependencies {
api(project(":fibonacci-spi"))
compileOnly("com.google.auto.service:auto-service-annotations:1.0-rc6")
annotationProcessor("com.google.auto.service:auto-service:1.0-rc6")

testImplementation(testFixtures(project(":fibonacci-spi")))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.5.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.5.2")
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.fibonacci.impl;

import com.baeldung.fibonacci.FibonacciSequenceGenerator;
import com.google.auto.service.AutoService;

/** Recursive implementation of the {@link FibonacciSequenceGenerator}. */
@AutoService(FibonacciSequenceGenerator.class)
public final class RecursiveFibonacci implements FibonacciSequenceGenerator {

@Override
public int generate(int nth) {
if (nth < 0) {
throw new IllegalArgumentException("sequence number must be 0 or greater");
}
if (nth <= 1) {
return nth;
}
return generate(nth - 1) + generate(nth - 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.fibonacci.impl;

import com.baeldung.fibonacci.FibonacciSequenceGenerator;
import com.baeldung.fibonacci.FibonacciSequenceGeneratorFixture;

/**
* Unit test which reuses the {@link FibonacciSequenceGeneratorFixture} test mix-in exported from
* the fibonacci-spi project.
*/
final class RecursiveFibonacciUnitTest implements FibonacciSequenceGeneratorFixture {

@Override
public FibonacciSequenceGenerator provide() {
return new RecursiveFibonacci();
}
}
13 changes: 13 additions & 0 deletions gradle-6/fibonacci-spi/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
`java-library`
`java-test-fixtures`
}

dependencies {
testFixturesApi("org.junit.jupiter:junit-jupiter-api:5.5.2")
testFixturesImplementation("org.junit.jupiter:junit-jupiter-engine:5.5.2")
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.fibonacci;

/** Describes an SPI for a Fibonacci sequence generator function. */
public interface FibonacciSequenceGenerator {

/**
* @param nth the index of the number in the fibonacci sequence
* @return the nth number in the fibonacci sequence
*/
int generate(int nth);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.fibonacci;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

/**
* Reusable test fixture for {@link FibonacciSequenceGenerator} implementations. Tests will be
* skipped if no such implementation exists.
*/
public interface FibonacciSequenceGeneratorFixture {

/** @return the implementation of {@link FibonacciSequenceGenerator} to test. Must not be null */
FibonacciSequenceGenerator provide();

@Test
default void when_sequence_index_is_negative_then_throws() {
final FibonacciSequenceGenerator generator = provide();
assertThrows(IllegalArgumentException.class, () -> generator.generate(-1));
}

@Test
default void when_given_index_then_generates_fibonacci_number() {
final FibonacciSequenceGenerator generator = provide();
final int[] sequence = {0, 1, 1, 2, 3, 5, 8};
for (int i = 0; i < sequence.length; i++) {
assertEquals(sequence[i], generator.generate(i));
}
}
}
1 change: 1 addition & 0 deletions gradle-6/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.parallel=true
2 changes: 2 additions & 0 deletions gradle-6/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
rootProject.name = "gradle-6"

include("dependency-constraints")
include("fibonacci-spi")
include("fibonacci-recursive")
include("httpclient-platform")
include("module-metadata-publishing")
include("person-rest-client")
Expand Down

0 comments on commit dd72ecd

Please sign in to comment.