Spring batch:Test case for Tasklet – Key is not appearing in actual class when it is invoked from Test class

huangapple 未分类评论50阅读模式
英文:

Spring batch:Test case for Tasklet - Key is not appearing in actual class when it is invoked from Test class

问题

我正在尝试学习批处理(Batch)和任务块(Tasklet)。

我正在为 Spring Batch 中的任务块代码编写测试用例。我在测试类中设置了一个映射(map),然后进行调试,但实际的类中并没有我从测试类中传递的键(key)。

MyEventTasklet.java

public class MyEventTasklet implements Tasklet {
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
        TreeMap<String, Map<Integer, Set<Student>>> studentMap = chunkContext.getStepContext().getJobExecutionContext().get("keyOfStudentMap");
    }
}

MyEventTaskletTest.java

@RunWith(MockitoJUnitRunner.class)
public class MyEventTaskletTest {

    @Mock
    StepContribution stepContribution;

    @Mock
    ChunkContext chunkContext;

    @Mock
    StepContext stepContext;

    @InjectMocks
    MyEventTasklet myEventTasklet = new MyEventTasklet();

    @Test
    public void testExecute() {

        TreeMap<String, Map<Integer, Set<Student>>> studentMap = new TreeMap<>();
        Map<Integer, Set<Student>> m2 = new TreeMap<>();
        m2.put(100, createStudentData());

        studentMap.put("keyOfStudentMap", m2);

        Map<String, Object> map = new TreeMap<>();
        map.put("keyOfStudentMap", new Object());

        chunkContext = Mockito.mock(ChunkContext.class);
        stepContribution = Mockito.mock(StepContribution.class);
        stepContext = Mockito.mock(StepContext.class);

        Mockito.when(stepContext.getJobExecutionContext()).thenReturn(map);
        Mockito.when(chunkContext.getStepContext()).thenReturn(stepContext);
        Mockito.when(chunkContext.getStepContext().getJobExecutionContext().get("keyOfStudentMap"))
            .thenReturn((TreeMap<String, Map<Integer, Set<Student>>>) studentMap);

        // 当我进行调试时,我能看到这里的 studentMap 对象内容为 {keyOfStudentMap={100=[StudentObject]}},
        // 但是当我查看实际的类时,它变成了 {100=[StudentObject]}
    }
}

我不确定为什么会出现这种情况,我是否做错了什么?非常感谢您提供任何帮助。

英文:

I am trying to learn Batch and Tasklet.

I am writing a test case for a Tasklet code in spring batch. I am setting a map in my Test class and debug, the actual class is not having the key which I am passing from my test class.

MyEventTasklet.java

public class MyEventTasklet implements Tasklet {
    public RepeatStatus execute (StepContribution contribution, ChunkContext chunkContext){
    
    TreeMap&lt;String, Map&lt;Integer, Set&lt;Student&gt;&gt;&gt; studentMap = chunkContext.getStepContext().getJobExecutionContext().get(&quot;keyOfStudentMap&quot;);
    
 }   
    }

MyEventTaskletTest.java

         @RunWith(MockitoJunitRunner.class)
            public class MyEventTaskletTest{
            
            @Mock
            StepContribution stepContribution;
            
            @Mock
            ChunkContext chunkContext;
            
            @Mock
            StepContext stepContext;
            
            @InjectMocks
            MyEventTasklet myEventTasklet = new MyEventTasklet();
            
            @Test
            public void testExecute(){
            
            TreeMap&lt;String, Map&lt;Integer, Set&lt;Student&gt;&gt;&gt; studentMap = new TreeMap&lt;&gt;();
            Map&lt;Integer, Set&lt;Student&gt;&gt; m2 = new TreeMap&lt;&gt;();
            m2.put(100, createStudentData());
            
            studentMap.put(&quot;keyOfStudentMap&quot;, m2);
            
            Map&lt;String, Object&gt; map = new TreeMap&lt;&gt;();
            map.put(&quot;keyOfStudentMap&quot;, new Object());
            
            chunkContext = Mockito.mock(ChunkContext.class);
            stepContribution = Mockito.mock(StepContribution.class);
            stepContext = Mockito.mock(StepContext.class);
            
            Mockito.when(stepContext.getJobExecutionContext()).thenReturn(map);
            Mockito.when(chunkContext.getStepContext()).thenReturn(stepContext);
            Mockito.when(chunkContext.getStepContext().getJobExecutionContext().get(&quot;keyOfStudentMap&quot;))
            .thenReturn((TreeMap&lt;String, Map&lt;Integer, Set&lt;Student&gt;&gt;&gt;)studentMap);
            
        // When I am debugging I can see here, the studentMap object which is having something like {keyOfStudentMap={100=[StudentObject]}}, 
but when I see in actual class it is becoming {100=[StudentObject]}
            
     }
            
 }

I am not sure why this is happening, I am doing something wrong? Any kind of help would be highly appreciated.

答案1

得分: 0

你的问题在这里:

Mockito.when(stepContext.getJobExecutionContext()).thenReturn(map);

map 不是一个 ExecutionContext。你应该对一个 ExecutionContext 实例进行模拟/存根,而不是一个 Map&lt;String, Object&gt; 实例。你的任务应该调用:

chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext()

而不是:

chunkContext.getStepContext().getJobExecutionContext()
英文:

Your problem is here:

Mockito.when(stepContext.getJobExecutionContext()).thenReturn(map);

map is not an ExecutionContext. You should be mocking/stubbing an ExecutionContext instance and not a Map&lt;String, Object&gt; instance. Your tasklet should be calling:

chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext()

instead of:

chunkContext.getStepContext().getJobExecutionContext()

huangapple
  • 本文由 发表于 2020年5月5日 03:57:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61600546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定