如何去取ContextConfiguration

本文介绍了在Spring框架中如何通过不同方式配置测试上下文来获取Bean的方法,包括使用注解和XmlBeanFactory,并展示了多种配置文件加载的例子。
我们平时写test去测试Spring一起的程序时,一般都要加载context,然后再取bean之类。

写法如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {

@Autowired
MyService service;
...

}


现在的问题是,我要获取一个bean,但是上面这种通过annotation申明xml配置文件的方式,和我们原来的那种(如下),那光光写在annotation,那我怎么通过context的reference去获取bean呢?


[b]解决办法[/b]
其实申明一个context的成员变量,autowired一下就ok了
@Autowired
ApplicationContext context;



package com.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class SpringMain {

/**
* @param args
*/
public static void main(String[] args) {
SpringBean bean = null;
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();

ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
bean = (SpringBean)act.getBean("test1");
bean.sayHello();

factory = new XmlBeanFactory(new FileSystemResource("F:\\ccc\\workspace_zuhe\\Test\\src\\applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();
}

}



--------------------------------------

如果有多个xml的配置文件:
@ContextConfiguration(locations = {"classpath:com/xxx/aaa/applicationContext.xml",
"classpath:com/xxx/aaa/common-config.xml",
"classpath:com/xxx/aaa/applicationContext-dao.xml",})
package jp.co.sej.ssc.sh.sshb0051; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import jp.co.sej.ssc.Application; import jp.co.sej.ssc.cl.sclb1040.Sclb1041; import jp.co.sej.ssc.common.CommonFunction; import jp.co.sej.ssc.sh.sshb0051.common.DbUnitUtils; import jp.co.sej.ssc.sh.sshb0051.common.DbUtils; import jp.co.sej.ssc.sh.sshb0051.common.TestConfig; import jp.co.sej.ssc.sh.sshb0051.constants.TestCommonConst; import org.apache.logging.log4j.ThreadContext; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.batch.test.context.SpringBatchTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; /** * Sshb0051Test08クラス.<br> */ @SpringBatchTest @ContextConfiguration(classes = { Application.class, Sshb0051JobConfig.class, TestConfig.class }) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @ExtendWith(MockitoExtension.class) @ExtendWith(SystemStubsExtension.class) @ComponentScan(basePackages = "common") //@Import(Sclb1041.class) class Sshb0051Test08 { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Autowired private JdbcTemplate jdbcTemplate; /** * テスト準備。 * * @throws Exception 例外 */ @BeforeEach void setUpData() throws Exception { } @AfterEach void tearDownData() throws Exception { // Close Database Connection DbUtils.closeConnection(); } @Test @DisplayName("Test01:Sshb0051Test01") void test01() throws Exception { // ネットギフト情報取込リストの受付日時 < 変数.システム日付 insert DbUnitUtils.importTablesCsv(new File(TestCommonConst.FILE_PATH + "/sshb0051/inputdb/data8/")); ThreadContext.put(CommonFunction.TASKID, "TASKID"); // Job実行 JobExecution jeResult = jobLauncherTestUtils.launchJob(); // 実行結果確認 assertEquals("COMPLETED", jeResult.getExitStatus().getExitCode()); // 実施後結果チェック DbUnitUtils.checkMasterMaxCsv(TestCommonConst.FILE_PATH, "sshb0051/test08output", "t_electronic_pos_journal_time_management"); } } 我现在的测试代码是这样的
最新发布
10-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值