如何使用JUnit测试自动装配服务

时间:2014-12-20 11:44:54

标签: java spring junit autowired spring-annotations

如何仅使用spring-annotations

在测试类中自动装配服务

当我尝试时,我在下面遇到此错误,而在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:不能   autowire字段: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符合条件   autowire候选人这种依赖。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

我的服务类是

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("AHMED@A.com");


        userService.register(user);
    }



}

1 个答案:

答案 0 :(得分:5)

您是否尝试将测试本身作为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 {

你应该能够很好地注入你的服务。

希望它有所帮助。