我无法使用@RunWith(SpringJUnit4ClassRunner.class)&运行我的DAOImpl类的测试。获得以下异常: 没有这样的方法例外。 请参考以下eclipse screeshot中的图像:
我怀疑这可能是因为存放在错误位置的存储库-test.xml配置文件& spring无法从@ContextConfiguration注释&的类路径位置中选择它。从此文件加载配置。 有人可以告诉我,我是否正确地将配置测试文件放在项目下?
以下是位于“WebContent / WEB-INF / spring /”
下的repository-test.xml <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="br.com.braziljs.loiane" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost/braziljs</value></property>
<property name="username"><value>root</value></property>
<!-- <property name="password"><value>root</value></property>-->
</bean>
<!-- Configures Hibernate - Database Config --> <!-- import resource="db-config.xml" /> -->
</beans>
这是Spring Test课程:
package br.com.braziljs.loiane.test;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
//import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import br.com.braziljs.loiane.dao.DeviceDAO;
import br.com.braziljs.loiane.model.MIDevice;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("/WEB-INF/spring/repository-test.xml")
public class TestDeviceDao {
/* @Autowired
private WebApplicationContext webApplicationContext;*/
@Before
public void setUp() {
// this.deviceRepository = new DeviceDAO(ds);
System.out.println("inside setup");
}
/*
@Autowired
private DeviceDAO deviceRepository;
@Autowired
private DataSource ds;
*/
@Test
public void test() {
// when(hibernateTemplate.get(Book.class, 1L)).thenReturn(new Book());
// final MIDevice device = deviceRepository.findByDeviceId(1L);
// verify(hibernateTemplate, times(1)).get(Book.class, 1L);
// assertNotNull(device);
// System.out.println(device);
System.out.println("inside setup");
}
}