junit无法注入service

滑山论贱 发布于 2013/12/01 21:53
阅读 11K+
收藏 2
import junit.framework.TestCase;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.com.sinosoft.domain.LoginInfo;
import cn.com.sinosoft.service.user.dao.LoginInfoDao;
import cn.com.sinosoft.service.user.service.LoginInfoService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:../../main/resources/applicationContext.xml"})
public class MyTest extends TestCase {
	@Resource
	private LoginInfoService loginInfoService;
//	@Resource
//	private LoginInfoDao<LoginInfo> loginInfoDao;
	
	@Test
    public void testA(){
        System.out.println("AAAAAAAAAAAAAAAA");
    }
	
//	@Test
	public void testaddLogin(){
		System.out.println(System.getProperty("user.dir"));
		LoginInfo login=new LoginInfo();
		login.setLoginName("11111111@qq.com");
		login.setPassword("123456");
		//loginInfoService.addLoginInfo(login);
	}
	
//	public void testGetLogin(){
//		LoginInfo logininfo=loginInfoService.findLoginInfoByName("11111111@qq.com");
//		if(logininfo!=null)
//			System.out.println(logininfo.getPassword()+"================");
//	}
	
}
  1. 我们项目都是零配置的,
    @Service("loginInfoService")
    public class LoginInfoServiceImpl implements LoginInfoService{

    service impl已经注入了,但是junit测试怎么就是无法注入?难道我applicationContext.xml没有加载?

  2. 我把上面的ContextConfiguration改为 @ContextConfiguration(locations = {"classpath:applicationContext.xml"})和maven一起启动能插入。

但是没有报加载不到啊?请大师指导一下。谢谢了。

 

 


加载中
0
终曲
终曲

测试类加注解,配置文件的路径最好还是用classpath

@TestExecutionListeners( { TransactionalTestExecutionListener.class,

DependencyInjectionTestExecutionListener.class })

一个是注入,一个是事务管理

所有这些注解一般加在单元测试的基类上,然后各子类里面在需要走事务的方法上加@Transactional来使数据库写操作回滚

0
滑山论贱
滑山论贱
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.sinosoft.service.user.service.LoginInfoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.sinosoft.service.user.service.LoginInfoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
	... 26 more

这个是报错

0
终曲
终曲
把log4j的改到info级别输出,看具体到哪步出问题,反正我当时这么排查的,你的配置应该没错,可能哪里粗心写错字母之类的吧
0
j
just4scala

报的错是:org.springframework.beans.factory.NoSuchBeanDefinitionException

那就应该是配置文件的问题了,applicationContext.xml中有component-scan么?

贴出来看下

0
滑山论贱
滑山论贱
解决了,没有更新svn,maven配置的包冲突了,。。。。哎
滑山论贱
滑山论贱
回复 @醉生梦死丶 : 我是包有冲突,你看看包有没有冲突了
醉生梦死丶
醉生梦死丶
你好,我的项目也是springmvc零配置的,用junit4测试也出现这种情况。但是我没有用maven,请问怎么解决
0
四火
四火
怎么解决的?详细说说我的没有冲突啊
0
满风
满风

哪个包?和哪个包冲突了??

返回顶部
顶部