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

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

Open
GirirajSinghRathore opened this issue Oct 15, 2024 · 2 comments

Comments

@GirirajSinghRathore
Copy link

GirirajSinghRathore commented Oct 15, 2024

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();
    }
}
  • JUnit version: 5.10.0
  • Java version: 17
  • JavaBeanTester version: 2.8.3
@GirirajSinghRathore GirirajSinghRathore changed the title NullPointerException in JavaBeanTester: “Cannot invoke Object.getClass()” when testing POJO with SLF4J warning NullPointerException in JavaBeanTester: “Cannot invoke Object.getClass()” when testing POJO Oct 15, 2024
@GirirajSinghRathore
Copy link
Author

@hazendaz can you please check

@hazendaz
Copy link
Owner

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.

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