Reputation: 674
I have this method to update a table in DB, it works correctly, until I create the unit test for it:
public class ReferentialAdapter {
private final JPAQueryFactory queryFactory;
public void enableServiceConfigs(String roleCode, String countryIsoCode, List<String> services) {
QServiceConfigEntity qServiceConfigEntity = QServiceConfigEntity.serviceConfigEntity;
QCountryRoleEntity qCountryRoleEntity = QCountryRoleEntity.countryRoleEntity;
QServiceEntity qServiceEntity = QServiceEntity.serviceEntity;
JPQLQuery<Long> countryRoleId = JPAExpressions.select(qCountryRoleEntity.id).from(qCountryRoleEntity)
.where(qCountryRoleEntity.country.isoCode.equalsIgnoreCase(countryIsoCode).and(qCountryRoleEntity.role.code.equalsIgnoreCase(roleCode)));
BooleanExpression whereCondition = qServiceConfigEntity.countryRole.id.eq(countryRoleId)
.and(qServiceConfigEntity.service.id.in(serviceIds));
queryFactory.update(qServiceConfigEntity)
.where(whereCondition)
.set(qServiceConfigEntity.isEnabled, Boolean.TRUE)
.execute();
}
}
configuration for JPAQueryFactory:
@Configuration
public class QueryDslConfig {
@PersistenceContext
private EntityManager entityManager;
@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
but this test throw a null pointer exception when call the execute() for the update.
@ExtendWith(MockitoExtension.class)
class ReferentialAdapterTest {
@Mock
private JPAQueryFactory queryFactory;
@InjectMocks
private ReferentialAdapter referentialAdapter;
@Test
void shouldEnableServicesConfig() {
String roleCode = "CA_ICS_HBE";
String countryIsoCode= "FR";
List<String> services = Arrays.asList("VT" , "CAF");
referentialAdapter.enableServiceConfigs(roleCode,countryIsoCode,services);
verify(queryFactory,times(1)).update(any());
}
}
Upvotes: 0
Views: 232