I am writing a rule to determine the type of service that should be received based on the category of grievance.
I wrote a jUnit integration test to test the rule using a newly created java object called 'grievanceDTO'.
@Test
public class RuleTest {
@Autowired
private GrievanceRuleService grievanceRuleService;
@Autowired
public KieContainer kieContainer;
@Test
public void calculateEligibility() {
Long id = (long) 1;
GrievanceDTO grievanceDTO = new GrievanceDTO(id,"NA", "Access/Availability", "NA", "NA", "NA", "NA", "NA");
String serviceType = grievanceRuleService.determineGrievance(grievanceDTO);
assertNotNull(serviceType);
}
}
I wrote a service class to fire the rules.
@Service
public class GrievanceRuleService {
@Autowired
public KieContainer kieContainer;
public String determineGrievance(GrievanceDTO grievanceDTO) {
KieSession kieSession = kieContainer.newKieSession();
kieSession.setGlobal("grievanceDTO", grievanceDTO);
kieSession.insert(grievanceDTO);
kieSession.fireAllRules();
kieSession.dispose();
return grievanceDTO.getServiceType();
}
}
The jUnit test fails.
java.lang.NullPointerException at cm.grievance.rules.RuleTest.calculateEligibility(RuleTest.java:13)
I wrote a separate configuration class with a KieContainer bean, which I autowire to my service.
@Configuration
@EnableJpaRepositories
@ComponentScan("dm.vantage.producer.service")
public class RuleConfiguration {
public static final String grivFile = "/src/main/resources/drools/GrievanceRules.drl";
@Bean
public KieContainer kieContainer() {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource(grivFile));
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
}
And finally, my rule (DRL) file, which I made as simple as possible for the purpose of testing.
package drools;
//generated from Decision Table
import cm.grievance.service.dto.GrievanceDTO;
rule "grievancesMap_10"
when
$grievances:GrievanceDTO($grievances.getCategory().equals("NA"))
then
$grievances.setServiceType("Timely Access");
end
I can add the dto, but basically it has 'category' as the fourth field and 'serviceType' as the third.
I don't know why I get a null pointer as I have tested this and it should be populating the return statement with some value.
I was able to figure out the issue. It was with this line of code.
GrievanceDTO grievanceDTO = new GrievanceDTO(id,"NA", "Access/Availability", "NA", "NA", "NA", "NA", "NA");
Instead of this instantiation, I replaced the assignment of the variable using setter methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.