# truth0
**Repository Path**: mirrors_andyglick/truth0
## Basic Information
- **Project Name**: truth0
- **Description**: forked truth - broke out the gwt code as a separate maven module
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-09-24
- **Last Updated**: 2026-02-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Truth
=====
***We've made failure a strategy***
**Continuous Integration:** [](https://travis-ci.org/truth0/truth)
**Latest Release:** *0.13*
**Latest Artifact:** *org.junit.contrib:truth:jar:0.13*
**Note:** Truth is subject to change and prior to 1.0, may introduce
breaking changes. We're getting closer to "prime time" but please
use with caution before release 1.0. Consider Truth in alpha.
###***Table of Contents***
- [Introduction](#introduction)
- [Installation](#installation)
- [Using Truth](#using-truth)
- [Super basics](#super-basics)
- [Failure Strategy](#failure-strategy)
- [How does Truth work?](#how-does-truth-work)
- [Extensibility](#extensibility)
- [Categorically testing the contents of collections](#categorically-testing-the-contents-of-collections)
- [Some code examples](#some-code-examples)
- [Basic objects](#basic-objects)
- [Booleans](#booleans)
- [Numerics](#numerics)
- [Strings](#strings)
- [Iterables, Collections, Sets, and the like.](#iterables-collections-sets-and-the-like)
- [GWT](#gwt)
- [Planned improvements and changes](#planned-improvements-and-changes)
- [Acknowledgements](#acknowledgements)
Introduction
------------
Truth is a testing framework suitable for making assertions and assumptions
about code. Truth adopts a fluent style for your test propositions, is
extensible in several ways, supports IDE completion/discovery of available
propositions, and supports different responses to un-true propositions.
Truth can be used to declare assumptions (skip the test if they fail),
assertions (fail the test), and expectations (continue but report errors
and fail at the end).
While intended to work with JUnit, Truth can be used with other testing
framework with minimal effort. Truth is released as a maven artifact
through a custom repository (it will be released to repo1.maven.org soon),
and is licensed with the Apache 2.0 open-source license. As such, you are
free to use it or modify it subject only to the terms in that license.
Installation
------------
To prepare to use Truth, declare this dependency in maven or an equivalent:
| Strategy | Constant | Behaviour | Framework supported | Notes |
|---|---|---|---|---|
| Assertion | Truth.ASSERT | Aborts and fails test, reports failure | JUnit, TestNG, others (untested) | |
| Assumption | Truth.ASSUME | Aborts and ignores/skips test | JUnit | |
| Expectation | Expect.create() | Continues test, reports errors and failure upon test completion | JUnit | You must declare an @Rule per the ExpectTest |
contains("foo") will be invoked
on each element in the iterable in turn, reporting failure as if a
separate ASSERT.that(anElement).contains("foo") had been invoked for
each element. Naturally, ASSERT will fail on the first failing element,
ASSUME will skip the test on any failing element, and EXPECT will
gather all failures and report them at the end of the test.
This approach can be used for custom types too, so long as they have
a SubjectFactory
public static final SubjectFactoryASSERT.about(MY_TYPE).that()...
is available to the developer over iterables of that type.
### Some code examples
These are not exhaustive examples, but commonly used propositions. Please check out
the Javadocs for the Subject subclasses, or use IDE syntax completion proposals to
discover predicates for types you provide as subjects.
#### Basic objects
Equality is simply "is" in Truth.
ASSERT.that(this).is(that);
Type information is as usual:
ASSERT.that(this).isA(MyObject.class);
Often propositions have negative forms:
ASSERT.that(this).isNotA(String.class);
Nullness is checked simply with:
ASSERT.that(something).isNull();
ASSERT.that(somethingElse).isNotNull();
Fields' presence and their values can *(as of 0.8)* be checked with:
ASSERT.that(something).hasField("foo").withValue("bar");
This should work even with private fields, and can be useful in testing
generated properties created by some frameworks like Lombok and Tapestry.
#### Class objects
ASSERT.that(aClass).declaresField("foo");
+ Note, do not use hasField() on Class objects, as you will be
testing whether Class.class itself has that field, not whether the
type it represents declares that field. A deprecation warning should
notify you of this usage, but be careful, and use declaresField("foo")
instead.
#### Booleans
ASSERT.that(something).isTrue();
ASSERT.that(something).isFalse();
#### Numerics
ASSERT.that(5).isBetween(4, 5);
ASSERT.that(5).isExclusivelyInRange(4, 6);
#### Strings
ASSERT.that(aString).contains("blah");
ASSERT.that(aString).startsWith("foo");
ASSERT.that(aString).endsWith("bar");
#### Iterables, Collections, Sets, and the like.
##### Iterables
ASSERT.that(anIterable).isEmpty();
ASSERT.that(anIterable).iteratesOverSequence(a, b, c);
##### Collections
One can simply use object equality if you want to test collection
equivalence, given the guarantees of Collections' implementations of
.equals():
ASSERT.that(colectionA).is(collectionB);
Testing properties like size should be done like so:
ASSERT.that(collection.size()).is(5);
Or you can test that a specific item is present present:
ASSERT.that(collectionA).has().item(q);
Or you can test that all provided items are present:
ASSERT.that(collectionA).has().allOf(a, b, c);
Or you can be *even* more explicit and test that all ***and only*** the provided items are present:
ASSERT.that(collectionA).has().exactly(a, b, c, d);
optionally you can further constrain this:
ASSERT.that(collectionA).has().allOf(a, b, c).inOrder();
ASSERT.that(collectionA).has().exactly(a, b, c, d).inOrder();
Or you can assert using a (very) limited "or" logic with:
ASSERT.that(collectionA).has().anyOf(b, c);
You can also pass in collections as containers of expected results, like so:
ASSERT.that(collectionA).has().allFrom(collectionB);
ASSERT.that(collectionA).has().anyFrom(collectionB);
ASSERT.that(collectionA).has().exactlyAs(collectionB).inOrder();
##### Lists
Specific properties can be proposed on lists, such as:
ASSERT.that(myList).isOrdered(); // uses default ordering and is strict, no equal elements.
ASSERT.that(myList).isPartiallyOrdered(); // like isOrdered, but equal elements may be present.
And custom comparators can be provided
ASSERT.that(myList).isOrdered(aComparator);
ASSERT.that(myList).isPartiallyOrdered(aComparator);
##### Maps
Presence of keys, keys for values, or values can be asserted
ASSERT.that(map).hasKey("foo");
ASSERT.that(map).hasKey("foo").withValue("bar");
ASSERT.that(map).lacksKey("foo");
ASSERT.that(map).hasValue("bar");
Naturally, also:
ASSERT.that(map).isEmpty();
ASSERT.that(map).isNotEmpty();
Testing properties like size should be done like so:
ASSERT.that(map.size()).is(5);
GWT
---
A subset of Truth functionality is GWT compatible. Most propositions and subjects
that do not inherently use reflection or complex classloading are available. This
mostly excludes categorical testing of collection contents since that uses code
generation not supportable (as is) on GWT, as well as ClassSubjects, which largely
concerns itself with the internals of Java classes and testing them. Also, raw
field access is not supported, though this might be in the future if there is enough
of a use-case for it.
Planned improvements and changes
--------------------------------
* Subject wrappers for new types:
* New subjects for Float/Double and other currently missing types.
* Support for Annotations and methods and field availability on classes.
* Support for Protocol Buffers, JavaBeans (and other reflective types)
* Support for Guava collections and types (Multimaps, Multisets, etc.)
* New propositions on existing Subject wrappers:
* StringSubject, IntegerSubject, etc.
Acknowledgements
----------------
Thanks to Github and Cloudbees for having a strong commitment to open-source, and
providing us with tools so we can provide others with code. And thanks to Google
for [Guava](http://code.google.com/p/guava-libraries "Guava"), and for encouraging
us to try to solve problems in better ways and share that with the world.
Also thanks to the authors of JUnit, TestNG, Hamcrest, FEST, and others for creating
testing tools that let us write high-quality code, for inspiring this work and for
moving the ball forward in the field of automated software testing. This project
works with, works alongside, and sometimes works in competition with the above
tools, but owes a debt that everyone owes to those gone before. They paved the
way, and we hope this contribution is helpful to the field.