本文介绍了春季:JUnit测试:applicationContext未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试类,但是我设置为"ContextConfiguration-locations"并不重要-它从不设置我的UserService.当我在locations-property中设置不存在的文件时,它不会引发任何错误...我在做什么错了?

I've got the following Test-class, but it doesn't matter what I set as "ContextConfiguration-locations" - it never set my UserService. And it doesn't throw any error when I set a non-existing file in the locations-property... what am I doing wrong?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml" })
public class BasicPersistenceTest {

@Autowired
private UserService userService;

@Test
public void testUserLogin() throws Exception {

    User result = userService.getUser("stefan", "walkner");

    Assert.assertNotNull(result);
}

@Test
public void testLogin() {
    User user = userService.getUser("stefan", "walkner");
    Assert.assertNull(user);
}

public UserService getUserService() {
    return userService;
}

public void setUserService(UserService userService) {
    this.userService = userService;
}
}

春季版本:2.5.6.SEC01

Spring-Version: 2.5.6.SEC01

JUnit版本:4.5

JUnit-Version: 4.5

JDK:1.5

推荐答案

我不知道您的测试为什么没有显示任何异常,而是 Spring 2.5与JUnit 4.5不兼容.要么移到Spring 3里程碑,要么将JUnit降级到4.4.

I don't know why your test does not show any exceptions, but Spring 2.5 is not compatible with JUnit 4.5. Either move to the Spring 3 milestones, or downgrade JUnit to 4.4 .

这篇关于春季:JUnit测试:applicationContext未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:00