GithubHelp home page GithubHelp logo

kobee1203 / to-string-verifier Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jparams/to-string-verifier

0.0 0.0 0.0 223 KB

To String Verifier provides an easy and convenient way to test the toString method on your class.

License: MIT License

Java 100.00%

to-string-verifier's Introduction

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.

to-string-verifier's People

Contributors

jparams avatar dependabot-preview[bot] avatar riggs333 avatar thunderforge avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.