Top Related Projects
Java (and original) version of Hamcrest
✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
AssertJ is a library providing easy to use rich typed assertions
Most popular Mocking framework for unit tests written in Java
TestNG testing framework
Quick Overview
Truth is an open-source assertion library for Java and Android, developed by Google. It provides a fluent API for writing test assertions, making tests more readable and easier to maintain. Truth aims to improve upon traditional assertion libraries by offering clearer error messages and a more intuitive syntax.
Pros
- Fluent, readable syntax for writing assertions
- Detailed and informative failure messages
- Supports custom types and extensions
- Integrates well with popular testing frameworks like JUnit
Cons
- Learning curve for developers accustomed to traditional assertion libraries
- May require additional setup compared to built-in JUnit assertions
- Limited adoption compared to more established libraries like AssertJ
Code Examples
- Basic assertion:
import static com.google.common.truth.Truth.assertThat;
assertThat(actualValue).isEqualTo(expectedValue);
- Collection assertion:
import static com.google.common.truth.Truth.assertThat;
List<String> actual = Arrays.asList("apple", "banana", "cherry");
assertThat(actual).containsExactly("apple", "banana", "cherry").inOrder();
- Custom type assertion:
import static com.google.common.truth.Truth.assertAbout;
public static Subject.Factory<PersonSubject, Person> people() {
return PersonSubject::new;
}
assertAbout(people()).that(actualPerson).hasName("John Doe").hasAge(30);
Getting Started
To use Truth in your Java project, add the following dependency to your Maven pom.xml
:
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
For Gradle, add this to your build.gradle
:
testImplementation 'com.google.truth:truth:1.1.3'
Then, in your test classes, import the necessary static methods:
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertAbout;
Now you can start writing assertions using Truth's fluent API in your test methods.
Competitor Comparisons
Java (and original) version of Hamcrest
Pros of JavaHamcrest
- More mature and widely adopted in the Java ecosystem
- Extensive library of matchers for various data types and scenarios
- Highly extensible, allowing custom matcher creation
Cons of JavaHamcrest
- More verbose syntax compared to Truth
- Less fluent API for chaining assertions
- Steeper learning curve for creating custom matchers
Code Comparison
JavaHamcrest:
assertThat(actualValue, is(equalTo(expectedValue)));
assertThat(list, hasItems("item1", "item2"));
Truth:
assertThat(actualValue).isEqualTo(expectedValue);
assertThat(list).containsAtLeast("item1", "item2");
Key Differences
- Syntax: Truth offers a more fluent and readable syntax
- Extensibility: JavaHamcrest provides more built-in matchers and easier custom matcher creation
- Error messages: Truth generally produces more descriptive and helpful error messages
- Learning curve: Truth is often considered easier for beginners to pick up and use effectively
Use Cases
- JavaHamcrest: Projects with complex custom matching requirements or those deeply integrated with JUnit
- Truth: Projects prioritizing readability and simplicity in assertions, especially for teams new to testing
Both libraries are powerful tools for writing expressive and maintainable tests in Java, with the choice often coming down to personal or team preference.
✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
Pros of JUnit 5
- More comprehensive testing framework with extensive features
- Better support for parameterized tests and dynamic test generation
- Wider adoption and community support in the Java ecosystem
Cons of JUnit 5
- Steeper learning curve due to more complex API
- Requires more setup and configuration for advanced features
- Slightly more verbose assertion syntax compared to Truth
Code Comparison
JUnit 5 assertion:
assertEquals(expected, actual, "Optional failure message");
assertTrue(condition, "Optional failure message");
Truth assertion:
assertThat(actual).isEqualTo(expected);
assertThat(condition).isTrue();
JUnit 5 offers a more traditional assertion style, while Truth provides a more fluent and readable syntax. Truth's assertions are generally more concise and self-explanatory, which can lead to more readable test code. However, JUnit 5's assertions offer more flexibility in terms of custom failure messages and assertion types.
Both libraries have their strengths, and the choice between them often depends on personal preference and project requirements. JUnit 5 is a more comprehensive testing framework, while Truth focuses specifically on providing a user-friendly assertion library.
AssertJ is a library providing easy to use rich typed assertions
Pros of AssertJ
- More extensive set of assertions and matchers
- Better support for Java 8+ features (e.g., lambdas, streams)
- More frequent updates and active community support
Cons of AssertJ
- Steeper learning curve due to larger API surface
- Slightly more verbose syntax in some cases
- Potentially slower execution time for complex assertions
Code Comparison
Truth:
assertThat(actual).isEqualTo(expected);
assertThat(list).containsExactly(1, 2, 3);
AssertJ:
assertThat(actual).isEqualTo(expected);
assertThat(list).containsExactly(1, 2, 3);
Key Differences
- Syntax: Both libraries have similar syntax for basic assertions
- Flexibility: AssertJ offers more specialized assertions for different types
- Integration: Truth integrates well with other Google libraries, while AssertJ works seamlessly with various testing frameworks
Use Cases
- Truth: Ideal for projects using other Google libraries or preferring a simpler API
- AssertJ: Better suited for complex assertions and projects leveraging Java 8+ features
Community and Support
- Truth: Backed by Google, with a focus on simplicity and readability
- AssertJ: Large community-driven project with frequent updates and extensive documentation
Most popular Mocking framework for unit tests written in Java
Pros of Mockito
- More versatile for mocking and stubbing in unit tests
- Larger community and ecosystem with extensive documentation
- Supports both Java and Kotlin natively
Cons of Mockito
- Syntax can be more verbose compared to Truth
- Learning curve might be steeper for beginners
- Less focus on readability in test assertions
Code Comparison
Truth:
assertThat(result).isEqualTo(expectedValue);
assertThat(list).containsExactly("a", "b", "c");
Mockito:
when(mockObject.someMethod()).thenReturn(expectedValue);
verify(mockObject, times(1)).someMethod();
Summary
Mockito excels in creating mock objects and verifying interactions, making it ideal for complex unit testing scenarios. Truth, on the other hand, focuses on providing clear and readable assertions. While Mockito offers more flexibility in mocking, Truth's syntax is generally more concise and easier to understand at a glance. The choice between the two often depends on the specific testing needs of the project and the team's preferences.
TestNG testing framework
Pros of TestNG
- More comprehensive test framework with support for parallel execution and test grouping
- Built-in support for data-driven testing and parameterization
- Extensive reporting capabilities and integration with various tools
Cons of TestNG
- Steeper learning curve due to more complex configuration and annotations
- Less focus on fluent, readable assertions compared to Truth
- Potentially more verbose test code for simple scenarios
Code Comparison
TestNG assertion:
@Test
public void testAddition() {
int result = calculator.add(2, 3);
Assert.assertEquals(result, 5);
}
Truth assertion:
@Test
public void testAddition() {
int result = calculator.add(2, 3);
assertThat(result).isEqualTo(5);
}
Summary
TestNG is a full-featured testing framework offering a wide range of capabilities for complex testing scenarios. It excels in managing large test suites and provides powerful tools for test organization and execution. However, it may be overkill for simpler projects and can require more setup and configuration.
Truth, on the other hand, focuses primarily on providing clear and readable assertions. It offers a more intuitive API for writing test assertions, which can lead to more maintainable test code. While it lacks some of the advanced features of TestNG, it can be easily integrated with other testing frameworks and is often preferred for its simplicity and expressiveness in assertion writing.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
What is Truth?
Truth makes your test assertions and failure messages more readable. Similar to AssertJ, it natively supports many JDK and Guava types, and it is extensible to others.
Truth is owned and maintained by the Guava team. It is used in the majority of the tests in Googleâs own codebase.
Read more at the main website.
Top Related Projects
Java (and original) version of Hamcrest
✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
AssertJ is a library providing easy to use rich typed assertions
Most popular Mocking framework for unit tests written in Java
TestNG testing framework
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot