Reputation: 33
I like to test my spring code:
@ContextConfiguration(locations = { "/applicationContext.xml" })
@Transactional()
public class Test {
@Autowired
MyDao dao;
@org.junit.Test
@Rollback(false)
public void testSomething() throws Exception {
MyEntity e = new MyEntity();
dao.create(e);
}
}
Running this test with eclipse (as an JUNIT test) just gives an Nullpointer-Exception.
What have I done wrong? Thx!
Upvotes: 3
Views: 176
Reputation: 3724
You can add a Transactional test base class like this
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
public class IntegrateTestBase extends AbstractTransactionalJUnit4SpringContextTests {
}
Then wirite your test class
public class Test extends IntegrateTestBase {
@Autowired
MyDao dao;
@org.junit.Test
@Rollback(false)
public void testSomething() throws Exception {
MyEntity e = new MyEntity();
dao.create(e);
}
}
You need not write @ContextConfiguration
and @Transcational
in each test class
Upvotes: 2
Reputation: 3329
Just add @RunWith(SpringJUnit4ClassRunner.class)
to your class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
@Transactional()
public class Test {
@Autowired
MyDao dao;
@org.junit.Test
@Rollback(false)
public void testSomething() throws Exception {
MyEntity e = new MyEntity();
dao.create(e);
}
}
You need spring-test
for that.
Upvotes: 4