paulwong

          SPRING BATCH 測試

          SPRING BATCH 柯以測試的內容:JOB, STEP, INTEMPROCESSOR, ITEMREADER, ITEMWRITER。
          JOB, STEP屬于功能測試(黑盒)的范疇,INTEMPROCESSOR, ITEMREADER, ITEMWRITER屬于單元測試(白盒)的范疇。

          /*
           * Copyright 2006-2007 the original author or authors.
           *
           * Licensed under the Apache License, Version 2.0 (the "License");
           * you may not use this file except in compliance with the License.
           * You may obtain a copy of the License at
           *
           *      
          http://www.apache.org/licenses/LICENSE-2.0
           *
           * Unless required by applicable law or agreed to in writing, software
           * distributed under the License is distributed on an "AS IS" BASIS,
           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           * See the License for the specific language governing permissions and
           * limitations under the License.
           
          */
          package example;

          import static org.junit.Assert.assertEquals;
          import static org.junit.Assert.assertNotNull;

          import java.util.Date;
          import java.util.concurrent.Callable;

          import org.junit.Before;
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.batch.core.BatchStatus;
          import org.springframework.batch.core.Job;
          import org.springframework.batch.core.JobExecution;
          import org.springframework.batch.core.JobParameters;
          import org.springframework.batch.core.JobParametersBuilder;
          import org.springframework.batch.core.StepExecution;
          import org.springframework.batch.core.launch.JobLauncher;
          import org.springframework.batch.item.ExecutionContext;
          import org.springframework.batch.item.ItemReader;
          import org.springframework.batch.item.ItemStream;
          import org.springframework.batch.item.NonTransientResourceException;
          import org.springframework.batch.item.ParseException;
          import org.springframework.batch.item.UnexpectedInputException;
          import org.springframework.batch.test.JobLauncherTestUtils;
          import org.springframework.batch.test.MetaDataInstanceFactory;
          import org.springframework.batch.test.StepScopeTestExecutionListener;
          import org.springframework.batch.test.StepScopeTestUtils;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.test.annotation.DirtiesContext;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.TestExecutionListeners;
          import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
          import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

          @ContextConfiguration(locations = { "/test-context.xml",
                  "classpath:/META-INF/spring/batch/hello-tasklet-context.xml",
                  "classpath:/META-INF/spring/batch/jdbc-job-context.xml",
                  "classpath:/META-INF/spring/integration/hello-integration-context.xml"})
          @RunWith(SpringJUnit4ClassRunner.class)
          //測試ITEMREADER/ITEMPROCESSOR/ITEMWRITER時用到
          @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class
              StepScopeTestExecutionListener.class })
          public class HelloTaskletTests {
              
              @Autowired
              private JobLauncher jobLauncher;
              
              @Autowired
              private Job helloWorldJob;

              @Autowired
              private JobLauncherTestUtils jobLauncherTestUtils;//測試JOB/STEP的入口
              
              @Autowired
              private ItemReader xmlReader;
              
              public void testLaunchJobWithJobLauncher() throws Exception {
                  JobExecution jobExecution = jobLauncher.run(helloWorldJob, new JobParameters());
                  assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
              }

              /**
               * Create a unique job instance and check it's execution completes
               * successfully - uses the convenience methods provided by the testing
               * superclass.
               
          */
              @Test
              public void testLaunchJob() throws Exception {

                  JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobLauncherTestUtils.getUniqueJobParameters());
                  assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
              }
              
              public void testIntegration()
              {
                  while(true)
                  {
                      
                  }
              }
              
              /**
               * 測試某個STEP
               
          */
              @Test
              public void testSomeStep()
              {
                  JobExecution jobExecution = jobLauncherTestUtils.
                          launchStep("xmlFileReadAndWriterStep",getJobParameters());
                  assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
              }
              
              /**
               * 測試READER的方式1時,所需的方法
               * 
          @return
               
          */
              public StepExecution getStepExecution() {
                  StepExecution execution = MetaDataInstanceFactory
                          .createStepExecution(getJobParameters());
                  return execution;
              }
              
              /**
               * 測試READER的方式1
               * 
          @throws Exception
               
          */
              @Test
              @DirtiesContext
              public void testReader() throws Exception {
                  int count = StepScopeTestUtils.doInStepScope(getStepExecution(),
                          new Callable<Integer>() {
                              @Override
                              public Integer call() throws Exception {
                                  int count = 0;
                                  try {
                                      ((ItemStream) xmlReader)
                                              .open(new ExecutionContext());
                                      while (xmlReader.read() != null) {
                                          count++;
                                      }
                                      return count;
                                  } finally {
                                      ((ItemStream) xmlReader).close();
                                  }
                              }
                          });
                  assertEquals(3, count);
              }
                
              /**
               * 測試READER的方式2
               * 
          @throws UnexpectedInputException
               * 
          @throws ParseException
               * 
          @throws NonTransientResourceException
               * 
          @throws Exception
               
          */
              @Test
              @DirtiesContext
              public void testReader2() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception
              {
                  assertNotNull(xmlReader.read());
              }
              
              /**
               * 測試READER的方式2時,必須加的方法
               
          */
              @Before
              public void setUp() {
                  ((ItemStream) xmlReader).open(new ExecutionContext());
              }
                
              /**
               * 
               * 
          @return
               
          */
              private JobParameters getJobParameters() {
                  
                  String inputFile = "/Users/paul/Documents/PAUL/DOWNLOAD/SOFTWARE/DEVELOP/"
                          + "SPRING BATCH/spring-batch-2.1.9.RELEASE/samples/"
                          + "spring-batch-simple-cli/file/trades1.xml";
                  
                  String outputFile = "/Users/paul/Documents/PAUL/DOWNLOAD/SOFTWARE/DEVELOP/"
                          + "SPRING BATCH/spring-batch-2.1.9.RELEASE/samples/"
                          + "spring-batch-simple-cli/file/output/out.xml";
                  
                  JobParameters jobParameters = new JobParametersBuilder()
                          .addString("input.file.path", inputFile)
                          .addString("output.file.path", outputFile)
                          .addDate("date", new Date()).toJobParameters();
                  
                  return jobParameters;
              }


          }


          參考例子: 
          http://code.google.com/p/springbatch-in-action/source/browse/trunk/sbia/ch15/src/test/java/com/manning/sbia/ch15/batch/integration/?r=128#integration%2Fjob

          SPRING-BATCH TEST
          https://src.springframework.org/svn/spring-batch/trunk/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/

          posted on 2012-11-10 17:25 paulwong 閱讀(2340) 評論(0)  編輯  收藏 所屬分類: SRPING BATCH

          主站蜘蛛池模板: 怀宁县| 孟村| 孝义市| 伊宁县| 麻栗坡县| 寿光市| 陇西县| 都江堰市| 郓城县| 正安县| 芦溪县| 全椒县| 江阴市| 郯城县| 兰坪| 龙井市| 丰城市| 黑龙江省| 苏尼特右旗| 兖州市| 额尔古纳市| 婺源县| 涿州市| 绥阳县| 叙永县| 大田县| 石景山区| 错那县| 鄂托克前旗| 永和县| 大余县| 波密县| 桓仁| 虹口区| 永丰县| 龙海市| 邵阳县| 镇雄县| 观塘区| 抚顺县| 睢宁县|