原创

MockStatic と MockConstruction が春のバッチ テスト ケースで機能しない

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

Springタスクレットのステップテストで静的関数と新しいインスタンス化をモックしようとしていますが、モックは機能せず、実際の実装に移行します。

@SpringBatchTest
@SpringBootTest
@SpringJUnitConfig(BatchConfig.class)
@EnableAutoConfiguration
@ContextConfiguration(classes = { BatchConfig.class, DataSourceConfiguration.class, DatabaseConfig.class})
@ActiveProfiles(profiles = "test")
@TestPropertySource(value = { "classpath:application-test.properties" })

public class TestingProcessor {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;
    @Autowired
    private JobRepositoryTestUtils jobRepositoryTestUtils;
    @InjectMocks
    Processor processor;
    @InjectMocks
    Payload payload;

    @MockBean
    JobListner jobListner;
    @MockBean
    AlertNotification ans;
    @MockBean
    AlertConfig alertConfig;
    
    @Autowired
    @Qualifier("pipeline")
    Job job;

    @Autowired
    @Qualifier("JobLauncher")
    JobLauncher jobLauncher;

    @AfterEach
    public void cleanUp() {
        jobRepositoryTestUtils.removeJobExecutions();
    }

    @BeforeEach
    public void initMocks() {
        MockitoAnnotations.openMocks(this);
    }

    public StepExecution getExecutionContext() {
        StepExecution execution = MetaDataInstanceFactory.createStepExecution();
        execution.getExecutionContext().put(SchedulerConstants.CONTEXT, context);
        return execution;
    }
    @Test
    public void testingProcessorTasklet() throws Exception {
        this.jobLauncherTestUtils.setJobLauncher(jobLauncher);

        JobParameters jobParameters = new JobParametersBuilder()
                .addString(SchedulerConstants.TENANTID_CONTEXT, "TestTenantId")
                .addLong("time", System.currentTimeMillis())
                .toJobParameters();
        StepExecution executionContext = getExecutionContext();

        when(mockedClass1.getTenant(anyString())).thenReturn("mockedClass2");
        when(mockedClass2.getTenantId()).thenReturn("mockTenantId");
//these are working they return the mocked values


        try (MockedStatic<ConnectionFactory> mockedStatic = Mockito.mockStatic(ConnectionFactory.class)) {
            Connection connection = mock(Connection.class);
            mockedStatic.when(() -> ConnectionFactory.getConnection(any())).thenReturn(connection);
//these are not working even MockConstruction is not mocking

            JobExecution jobExecution = jobLauncherTestUtils.launchStep("PROCESS_IT", jobParameters, executionContext
                    .getExecutionContext());

            Assertions.assertEquals(BatchStatus.STARTING, jobExecution.getStatus());
            while (jobExecution.isRunning()) {
            }
            Assertions.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
        }
    }
}

他にモックする方法はありますか、私のやり方に問題がありますか。

conn = ConnectionFactory.getConnection(tenantID);

これは、Spring バッチ 5 と mockito5 を使用して実装をモックしたい実行です。 スプリング バッチ アノテーションが存在しない他のファイルでは、モック静的が正常に機能しているため、アノテーションが原因であると思います。

これらのクラスの実際の実装を実行しない場合、これらの呼び出しをモックするにはどうすればよいですか?

正文到此结束
热门推荐
本文目录