views:

1828

answers:

4

Hi all,

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

A: 

Is your UserService working outside the test? Otherwise check the bean definition. Are you sure that UserService isn't injected? Add this to your test:

@Before
public void setup() {
    Assert.assertNotNull("UserService not set", userService);
}

Otherwise it must be the classpath that is wrong.

crunchdog
A: 

please remove those getter/setters and try to make your testclass extending spring test classes, e.g. as following.

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

hope it will help.

Kent
+2  A: 

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 .

Robert Munteanu
This bit me too
Paul McKenzie
A: 

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

Dech