Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test class with 'simple' inner class #240

Open
boly38 opened this issue Feb 26, 2020 · 3 comments
Open

How to test class with 'simple' inner class #240

boly38 opened this issue Feb 26, 2020 · 3 comments

Comments

@boly38
Copy link

boly38 commented Feb 26, 2020

Hi

I would like to use PoJo tester 0.7.6 to test the following class:
(I'm using Lombok annotations too)

@Data
@Builder
@EqualsAndHashCode
public class Questionnaire {
    private String questionnaireId;
    private String id;
    private String label;
    private String text;
    private String comment;
    private List<String> tags;
    private Answer answer;

    @Data
    @Builder
    @EqualsAndHashCode
    public static class Answer {
        String type;
        List<String> options;
    }
}

I doubt that this class is no more a strict PoJo but I would like to use pojo tester if feasible.

So In my test I'm calling assertPojoMethodsFor(Questionnaire.class.getName()).areWellImplemented();

As result I've:

16:31:22.823 [main] DEBUG pl.pojo.tester.internal.field.AbstractFieldValueChanger - Could not change value 'Questionnaire.Answer(type=www.pojo.pl, options=[])' ot type class Questionnaire$Answer by any field value changer
16:31:22.823 [main] DEBUG pl.pojo.tester.internal.instantiator.ObjectGenerator - End of generating different objects (size=128) for base class Questionnaire(questionnaireId,id,label,text,comment,tags,answer) 

pl.pojo.tester.internal.assertion.equals.NotEqualEqualsAssertionError: 


Class Questionnaire has bad 'equals' method implementation.
The equals method should return false if objects should not be equal.
Current implementation returns true.
Object:
Questionnaire(questionnaireId=www.pojo.pl, id=www.pojo.pl, label=www.pojo.pl, text=www.pojo.pl, comment=www.pojo.pl, tags=[], answer=Questionnaire.Answer(type=www.pojo.pl, options=[]))
should not be equal to:
Questionnaire(questionnaireId=www.pojo.pl, id=www.pojo.pl, label=www.pojo.pl, text=www.pojo.pl, comment=www.pojo.pl, tags=[], answer=Questionnaire.Answer(type=www.pojo.pl, options=[]))

I think I do need to add a field value changer for my inner class
How can I do that ?

I would like to ask this on rocketChat or check the documentation but pojo.pl domain is unavailable at all.

Thanks
PS/ seems not exactly the same as #126

@boly38
Copy link
Author

boly38 commented Feb 26, 2020

(In fact dns is available, I just miss www. prefix. So I can access doc)

Seems related doc https://www.pojo.pl/writing-tests/#basic-test is not up-to-date ?

Here is what I've done:

// given
final ClassAndFieldPredicatePair baseClass = new ClassAndFieldPredicatePair(
   Questionnaire.class, FieldPredicate.includeAllFields(Questionnaire.class));
final ClassAndFieldPredicatePair fieldClasses = new ClassAndFieldPredicatePair(
   Answer.class, FieldPredicate.includeAllFields(Answer.class));

// when
// then
assertPojoMethodsFor(baseClass, fieldClasses).areWellImplemented();

no more Could not change value but same bad 'equals' method implementation result: any hint ?

@frecco75
Copy link
Collaborator

frecco75 commented Feb 26, 2020

In fact there is a bug I have identified concerning generation of objects when trying to test hashcode and equals methods. Sometimes it generates similar objects, with same hash code and equals, instead of generate objects with different attribute values.

A workaround is to use the quickly() method, which generates less objects (but which is not as powerful as default behavior)... or to specify explicitly the methods you want to test except hashcode and equals using the testing() method.

@boly38
Copy link
Author

boly38 commented Feb 27, 2020

well thanks,
while waiting for fix, I'll use this code:

        assertPojoMethodsFor(Questionnaire.class)
                .testing(Method.GETTER, Method.SETTER, Method.TO_STRING)
                // .testing(Method.EQUALS)
                // .testing(Method.HASH_CODE)
                .testing(Method.CONSTRUCTOR)
                .areWellImplemented();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants