'java.lang.IllegalArgumentException: Could not find field 'isBoolean' of type [class java.lang.Boolean] on target object

When I run test then it failed at this point ReflectionTestUtils.setField(service, SeRepositoryImpl.class, "isBoolean",true,Boolean.class) complains about Could not find field 'isBoolean' of type not found. Error trace as below. I am not sure why because my repositoryImpl class has isBoolean variable defined.

java.lang.IllegalArgumentException: Could not find field 'isBoolean' of type [class java.lang.Boolean] on target object [lautomation.repository.impl.SaRepositoryImpl@4a178d1e] or target class [lautomation.repository.impl.SaRepositoryImpl]
at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:175)

test class looks like

@MockBean(name = "seRepository")
PolicyRepository seRepository;

@InjectMocks
private SeRepositoryImpl service;


@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
}



@Test
public void testUpdateStatus() throws Exception{
    
    ReflectionTestUtils.setField(service, SeRepositoryImpl.class, "isBoolean",true,Boolean.class);
    List<Policy> policies = Arrays.asList(new Policy[] {new Policy() });        
    service.updateIssuedStatus(Mockito.any(Policy.class));
    Mockito.verify(seRepository, Mockito.times(1)).updateIssuedStatus(Mockito.any(Policy.class));

}

}

Respository implementation class SeRepositoryImpl has isBoolean variable defined

@Repository("seRepository")
@Log4j

public class SeRepositoryImpl implements PolicyRepository {
@Value("${change-db}")
private boolean isBoolean;
@Autowired
@Qualifier("jdbcDbName")
private NamedParameterJdbcTemplate jdbcTemplate;

@Override
public void updateIssuedStatus(final Policy policy) {
    if(!isBoolean) {
        log.warn("isBoolean is set to false - skipping db write.");
        return;
    }
    final HashMap<String, String> params = new HashMap<>();
    params.put("issued",
            new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    params.put("id", Integer.toString(policy.getId()));
    jdbcTemplate.update(updateIssuedStatus, params);
String currDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    log.info("params:"+Integer.toString(policy.getId())+" Date:"+currDate);

    final String sql = "call usp_updateDatabase(:policy,:currDate)";
    MapSqlParameterSource value = new MapSqlParameterSource();

    value.addValue("id",Integer.toString(policy.getId()));
    value.addValue("stop_dt",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    
        jdbcTemplate.update(sql, value);    
    

}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source