java Spring:JUnit-Test:applicationContext 未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1356312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:14:26  来源:igfitidea点击:

Spring: JUnit-Test: applicationContext is not loaded

javaunit-testingspringjunit

提问by swalkner

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?

我有以下测试类,但我设置为“ContextConfiguration-locations”并不重要 - 它从未设置我的 UserService。当我在locations-property中设置一个不存在的文件时它不会抛出任何错误......我做错了什么?

@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-Version: 2.5.6.SEC01

弹簧版本:2.5.6.SEC01

JUnit-Version: 4.5

JUnit 版本:4.5

JDK: 1.5

JDK:1.5

回答by Robert Munteanu

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 .

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

回答by Dech

I havn't test it yet, but if you really want to upgrade to spring 3.0, you can use the ehcache-spring-annotations framework.

我还没有测试过,但如果你真的想升级到 spring 3.0,你可以使用ehcache-spring-annotations 框架