5

私は春のプロジェクトを持っています。junit テストケースで問題なく動作します。juint が applicationcontext.xml を呼び出し、プロジェクトが正常に実行されます。しかし、jUnit を使用せずにプロジェクトを実行したいです。これが私のjUnitテストケースです

package com.dataload;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.util.StopWatch;

@ContextConfiguration(locations = "classpath:com/dataload/applicationcontext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class SICSVDataTestCase {

    private final static Logger logger = Logger
            .getLogger(SICSVDataTestCase.class);

    @Autowired
    private JobLauncher launcher;

    @Autowired
    private Job job;
    private JobParameters jobParameters = new JobParameters();

    @Test
    public void testLaunchJob() throws Exception {
        StopWatch sw = new StopWatch();
        sw.start();
        launcher.run(job, jobParameters);
        sw.stop();
        logger.info(">>> TIME ELAPSED:" + sw.prettyPrint());
    }
}

このテストケースはプロジェクトを実行します。しかし、applicationcontext.xml を呼び出してプロジェクトを実行できる別のクラスを使用したいと考えています。

あれは、

package com.dataload;

public class insertCSV 
{
    public static void main(String args[])
    {
        /*  Code to run the project */
    }
}

コーディング方法を教えてもらえますか? ありがとう

4

3 に答える 3

11

メインの開始時にこれを追加します

ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml");

JobLauncher launcher=(JobLauncher)context.getBean("launcher");
Job job=(Job)context.getBean("job");

//Get as many beans you want
//Now do the thing you were doing inside test method
StopWatch sw = new StopWatch();
sw.start();
launcher.run(job, jobParameters);
sw.stop();
//initialize the log same way inside main
logger.info(">>> TIME ELAPSED:" + sw.prettyPrint());
于 2013-03-12T09:05:07.073 に答える
1
package com.dataload;

    public class insertCSV 
    {
        public static void main(String args[])
        {
            ApplicationContext context =
        new ClassPathXmlApplicationContext("applicationcontext.xml");


            // retrieve configured instance
            JobLauncher launcher = context.getBean("laucher", JobLauncher.class);
            Job job = context.getBean("job", Job.class);
            JobParameters jobParameters = context.getBean("jobParameters", JobParameters.class);
        }
    }
于 2013-03-12T09:09:30.190 に答える
1

私は途中で使用していますが、それは私のために働いています。

public static void main(String[] args) {
    new CarpoolDBAppTest();

}

public CarpoolDBAppTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    Student stud = (Student) context.getBean("yourBeanId");
}

ここで Student は私のクラスで、yourBeanId に一致するクラスを取得します。

次に、実行したい操作でそのオブジェクトに取り組みます。

于 2013-03-12T09:08:08.873 に答える