I am trying to test CRUD methods with JUnit using Spring framework. The code below works perfectly
@Transactional
public class TestJdbcDaoImpl {
@SuppressWarnings("deprecation")
@BeforeClass
@Test
public static void setUpBeforeClass() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
JdbcDaoImpl dao = ctx.getBean("jdbcDaoImpl",JdbcDaoImpl.class);
Circle cicle = new Circle();
dao.insertCircle(new Circle(6,"e"));
}}
However, following code with @RunWith(SpringJUnit4ClassRunner.class) gives me an error
**Jun 11, 2014 1:00:15 PM org.springframework.test.context.TestContextManager retrieveTestExecutionListeners INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] **
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring.xml")
@Transactional
public class TestJdbcDaoImpl {
@Autowired
private static JdbcDaoImpl dao;
@SuppressWarnings("deprecation")
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Circle cicle = new Circle();
dao.insertCircle(new Circle(10,"e"));
}
There are at least two problems in your test
1) You can not use the standard ServletContext in a test. Either you use a part of the Spring configuration that only uses Beans that not use the ServletContext, or you use the Spring-MVC-Test support (available since Spring 3.2)
2) @BeforeClass
run before the spring context ins loaded. Therefore use @Before
instead.
Example that try to fix both problems:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration // <-- enable WebApp Test Support
@ContextConfiguration("/spring.xml")
@Transactional
public class TestJdbcDaoImpl {
@Autowired
private static JdbcDaoImpl dao;
@SuppressWarnings("deprecation")
@Before // <-- Before instead of BeforeClass
public static void setUpBeforeClass() throws Exception {
Circle cicle = new Circle();
dao.insertCircle(new Circle(10,"e"));
}
WebAppConfiguration should only be used if you want to load a WebApplicationContext. In case of a simple unit test (i.e. not an integration test), you should not rely on this annotation.
The error message is pretty clear: either specify custom listener classes or make the default listener classes (and their required dependencies) available. In other words, either specify the following:
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })
as defined in org.springframework.test.context.TestContextManager:
private static final String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES = new String[] {
"org.springframework.test.context.web.ServletTestExecutionListener",
"org.springframework.test.context.support.DependencyInjectionTestExecutionListener",
"org.springframework.test.context.support.DirtiesContextTestExecutionListener",
"org.springframework.test.context.transaction.TransactionalTestExecutionListener" };
so that ServletTestExecutionListener is not loaded anymore, or add the following dependency: javax.servlet-api
I had this problem, and the @TestExecutionListener suggestion didn't work for me. I then updated my junit dependency (4.8.2->4.12), and that resolved the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With