めも

技術メモとその他

spring batch : junit test sample

1.project files are below(use terasolua spring batch sample)

f:id:annazola:20190513073109p:plain

2.test codes(test class)

package batch;

 

 

import static org.junit.Assert.fail;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.JUnit4;

import org.springframework.batch.core.JobParameters;

import org.springframework.batch.test.JobLauncherTestUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:test-context.xml" ,

"classpath:job01.xml"})

 

public class testJob01 {

 

@Autowired

    private JobLauncherTestUtils jobLauncherTestUtils;

 

@Test

    public void testJob() throws Exception {

jobLauncherTestUtils.launchJob();

    }

 

}

 *if you code "JUnit4.class" after @RunWith annotation, @Autowired

failed(NPE)

 

3.test codes(setting file: test-context.xml)

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:batch="http://www.springframework.org/schema/batch"

       xmlns:util="http://www.springframework.org/schema/util"

       xmlns:jdbc="http://www.springframework.org/schema/jdbc"

       xmlns:c="http://www.springframework.org/schema/c"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd

            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

 

    <bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils">

        <property name="job" ref="job01" />

        <property name="jobRepository" ref="jobRepository" />

        <property name="jobLauncher" ref="jobLauncher" />

    </bean>

    </beans>