to-string-tester

To String Tester

License

License

GroupId

GroupId

com.jparams
ArtifactId

ArtifactId

to-string-tester
Last Version

Last Version

1.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

to-string-tester
To String Tester
Project URL

Project URL

https://github.com/jparams/to-string-tester
Project Organization

Project Organization

JParams
Source Code Management

Source Code Management

https://github.com/jparams/to-string-tester

Download to-string-tester

How to add to project

<!-- https://jarcasting.com/artifacts/com.jparams/to-string-tester/ -->
<dependency>
    <groupId>com.jparams</groupId>
    <artifactId>to-string-tester</artifactId>
    <version>1.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.jparams/to-string-tester/
implementation 'com.jparams:to-string-tester:1.1.2'
// https://jarcasting.com/artifacts/com.jparams/to-string-tester/
implementation ("com.jparams:to-string-tester:1.1.2")
'com.jparams:to-string-tester:jar:1.1.2'
<dependency org="com.jparams" name="to-string-tester" rev="1.1.2">
  <artifact name="to-string-tester" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jparams', module='to-string-tester', version='1.1.2')
)
libraryDependencies += "com.jparams" % "to-string-tester" % "1.1.2"
[com.jparams/to-string-tester "1.1.2"]

Dependencies

compile (1)

Group / Artifact Type Version
com.jparams : object-builder jar 1.0.6

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 2.6.0
eu.codearte.catch-exception : catch-exception jar 1.4.4
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

To String Verifier

Maven Central Build Status Javadocs

Getting Started

Get To String Verifier

Maven:

<dependency>
    <groupId>com.jparams</groupId>
    <artifactId>to-string-verifier</artifactId>
    <version>1.x.x</version>
    <scope>test</scope>
</dependency>

Gradle:

testImplementation 'com.jparams:to-string-verifier:1.x.x'

What is To String Verifier?

To String Verifier is the easiest, most convenient way to test the toString method on your class. toString methods are incredibly important for logging and debugging purposes, yet when it comes to testing, it is often forgotten or ignored. It is very easy to add a new field to your class and forget to update the toString method. Without tests in place, you won’t know something is missing until you are sitting looking through logs or debugging your application and by that time its already too late.

Write a Test

Writing a test is easy! In most cases you want to ensure that your toString method contains the class name and all the fields. Let's look at how we would write this test!

@Test
public void testToString()
{
    ToStringVerifier.forClass(User.class)
                    .withClassName(NameStyle.SIMPLE_NAME)
                    .verify();
}

Oh! A user object. I would not want to include the password in the toString, how do I exclude it from my test?

@Test
public void testToString()
{
    ToStringVerifier.forClass(Person.class)
                    .withClassName(NameStyle.SIMPLE_NAME)
                    .withIgnoredFields("password")
                    .withFailOnExcludedFields(true) // with this set true, if a developer accidently adds the password to the toString(), the unit test will fail
                    .verify();
}

To String Verifier is incredibly configurable, so take a look at the Java Docs for more options.

Package Scan

Some times you want to test a number of classes at once, or maybe all classes in a package. Well, we have you covered!

Testing multiple classes is as easy as: ToStringVerifier.forClasses(Person.class, Animal.class, Tree.class).verify()

To scan an entire package:

@Test
public void testToString()
{
    boolean scanRecursively = true; // when set to true, this will scan the given package and all subpackages.
    Predicate<Class<?>> predicate = (clazz) -> clazz.getName().endsWith("Model"); // optional parameter
    ToStringVerifier.forPackage("my.most.awesome.package", scanRecursively, predicate).verify();
}

Presets

To String Verifier also comes with a number of Vendor presets. These presets describe how different vendors style and format their toString output. Just tell us the Vendor preset to apply and begin testing!

ToStringVerifier.forClass(Person.class).withPreset(Presets.ECLIPSE).verify()

Here are all the Vendor presets available out of the box:

  • IntelliJ default style
  • Eclipse default style
  • Guava's MoreObjects toStringHelper.
  • Apache's ToStringBuilder. Supported ToStringStyles include:
    • JSON_STYLE
    • NO_CLASS_NAME_STYLE
    • DEFAULT_STYLE
    • MULTI_LINE_STYLE
    • SHORT_PREFIX_STYLE

Compatibility

This library is compatible with Java 8 and above.

Versions

Version
1.1.2
1.1.1
1.0.4
1.0.3
1.0.2
1.0.1