Skip to content

Ekryd/reflection-utils

Folders and files

NameName
Last commit message
Last commit date
Jan 29, 2025
Aug 22, 2023
Aug 22, 2023
Oct 7, 2013
Mar 15, 2015
Oct 16, 2020
Oct 1, 2015
Feb 27, 2025
Oct 16, 2023

Repository files navigation

Reflection-utils Icon

Build Status Coverage Status Maven Central Coverity

This library will make it easier to get and set internal fields in classes using java reflection. The library is optimized for convenience and not for speed, so it works best with unit tests.

Mocking is great! Sometimes the easiest way to insert mocks are by Java reflection. Especially if you cannot use constructor arguments or setter methods.

Example of how to use the ReflectionHelper in a test

    @Test
    public void storeMethodShouldSaveEntity() throws Exception {
        // Setup entities
        EntityHandler handler = new EntityHandlerImpl();        
        StoreDao storeDao = mock(StoreDao.class);
        
        // *** This is the ReflectionHelper! You don't need to know the name of field nor the type, just set the instance ***
        new ReflectionHelper(handler).setField(storeDao);
        
        // Perform method
        handler.store();

        // Verify save method
        verify(storeDao).save();
    }

See more