Linked Questions

Popular Questions

"How can i mock JobExecution while testing a Single step using JobLauncherTestUtils?"

"i`ve tried with

@MockBean JobExplorer jobExplorer 

but it is throwing an Exception:

Failed to load applicationContext,Caused by InstanceAlreadyExistsException

but it works fine when i am running it individually.So i tried to use

@Mock JobExplorer jobExplorer;
public void setUp(){
    MockitoAnnotations.init(this);
}

but This one not even working :( Then i added

@RunWith(MockitoJunitRunner.class)
@Configuartion(...)
class MockTest{
@Mock JobExplorer jobExplorer
public void setUp(){
    MockitoAnnotations.init(this);
}

}

Main Code:


@RunWith(SpringRunner.class)
@Configuration(classes=TestConfig.class)
Class MockTest{
   private static final String jobName ="job";
   @Autowired JobLauncherTestUtils;
   @Autowired JobRegistry jobRegistry;
   @MockBean JobExplorer jobExplorer;
@Before
public void setUp(){
   Job job = jobRegistry.getJob(jobName);
   JobInstance jobInstance = new JobInstance(0L,job.getName());
   JobExecution jobExecution = new JobExecution(0L);
   jobExecution.setJobInstance(jobInstance);
}
@Test
public testMock(){
   //jobParams
   Data data = new Data();
   ExecutionContext context = new ExecutionContext();
   context.put("data",Data);
   jobExecution.setExecutionContext(context);
   when(jobExplorer.getJobExecution(any()).thenReturn(jobExecution);
   JobExecution jobExecution = testUtils.launchStep("stepname",context);
}
@After
public void tearDown(){
//statements
}

Related Questions