🚧 Gradle 6 - Test Fixtures
This commit is contained in:
parent
0c2d312186
commit
99e94be7d0
|
@ -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()
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.gradle.parallel=true
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue