如何在springbatch测试中的when().thenReturn()语句中模拟私有方法

问题描述 投票:0回答:1

springBatch Bean 中有一个私有方法。配置类。

@Slf4j
public class EodDataDumpSpringBatchConfig {

private Resource getWriterResource(StepExecution stepExecution, Storage storage) {
        String chunkUri = stepExecution.getJobParameters().getString(EodDataDumpConstants.CHUNK_URI);
        String fileUri = chunkUri + stepExecution.getStepName();
        Resource resource = new GoogleStorageResource(storage, fileUri);
        return resource;
    }

....

}

我正在写一个springBatchTest:

@SpringBatchTest
@EnableAutoConfiguration
@SpringJUnitConfig({EodDataDumpSpringBatchConfig.class})
@ExtendWith(MockitoExtension.class)
@TestPropertySource(locations = "classpath:application.properties")
@MockitoSettings(strictness = Strictness.LENIENT)
public class EodDataDumpSpringBatchTest {

@Value("file:src/test/resources/storageFile.csv")
    private Resource resource;

@Test
    public void dataDumpJobEndToEndTest() throws Exception {
        
    }

}

我想模拟when().thenReturn语句中的私有方法。 我的意思是,当任何方法调用该私有方法时,它应该获得一个模拟的资源。

我尝试过:

  1. ReflectionUtils
  2. PowerMockit
spring-boot mockito spring-batch
1个回答
0
投票

你不能模拟私有函数,你可以将其更改为包私有,然后你就可以测试它了。

看看这个答案

© www.soinside.com 2019 - 2024. All rights reserved.