2013-03-12 139 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); 
     } 
    } 
相關問題