尝试在Spring项目中使用maven和junit删除测试数据

时间:2013-01-30 15:09:16

标签: java spring hibernate spring-mvc junit

我正在开发一个spring项目,我正在写junit测试用例来删除我的一些测试数据,但是我收到了以下错误..

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined

我的junit测试如下所示:

public class TestRemoveTestData implements BeanFactoryAware {

    static boolean functionReturn;

    String user = "xxxx@aol.com.dev";

    private BeanFactory beanFactory;

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;

    }

    @Autowired
    private SessionFactory sessionFactory;


    @Test
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED)
    public void RemoveEnrollmentRecord()
    {
        Session mySession = sessionFactory.getCurrentSession();
        //
        // Get profile record based on email address
        //
        Profile myProfile = (Profile) mySession.createCriteria(Profile.class)
                .add(Restrictions.like("email", user)).uniqueResult();
        //
        // Start to create a new Enrollment Record with a status of "U"
        //
        Enrollment newEnrollment = new Enrollment();
        newEnrollment.setProfile_id(myProfile.getProfile_id());
        newEnrollment.setContact_id(myProfile.getContact_id());
        mySession.delete(newEnrollment);
        mySession.flush();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要加载配置文件。例如,在我的JPA& amp; spring security,我的配置文件是applicationContext.xml,applicationContext-security.xml,applicationContext-jpa.xml。您可以按以下方式加载它们:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/META-INF/spring/applicationContext*.xml"})
public class TestRemoveTestData implements BeanFactoryAware {
 ...

}

注意:我使用通配符'*'加载我的所有配置文件。但您可以在locations = {“file1”,“file2”,...}

中列出它们