https://github.com/OpenHFT/Chronicle-Test-Framework
https://github.com/OpenHFT/Chronicle-Test-Framework
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/OpenHFT/Chronicle-Test-Framework
- Owner: OpenHFT
- License: apache-2.0
- Created: 2021-10-27T13:02:33.000Z (over 3 years ago)
- Default Branch: ea
- Last Pushed: 2024-04-12T08:36:45.000Z (about 1 year ago)
- Last Synced: 2024-04-12T15:34:00.360Z (about 1 year ago)
- Language: Java
- Size: 301 KB
- Stars: 23
- Watchers: 12
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
Awesome Lists containing this project
- awesome-java - Chronicle Test Framework
README
= Chronicle Test Framework
:css-signature: demo
:toc: macro
:toclevels: 2
:icons: fontimage:https://maven-badges.herokuapp.com/maven-central/net.openhft/chronicle-test-framework/badge.svg[caption="",link=https://maven-badges.herokuapp.com/maven-central/net.openhft/chronicle-test-framework]
image:https://javadoc.io/badge2/net.openhft/chronicle-test-framework/javadoc.svg[link="https://www.javadoc.io/doc/net.openhft/chronicle-test-framework/latest/index.html"]
//image:https://javadoc-badge.appspot.com/net.openhft/chronicle-test-framework.svg?label=javadoc[JavaDoc, link=https://www.javadoc.io/doc/net.openhft/chronicle-test-framework]
image:https://img.shields.io/github/license/OpenHFT/Chronicle-Test-Framework[GitHub]
image:https://img.shields.io/badge/release%20notes-subscribe-brightgreen[link="https://chronicle.software/release-notes/"]
image:https://sonarcloud.io/api/project_badges/measure?project=OpenHFT_ChronicleTestFramework&metric=alert_status[link="https://sonarcloud.io/dashboard?id=OpenHFT_Chronicle-Test-Framework"]toc::[]
== About
This library provides support classes for writing JUnit tests. It supports both Junit4 and JUnit5.
== Permutations
Permutaions work like this:
[source, java]
----
@Test
void print() {
Permutation.of("A", "B", "C")
.forEach(System.out::println);
}
----This will produce the following output:
[source, text]
----
[A, B, C]
[A, C, B]
[B, A, C]
[B, C, A]
[C, A, B]
[C, B, A]
----Testing using permutations:
[source, java]
----
@TestFactory
// Exhaustively tests that the order of setter invocation does not matter
Stream demo() {
// Operations
final Consumer setA = myBean -> myBean.setA(1);
final Consumer setB = myBean -> myBean.setB(2);
final Consumer setC = myBean -> myBean.setC(3);
final MyBean expected = new MyBean(1, 2, 3);
// DynamicTests
return DynamicTest.stream(Permutation.of(setA, setB, setC),
Objects::toString,
operations -> {
final MyBean actual = new MyBean();
operations.forEach(oper -> oper.accept(actual));
assertEquals(expected, actual);
});
}
----== Combinations
Combinations work like this:
[source, java]
----
@Test
void print() {
Combination.of("A", "B", "C")
.forEach(System.out::println);
}
----This will produce the following output:
[source, text]
----
[]
[A]
[B]
[C]
[A, B]
[A, C]
[B, C]
[A, B, C]
----Testing using combinations:
[source, java]
----
@TestFactory
// Try all combinations of cosmic ray interference
@TestFactory
// Try all combinations of the Robot state machine
Stream demo() {
return DynamicTest.stream(Combination.>of(
FaultTolerantBitSet::cosmicRayBit3,
FaultTolerantBitSet::cosmicRayBit23,
FaultTolerantBitSet::cosmicRayBit13),
Objects::toString,
operations -> {
final FaultTolerantBitSet bitSet = new FaultTolerantBitSet();
operations.forEach(oper -> oper.accept(bitSet));
assertTrue(bitSet.isValid());
});
}
----== Combinations and Permutations
Combinations and Permutaions can be combined to create powerful exhaustive test vectors:
[source, java]
----
@Test
void demo() {
Combination.of("A", "B", "C")
.flatMap(Permutation::of)
.forEach(System.out::println);
}
----This will produce the following output:
[source, text]
----
[A]
[B]
[C]
[A, B]
[B, A]
[A, C]
[C, A]
[B, C]
[C, B]
[A, B, C]
[A, C, B]
[B, A, C]
[B, C, A]
[C, A, B]
[C, B, A]
----== Products
Products are equivalent to nested loops but are easier to convert to a `Stream` of `DynamicTest` objects:
[source, java]
----
@Test
void print() {
Product.of(Arrays.asList("A", "B", "C"), Arrays.asList(1, 2, 3))
.forEach(System.out::println);
}
----This will produce the following output:
[source, text]
----
Product2Impl{first=A, second=1}
Product2Impl{first=A, second=2}
Product2Impl{first=A, second=3}
Product2Impl{first=B, second=1}
Product2Impl{first=B, second=2}
Product2Impl{first=B, second=3}
Product2Impl{first=C, second=1}
Product2Impl{first=C, second=2}
Product2Impl{first=C, second=3}
----Products can use built-in tuples like `Product2Impl` or we can provide custom constructors to use our own.
Testing using combinations:
[source, java]
----
@TestFactory
// Exhaustively tests if various empty collections invariants holds
Stream demo() {
// Operations
final List> collections = Arrays.asList(new LinkedList<>(), new ArrayList<>(), new HashSet<>());
// Operations
final Consumer> empty =
c -> assertTrue(c.isEmpty(), c.getClass() + ".empty() was false");
final Consumer> size =
c -> assertEquals(0, c.size(), c.getClass() + ".size() != 0");
final Consumer> streamCount =
c -> assertEquals(0, c.stream().count(), c.getClass() + ".stream().count() != 0");
final List>> operations = Arrays.asList(empty, size, streamCount);// DynamicTests
return DynamicTest.stream(Product.of(collections, operations),
Objects::toString,
tuple -> {
tuple.second().accept(tuple.first());
});
}
----