GithubHelp home page GithubHelp logo

Comments (1)

sta-szek avatar sta-szek commented on June 3, 2024

Getting pl.pojo.tester.internal.assertion.equals.NotEqualEqualsAssertionError is caused by ObjectGenerator's method generateDifferentObjects
It creates new object of field type class and sets to baseObjectCopy without checking if values are equal so solution for this problem is to put tihs code int 72 line:

Object newFieldTypeInstance = createNewInstance(permutationFieldType);
    try {
        if (newFieldTypeInstance.equals(FieldUtils.getValue(baseObject, permutationField))) {
            newFieldTypeInstance = abstractFieldValueChanger.increaseValue(newFieldTypeInstance);
        }
    } catch (final IllegalAccessException e) {
        // TODO
        throw new RuntimeException("Make FieldUtils.getValue(...,...) throw runtime exception");
}

but this makes another problem.
When creating proxy object (using java proxy or javaasist proxy) we need to pass InvocationHandler or MethodInvocationHandler.

This is implemented to return 0:

return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (proxy, method, args) -> 0);

So when equals method is invoked, we get CastClassException. Naturally, we cant cast 0 to boolean so at first glance we can replace return statement with

return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (proxy, method, args) -> method.invoke(proxy,args));

but this makes sweet Java throw java.lang.reflect.UndeclaredThrowableException
.
.
.
Can you imagine life without exceptions?
Just imagine...

And finally great sollution for this is:

 private Object proxyByJava() {
        return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, this::createInvocationHandler);
    }

    private Object createInvocationHandler(final Object proxy, final Method method, final Object[] args) {
        try {
            return method.invoke(proxy, args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            final Class<?> returnType = method.getReturnType();
            if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) {
                return true;
            } else {
                return 0;
            }
        }
    }

😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆

from pojo-tester.

Related Issues (20)

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.