现在的未来的
2017-08-03 02:14
采纳率: 50%
浏览 3.6k
已采纳

关于Junit结合spring测试加载bean的问题

问题一:
public class text
{
private static final Logger logger = LoggerFactory.getLogger(text.class);
@Autowired
private NumBasicJob numBasicJob;
@Test
public void insert()
{
ApplicationContext act=new ClassPathXmlApplicationContext("spring.xml");
NumBasicJob p=(NumBasicJob) act.getBean("numBasicJob");
p.ExecuteOcfJob(text.class);
//numBasicJob.ExecuteOcfJob(text.class);
}
}
我代码全部使用注解,加载spring并没有报错,但是获取不到bean,也就是p为空,我认为可能是 ApplicationContext act=new ClassPathXmlApplicationContext("spring.xml
");不支持注解,不知道对不对?
问题2:
public class text extends BaseJunit4Test
{
private static final Logger logger = LoggerFactory.getLogger(text.class);
@Autowired
private NumBasicJob numBasicJob;
@Test
public void insert()
{
/* ApplicationContext act=new ClassPathXmlApplicationContext("spring.xml");
NumBasicJob p=(NumBasicJob) act.getBean("numBasicJob");
p.ExecuteOcfJob(text.class);*/
numBasicJob.ExecuteOcfJob(text.class);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"classpath:spring.xml"
})
public class BaseJunit4Test {
private static final Logger LOG_RECORD=Logger.getLogger(OcfProductBasicJob.class);
@Before
public void init() {
//在运行测试之前的业务代码
LOG_RECORD.info("start to execute task," + new Date().toString());
}
@After
public void after() {
//在测试完成之后的业务代码
LOG_RECORD.info("end to execute task," + new Date().toString());
}
}
按道理说,我使用junit结合spring进行测试的时候,numBasicJob不应该为空,因为这个支持注解。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题