2013-03-12 134 views
4

我有一个Spring项目。它适用于junit测试用例。 juint调用applicationcontext.xml并且项目成功运行。但我想在不使用jUnit的情况下运行该项目。 这里是我的JUnit测试用例如何加载Spring应用程序上下文

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 */ 
    } 
} 

任何人都可以建议我如何编写代码?由于

回答

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()); 
+0

谢谢。它的载入。但是当我运行测试用例时,我不需要调用其他类中的所有方法。它正在自动执行。但是在这里只加载applicationcontext.xml。为什么会发生? – 2013-03-12 09:12:01

+0

如果你的applicationContext有所有的bean被初始化,那么这将工作正常。如果你想调用其他东西,你必须在加载应用程序上下文后手动执行。你希望你的主类表现如何? – Anubhab 2013-03-12 09:14:22

+0

检查编辑..我认为这就是你需要的 – Anubhab 2013-03-12 09:16:42

1

开始我使用的方式添加这个,它是为我工作。

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

} 

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

这里学生是我的班级,你会得到与你的班级匹配的班级。

现在,您可以使用任何您想要执行的操作来处理该对象。

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); 
     } 
    } 
相关问题