Java 使用@RunWith(SpringJUnit4ClassRunner.class)运行SpringTest时,没有此类方法异常
我无法使用@RunWithSpringJUnit4ClassRunner.class为我的DAOImpl类运行测试&出现以下异常: 没有这样的方法例外。 请参考以下eclipse screeshot中的图像: 我怀疑这可能是因为repository-test.xml配置文件放置在错误的位置&spring无法从@ContextConfiguration注释的类路径位置中选择它&从该文件加载配置。 有人能告诉我我是否将配置测试文件正确地放在了项目下吗 下面是我的repository-test.xml,位于WebContent/WEB-INF/spring下/Java 使用@RunWith(SpringJUnit4ClassRunner.class)运行SpringTest时,没有此类方法异常,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,我无法使用@RunWithSpringJUnit4ClassRunner.class为我的DAOImpl类运行测试&出现以下异常: 没有这样的方法例外。 请参考以下eclipse screeshot中的图像: 我怀疑这可能是因为repository-test.xml配置文件放置在错误的位置&spring无法从@ContextConfiguration注释的类路径位置中选择它&从该文件加载配置。 有人能告诉我我是否将配置测试文件正确地放在了项目下吗 下面是我的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配置问题。确保pom.xml文件中的每个依赖项都在.classpath文件中,tacktrace&exception都在Eclipse附带的屏幕截图中。我怀疑这可能是由于classpath没有拾取文件
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");
}
}