如何仅通过使用弹簧注释在测试类中自动装配服务
当我尝试以下错误时,在UserServiceImp类中使用@Service批注
2014-12-20 15:35:52错误TestContextManager:334-在允许TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5af97850]准备测试实例[com.amsb.bariz.base.test]时捕获了异常。 UserTest @ 4520ebad] org.springframework.beans.factory.BeanCreationException:创建名称为'com.amsb.bariz.base.test.UserTest'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:public com.amsb.bariz.base.service.UserService com.amsb.bariz.base.test.UserTest.userService; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有发现类型为[com.amsb.bariz.base.service.UserService]的合格Bean:期望至少有1个bean符合此依赖项的自动装配条件。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我的服务等级是
package com.amsb.bariz.base.service.imp;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.amsb.bariz.base.dao.UserDao;
import com.amsb.bariz.base.dao.UserRoleDao;
import com.amsb.bariz.base.entity.User;
import com.amsb.bariz.base.entity.UserRole;
import com.amsb.bariz.base.service.UserService;
@Service("userService")
public class UserServiceImp implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private UserRoleDao userRoleDao;
public void register(User user) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
user.setPassword(passwordEncoder.encode(user.getPassword()));
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
Date dateNow = new Date(20070266);
Timestamp dn = new Timestamp(now.getTime());
user.setStatus("P");
user.setCreated_on(dn);
user.setEnabled(false);
UserRole ur = new UserRole(user,"USER_ROLE");
System.out.println("XDXDX ::" + user.toString());
userDao.create(user);
userRoleDao.create(ur);
}
}
我的测试课程是:
package com.amsb.bariz.base.test;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import com.amsb.bariz.base.dao.UserDao;
import com.amsb.bariz.base.entity.User;
import com.amsb.bariz.base.entity.UserRole;
import com.amsb.bariz.base.service.UserService;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@Configuration
@ComponentScan(basePackages={"com.amsb.bariz.base.service"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-main.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
public class UserTest {
@Autowired
public UserService userService;
@Test
public void userAdd() {
User user = new User();
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
Timestamp doo = new Timestamp(now.getTime());
Date a = new Date(0);
user.setPassword("oman");
user.setName("oman new ");
user.setStatus("N");
user.setCreated_on(doo);
user.setUpdated_on(doo);
user.setDob(new Date(20140522));
user.setUsername("[email protected]");
userService.register(user);
}
}
您是否正在尝试将测试本身用作Spring配置的一部分?那是行不通的。您需要做的是:-从测试本身中删除@Configuration
和@ComponentScan
注释-创建一个简单的TestConfiguration
类:
@Configuration
@ComponentScan(basePackages={"com.amsb.bariz.base.service"})
@ImportResource("classpath:spring/spring-main.xml")
public class TestConfiguration{ }
并在测试中引用它:
@ContextConfiguration(classes = { TestConfiguration.class }, loader = AnnotationConfigContextLoader.class)
public class UserTest {
而且您应该能够很好地注入服务。
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句