Truth 0.30
New Subjects
- OptionalSubject (for java.util.Optional, accessible via
Truth8.assertThat(Optional<T>)
)
(see details under "new extensions" below)
Changes to existing Subjects and Core classes
-
Preliminary versions of "Fuzzy Equality" for doubles/floats. See
DoublesSubject
for
new APIs, and we'll be updating the docs on fuzzy equality soon.- Subject implementers should note changes to the Service Provider Interface (SPI) for writing
custom Subject subclasses:getSubject()
->actual()
getDisplaySubject()
->actualAsString()
- If you want to provide a subject-specific string representation of the type being tested, a new
methodactualCustomStringRepresentation()
can be overridden. The deprecated
getDisplaySubject()
andgetSubject()
methods should not be overridden (and will be
deleted in Truth 1.0. The renamed methods arefinal
.
actualAsString()
has logic that not only consumes the string representation (default or custom)
but also honors.named()
and other contextual formatting.
- Subject implementers should note changes to the Service Provider Interface (SPI) for writing
-
More evolution of "Fuzzy" truth - near-value approximation for certain types rather than strict
equality is now supported by way of a "correspondence" mechanism, and the capabilities are
being extended to many existing subjects.-
To use:
assertThat(actualIterable) .comparingElementsUsing(correspondence) .containsExactlyElementsIn(expectedIterable);
Currently supports
containsExactlyElementsIn(Iterable<E>)
,doesNotContain(E)
, and
contains(E)
methods. Subjects forMap
,Iterable
, and theMultimap
types all support
correspondence. For more detail, see the javadoc for IterableSubject.UsingCorrespondence and
Correspondence
-
-
.named()
now supports a varargs/format construction, such as:assertThat(someBooleanFunction()) .named("processed %s(%s)", foo, bar) .isTrue();
This results in a more suitable error message:
Not true that processed foo(bar) <false> is true
as
compared with the default message, and requires less string concatenation where that may prove
awkward
New Extensions
- Add an extension for Java8 types, initially containing a subject for
java.util.Optional
-
Include "com.google.truth.extensions:truth-java8-extension:0.30" in your build dependencies
-
To use:
import static com.google.common.truth.Truth8.assertThat; import java.util.Optional; public class MyTest { @Test public void testOptional() { Optional<String> o = Optional.of("Foo"); assertThat(o).isPresent(); //succeeds assertThat(o).hasValue("Foo"); //succeeds assertThat(o).isEmpty(); // fails } }
-
Fixes
- Fix a missing
@Nullable
in AbstractVerb.that(T) - Various cleanups of code, docs, readme, javadocs, and contribution