Using ReflectionTestUtils
The org.springframework.test.util
package contains ReflectionTestUtils
, which is a collection of reflection-based utility methods to set a non-public field or invoke a private/protected setter method when testing the application code, as follows:
- ORM frameworks, such as JPA and Hibernate, condone private or protected field access as opposed to public setter methods for properties in a domain entity
- Spring's support for annotations such as
@Autowired
,@Inject
, and@Resource
, which provide dependency injections for private or protected fields, setter methods, and configuration methods
The following example demonstrates the capabilities of ReflectionUtils
:
- Create a
Secret
class in thecom.packt.testutils
package with a private ...
Get Mockito for Spring now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.