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

Validator with ToStringTester failed #147

Open
saidRaiss opened this issue Jan 31, 2023 · 1 comment
Open

Validator with ToStringTester failed #147

saidRaiss opened this issue Jan 31, 2023 · 1 comment

Comments

@saidRaiss
Copy link

I wish to test an override toString method :

    @Test
    void test() {
        PojoClass pojoClass = PojoClassFactory.getPojoClass(Person.class);
        Validator validator = ValidatorBuilder.create().with(new ToStringTester()).build();
        validator.validate(pojoClass);
    }

    public static class Person {
        private String name;
        public Person(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "Person[name: "  + name + "]";
        }
    }

I got an assertion error:

Expected string mismatch expected <zxF> but was <Person[name: OxoL]>
java.lang.AssertionError: Expected string mismatch expected <zxF> but was <Person[name: OxoL]>
	at com.openpojo.validation.affirm.JavaAssertionAffirmation.fail(JavaAssertionAffirmation.java:33)
	at com.openpojo.validation.affirm.JavaAssertionAffirmation.affirmEquals(JavaAssertionAffirmation.java:72)
	at com.openpojo.validation.affirm.Affirm.affirmEquals(Affirm.java:75)
	at com.openpojo.validation.test.impl.ToStringTester.run(ToStringTester.java:41)
	at com.openpojo.validation.utils.ValidationHelper.runValidation(ValidationHelper.java:102)
	at com.openpojo.validation.impl.DefaultValidator.validate(DefaultValidator.java:46)

I thing this is a generic error. On ToStringTester class the test compares instance to string with a random text which will be every time false.

public class ToStringTester implements Tester {

  public void run(PojoClass pojoClass) {
    Object instance = RandomFactory.getRandomValue(pojoClass.getClazz());

    IdentityHandlerStub identityHandlerStub = new IdentityHandlerStub(instance);
    identityHandlerStub.setToStringReturn(RandomFactory.getRandomValue(String.class));

    IdentityFactory.registerIdentityHandler(identityHandlerStub);

    Affirm.affirmEquals("Expected string mismatch", identityHandlerStub.getToStringReturn(), instance.toString());
  }
}
@sysmat
Copy link

sysmat commented Apr 15, 2024

yes this generic from lib is not good

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