Relative Content

Tag Archive for javaspring-bootreflectionautowired

Is it possible to use Reflection along with @Autowired annotation to access to fields of a private class?

I am writing tests for a project and I want to access to private field of a class that otherwise should not be accessed. The reason behind this is that I don’t want to fill the database with meaningless test datas so I am creating the test data within the code. Since there shouldn’t be any manual insertions to the list using the code I need to use reflection to achieve it. I am using @Autowired annotation to create an instance of the class. I am also trying to use reflection to get the private field of this said instance. Usually I expect it to get the required field but it always returns null. When I debugged the test I saw that all fields of the injected object appear as null.