PowerMockito使用

PowerMockito使用案例

PowerMockito的一个使用案例,记录下

pom.xml引入依赖


	junit
	junit
	4.12


	org.mockito
	mockito-core
	3.6.0


	org.powermock
	powermock-module-junit4
	2.0.2


	org.powermock
	powermock-api-mockito2
	2.0.2

代码案例
@RunWith(PowerMockRunner.class)
@PrepareForTest({LiveSyncOverriddenTasklet.class})
public class LiveSyncOverriddenTaskletTest {

    private LiveSyncOverriddenTasklet liveSyncOverriddenTasklet = new LiveSyncOverriddenTasklet();
    private static final String KEY = "sync.overridden.service.enable";

    private String datasetIdKey = "datasetIdSync";

    @Mock
    private ApplicationConfiguration appConfig;
    @Mock
    private Configuration config;
    @Mock
    private DsmRestTemplateService mockTemplate;
    @Mock
    private JobLauncher jobLauncher;

    private ChunkContext chunkContext;

    private StepContribution stepContribution;

    @Before
    public void setUp(){
        HashMap jobParamterMap = new HashMap();
        jobParamterMap.put("processRunId", new JobParamter("005"));
        JobParamters jobParamters = new JobParamters(jobParamterMap);

        JobInstance jobInstance = new JobInstance(1L, jobParamters, "test");
        JobExecution jobExecution = new JobExecution(jobInstance, 1l);
        StepExecution stepExecution = new StepExecution("test", 1);
        stepContribution = new StepContribution(stepExecution);
        chunkContext = new ChunkContext(new StepContext(stepExecution));
        liveSyncOverriddenTasklet.setAppConfig(appConfig);
        liveSyncOverriddenTasklet.setConfig(config);
    }

    @Test
    public void test_errorCase(){
        Map jobResponse = new LinkedHashMap<>();
        jobResponse.put("id", 1l);
        ApiResponse response = ApiResponse.fail(1, "get request failed");
        response.put("status", "FAIL");
        PowerMockito.whenNew(DsmRestTemplateService.calss).withArgument(ApiResponse.calss).thenReturn(mockTemplate);
        PowerMockito.when(mockTemplate.postRequest(any(JobExecutionDto.class), anyString(), anyString))).thenReturn(ApiRespone.success(jobResponse));

        liveSyncOverriddenTasklet.setDatasetIdKey("datasetIdSync");
        PowerMockito.when(liveSyncOverriddenTasklet.getConfig().getBoolean(KEY, false)).thenReturn(true);
        try{
            liveSyncOverriddenTasklet.execute(stepContribution, chunkContext);
        }catch (Exception e){
            Assert.assertrue(e instanceof RavenApplicationException);
        }
    }

    @Test
    public void test_successCase(){
        Map jobResponse = new LinkedHashMap<>();
        jobResponse.put("id", 1l);
        ApiResponse response = ApiResponse.fail(1, "get request failed");
        response.put("status", "COMPLETED");
        PowerMockito.whenNew(DsmRestTemplateService.calss).withArgument(ApiResponse.calss).thenReturn(mockTemplate);
        PowerMockito.when(mockTemplate.postRequest(any(JobExecutionDto.class), anyString(), anyString))).thenReturn(mockTemplate);

        liveSyncOverriddenTasklet.setDatasetIdKey("datasetIdSync");
        PowerMockito.when(liveSyncOverriddenTasklet.getConfig().getBoolean(KEY, false)).thenReturn(true);
        try{
            liveSyncOverriddenTasklet.execute(stepContribution, chunkContext);
        }catch (Exception e){
            Assert.assertrue(e instanceof RavenApplicationException);
        }
    }

}

你可能感兴趣的:(java,junit)