3

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;
}
}

Spring-Version: 2.5.6.SEC01

JUnit-Version: 4.5

JDK: 1.5

4
  • When using @Autowired, you don't need a setter for the property, and I don't see why you want to have a public getter for it either. Commented Aug 31, 2009 at 8:09
  • I removed getter/setter and included the setup()-method. bizzarely, it seems like the setup()-method is never called... I put a Assert.assertNotNull(null) in there and a debug-breakpoint, but it never gets there... Commented Aug 31, 2009 at 8:57
  • Did you annotate the setup() with the @Before annotation? And you don't get any Spring injection errors? How is your UserService bean declared, can you post that too? Commented Sep 6, 2009 at 7:09
  • If you use @Autowired, Spring does not need any method access to the property, it can access the private directly. Commented Oct 26, 2010 at 12:21

2 Answers 2

5

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 .

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Note that code.google.com/p/ehcache-spring-annotations also works with Spring 2.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.