hazendaz / javabean-tester

JavaBean Tester
http://codebox.org.uk/pages/articles/unit-testing-javabeans
Apache License 2.0
9 stars 5 forks source link

NullPointerException in JavaBeanTester: “Cannot invoke Object.getClass()” when testing POJO #963

Open GirirajSinghRathore opened 6 days ago

GirirajSinghRathore commented 6 days ago

I encountered a NullPointerException when using JavaBeanTester to test a POJO. The error occurs during the invocation of a method on a null object. Below is the full error message and steps to reproduce the issue.

Error Message:

java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "obj" is null

    at java.base/java.lang.reflect.Method.invoke(Method.java:561)
    at com.codebox.bean.JavaBeanTesterWorker.getterSetterTests(JavaBeanTesterWorker.java:219)
    at com.codebox.bean.JavaBeanTesterWorker.test(JavaBeanTesterWorker.java:157)
    at com.codebox.bean.JavaBeanTesterBuilder.test(JavaBeanTesterBuilder.java:204)
    at org.giriraj.pojo.TruckTest.testTruck(TruckTest.java:16)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Steps to Reproduce:

1.  Create a POJO class with Lombok annotations:
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class Truck {
    String wheels;
    String color;
    String model;
    String make;
}
2.  Write a JUnit test class to test the POJO using JavaBeanTester:
import com.codebox.bean.JavaBeanTester;
import org.giriraj.pojo.Truck;
import org.junit.jupiter.api.Test;

public class TruckTest {

    @Test
    public void testTruckBean() {
        JavaBeanTester.builder(Truck.class)
                .checkEquals()
                .checkConstructor()
                .checkClear()
                .loadData()
                .test();
    }
}
GirirajSinghRathore commented 6 days ago

@hazendaz can you please check

hazendaz commented 5 days ago

What is the delomboked code look like? As a pojo, it looks fine. Due to lombok usage, I don't know what its setting exactly. Would need to see that. If you write a unit test using that inside the project tests, I'm sure you can debug and figure it out and produced a pull request contribution. The tests are heavy on lombok and it works without issue in 1000s of projects I run it at work on latest between many jdks. So it has to do something with lombok there.