[英]StepBuilder.chunk() returning null with mockito
I have a Spring batch configuration class MyConfig with my reader, writer, step and Job beans.我有一个 Spring 批量配置 class MyConfig 和我的阅读器、编写器、步骤和作业 bean。 Below is my Step bean.下面是我的 Step bean。
@Bean
public Step myStep() {
return stepBuilderFactory.get(STEP_NAME).<MyType, MyType>chunk(getMinimumChunkSize())
.reader(myReader)
.writer(myWriter)
.build();
}
When I try unit testing it with the below class当我尝试使用以下 class 对其进行单元测试时
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = {MyConfig.class})
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
public class MyConfigTest {
@Mock
private StepBuilderFactory stepBuilderFactory;
@Mock
private StepBuilder stepBuilder;
@Mock
private SimpleStepBuilder simpleStepBuilder;
@Mock
FlatFileItemWriter myWriter;
@Mock
JdbcCursorItemReader myReader;
@InjectMocks
private MyConfig myConfig;
@Before
public void setUp() {
when(stepBuilderFactory.get(anyString())).thenReturn(stepBuilder);
when(stepBuilder.chunk(any())).thenReturn(simpleStepBuilder);
when(simpleStepBuilder.reader(myReader)).thenReturn(simpleStepBuilder);
when(simpleStepBuilder.writer(myWriter)).thenReturn(simpleStepBuilder);
}
@Test
public void testChecklistStep() {
final Step step = myConfig.checklistStep();
assertNotNull(step);
}
}
On debug I get stepBuilderFactory.get(STEP_NAME)
gives the mock as expected shown below在调试时,我得到stepBuilderFactory.get(STEP_NAME)
给出了如下所示的模拟
but the stepBuilderFactory.get(STEP_NAME).<MyType, MyType>chunk(getMinimumChunkSize())
is returning null instead of SimpleStepBuilder mock.但是stepBuilderFactory.get(STEP_NAME).<MyType, MyType>chunk(getMinimumChunkSize())
返回 null 而不是 SimpleStepBuilder 模拟。 This looks pretty straight forward, I am new to mockito and spring batch.这看起来很简单,我是 mockito 和 spring 批次的新手。 Why is the thenReturn not working in this case?为什么 thenReturn 在这种情况下不起作用?
I tried reading official documentation but this was of less help to what i am trying to do.我尝试阅读官方文档,但这对我想做的事情帮助不大。 Please suggest if my approach is wrong or if I'm missing anything?请建议我的方法是否错误或我是否遗漏了什么?
That's because the chunk
method creates a new step builder wrapping your mock: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java#L69 .这是因为chunk
方法创建了一个新的 step builder 来包装你的 mock: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/批处理/核心/步骤/生成器/StepBuilder.java#L69 。 So stepBuilder.chunk(any())
is not the mock you think it is.所以stepBuilder.chunk(any())
不是你想象的那样。
That said, I'm wondering why you need to mock StepBuilderFactory
and StepBuilder
anyway.也就是说,我想知道为什么你仍然需要模拟StepBuilderFactory
和StepBuilder
。 Your test is testing that stepBuilder
correctly creates a Step.您的测试正在测试stepBuilder
是否正确创建了 Step。 Unless you do not trust Spring Batch code, you should not be doing this.除非您不信任 Spring 批处理代码,否则您不应该这样做。
What you should be doing though is testing the actual business logic of your Step (correctly reading/writing data, its processing logic, etc).不过,您应该做的是测试 Step 的实际业务逻辑(正确读取/写入数据、其处理逻辑等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.