jparams / to-string-verifier

To String Verifier provides an easy and convenient way to test the toString method on your class.
MIT License
33 stars 6 forks source link

NullPointerException when class has field of type SortedSet #15

Closed Riggs333 closed 6 years ago

Riggs333 commented 6 years ago

To String Verifier Version: 1.4.2

java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Given a class with field of type java.util.SortedSet

import com.google.common.base.MoreObjects;

import java.util.SortedSet;
import java.util.TreeSet;

public class SortedSetContainer {

    private SortedSet<String> elements = new TreeSet<>();

    public void setElements(SortedSet<String> elements) {
        this.elements = elements;
    }

    public SortedSet<String> getElements() {
        return elements;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("elements", elements)
                .toString();
    }
}

When running To String Verifier

@Test
public void sortedSet() {
    ToStringVerifier.forClass(SortedSetContainer.class).verify();
}

A NullPointerException is thrown

java.lang.NullPointerException
    at com.jparams.verifier.tostring.ToStringVerifier.getFieldValues(ToStringVerifier.java:456)
    at com.jparams.verifier.tostring.ToStringVerifier.verifyField(ToStringVerifier.java:410)
    at com.jparams.verifier.tostring.ToStringVerifier.lambda$verify$5(ToStringVerifier.java:370)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.jparams.verifier.tostring.ToStringVerifier.verify(ToStringVerifier.java:373)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:479)
    at com.jparams.verifier.tostring.ToStringVerifier.verify(ToStringVerifier.java:336)

Interestingly when changing the type of the field to java.util.Set then the test passes.

jparams commented 6 years ago

Thanks for raising this issue. This is actually an issue with my object-builder project which generates instances for given classes. I will fix the issue in the next few days

jparams commented 6 years ago

This should now be fixed in v1.4.3. I have just released it so should sync into maven central soon.

Riggs333 commented 6 years ago

Super fast! Thank you - it works! :-) :+1: