Spring:JUnit测试:未加载applicationContext

3
我有以下测试类,但无论我将“ContextConfiguration-locations”设置为什么,它都不会设置我的UserService。而且当我在locations属性中设置不存在的文件时,它也不会抛出任何错误……我做错了什么?
@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;
}
}

Spring版本:2.5.6.SEC01

JUnit版本:4.5

JDK:1.5


使用@Autowired时,您不需要为属性设置setter,我也不明白为什么您想要一个公共getter。 - crunchdog
我移除了getter/setter并包含了setup()方法。奇怪的是,似乎从未调用setup()方法... 我在其中放置了Assert.assertNotNull(null)和一个调试断点,但它从未到达那里... - swalkner
你是否使用@Before注释来注解setup()方法了?并且没有收到任何Spring依赖注入错误吗?你的UserService bean是如何声明的?也可以发一下代码吗? - crunchdog
如果您使用@Autowired,Spring不需要任何方法访问属性,它可以直接访问私有属性。 - DwB
2个回答

5
我不知道为什么你的测试没有显示任何异常,但是Spring 2.5与JUnit 4.5不兼容。要么升级到Spring 3里程碑版本,要么将JUnit降级到4.4。

1

请注意,http://code.google.com/p/ehcache-spring-annotations/ 也适用于Spring 2.5。 - Eric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接