'How to fix java.lang.NumberFormatException: null in JUnit

I have a JUnit class for code coverage for spring batch processor class. The test class passes without any failure when executed individually but fails when i executes test run for all the test classes in the module. Somehow job execution context value is not being picked when running for entire module.

This is the class for which i wrote test class

public class RegistryResponseConverter implements ItemProcessor<Bundle, String> {

    
    @Override
    public String process(Bundle item) throws Exception {
        MetaDataInfo metadata = new MetaDataInfo(); 
        Long registryRequestKey = BatchUtility.getStepExecutionContextValue(BatchKeyConstants.REQUEST_KEY);
        String dataOriginKeyStr = BatchUtility.getJobExecutionContextValue(BatchKeyConstants.DATA_ORIGIN_KEY);
        Long dataOriginKey=Long.parseLong(dataOriginKeyStr);
        //removed rest of the codes as problem is at above lines getJobExecutionContextValue
        

        
    }

}

Below is Test class i wrote for the above class

@RunWith(MockitoJUnitRunner.class)
public class RegistryResponseConverterTest {

    @Mock
    private StepContribution contribution;

    @Mock
    private StepExecution stepExecution;

    @Mock
    private StepContext stepContext;

    @Mock
    private JobContext jobContext;

    @Mock
    private ChunkContext chunkContext;

    @Mock
    private JobExecution jobExecution;

    @Mock
    private JobParameters jobParameters;

    @Mock
    RequestGroupService requestGroupService;

    @Mock
    JobSynchronizationManager jobSynchronizationManager;

    @Mock
    ProcessStatusMasterService processStatusMasterService;
    
    @Mock
    DataOriginSettingServiceImpl dataOriginSettingService;
    
    
    @Mock
    private BundleToHL7Convertor bundleToHL7Conversion;
    
    @Mock
    RequestStatusHistoryService requestStatusHistoryService;
    
    @Mock
    private ZipFileCreation zipFileCreationStep;
    
    @Mock
    RegistryResponseService registryResponseService;
    
    @Mock
    RequestResponseFileTrackerService requestResponseFileTrackerService;
    
    @InjectMocks
    RegistryResponseConverter registryResponseConverter;

    @Before
    public void setupTest() {

    }


    @Test
    public void process() throws Exception {
        Bundle item=new Bundle();
        JobExecution jobExecution = new JobExecution(5l);
        ExecutionContext ctx = new ExecutionContext();
        ctx.putString(BatchKeyConstants.DATA_ORIGIN_KEY, "100");
        ctx.putLong(BatchKeyConstants.REQUEST_KEY, 100L);
        jobExecution.setExecutionContext(ctx);
        JobSynchronizationManager.register(jobExecution);
        StepExecution stepExecution=new StepExecution("processor", jobExecution);
        stepExecution.setExecutionContext(ctx);
        StepSynchronizationManager.register(stepExecution);
        when(dataOriginSettingService.findBySettingNameAndType(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(stepExecution);
        //when(bundleToHL7Conversion.doProcess(Mockito.any(), Mockito.any(), Mockito.any(),Mockito.any()));
        ProcessAndMacroStatus success = new ProcessAndMacroStatus();
        when(processStatusMasterService.findProcessMaster(Mockito.any())).thenReturn(success);
        RegistryResponse response=new RegistryResponse();
        when(registryResponseService.fetchByRequestKey(Mockito.any())).thenReturn(response);
        String str = registryResponseConverter.process(item);
        Assert.assertNull(str);

    }

}

Please help me why it is not picking job execution context value and giving null for dataOriginKeyStr value in case i am running test class for whole module but is successful when run as single test class



Sources

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

Source: Stack Overflow

Solution Source