Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JUnit测试MyBatis Spring项目_Spring_Junit_Mybatis - Fatal编程技术网

JUnit测试MyBatis Spring项目

JUnit测试MyBatis Spring项目,spring,junit,mybatis,Spring,Junit,Mybatis,尝试JUnit测试MyBatis Spring项目时抛出一个IllegalStateException 问题似乎出在自动连线MyBatis映射器bean上(请原谅我的行话,因为我对MyBatis Spring JUnit的整个设置都是新手)。我从别人那里继承了这个设置。如果有任何其他信息需要帮助,请告诉我 当我将整个内容作为web服务公开时,下面描述的设置就会起作用。但是,当我尝试JUnit测试它时,它失败了。测试时如何自动连接映射器 安装程序 在main/java下 持久性文件夹:Produ

尝试JUnit测试MyBatis Spring项目时抛出一个
IllegalStateException

问题似乎出在自动连线MyBatis映射器bean上(请原谅我的行话,因为我对MyBatis Spring JUnit的整个设置都是新手)。我从别人那里继承了这个设置。如果有任何其他信息需要帮助,请告诉我

当我将整个内容作为web服务公开时,下面描述的设置就会起作用。但是,当我尝试JUnit测试它时,它失败了。测试时如何自动连接映射器

安装程序 在main/java下
  • 持久性文件夹:ProductMapper.java
  • 服务文件夹:ProductService.java
  • ws文件夹:browservice.java
  • ws/impl文件夹:browserviceimpl.java
在主要/资源项下
  • 持久性文件夹:ProductMapper.xml
BrowserService.java ProductService.java Context.xml 编辑 请参见上面更新的
Context.xml
中的数据源Bean。配置看起来正确吗

看起来一个问题是数据源设置。我之所以这样假设,是因为JUnit测试运行,但抛出了一个异常。因此,最有可能的不是JUnit设置。如果数据源设置正确,MyBatis将成功实例化映射程序Bean,自动连接将成功

测试文件
它发现
Context.xml
文件没有任何问题,因为我把它放在了正确的目录中。

请提供单元测试的代码

我想您缺少spring配置

@ContextConfiguration(locations = { 
        "classpath:/com/comp/team/proj/context.xml" // give the correct path 
        })
@RunWith(SpringJUnit4ClassRunner.class)
public class BrowserServiceTest {

@Autowired
BrowserServiceImpl browserService;

@Test
public void shouldTestSmth(){

}
请记住,在测试过程中,Spring使用Spring测试库

编辑: 我想最重要的部分还没有显示出来。在context.xml中是否有ProductMapper的bean定义?我想它不见了


我还可以看到您混合了注释和bean定义。也许您需要bean定义?

请提供单元测试的代码

我想您缺少spring配置

@ContextConfiguration(locations = { 
        "classpath:/com/comp/team/proj/context.xml" // give the correct path 
        })
@RunWith(SpringJUnit4ClassRunner.class)
public class BrowserServiceTest {

@Autowired
BrowserServiceImpl browserService;

@Test
public void shouldTestSmth(){

}
请记住,在测试过程中,Spring使用Spring测试库

编辑: 我想最重要的部分还没有显示出来。在context.xml中是否有ProductMapper的bean定义?我想它不见了


我还可以看到您混合了注释和bean定义。也许您需要bean定义?

正确数据源配置

我发现有另一种方法可以实现映射bean

<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="com.comp.team.proj.persistence.ProductMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>


我认为引发非法状态异常是因为:


数据源配置不正确,导致映射失败,导致未创建映射程序bean(
NoSuchBeanDefinitionException
)。

更正数据源配置

我发现有另一种方法可以实现映射bean

<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="com.comp.team.proj.persistence.ProductMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>


我认为引发非法状态异常是因为:


数据源配置不正确,导致映射失败,导致未创建映射器bean(
NoSuchBeanDefinitionException
).

测试代码在哪里..您还需要从测试类配置自动连接..并且需要使用诸如runwithjunit4class和contextconfiguration(“locationof dispatcherservlet.xml”)之类的注释。如果您正在为使用自动连线的类进行测试,则应使用pawelccb建议的代码。。如果您不提供pawelccb提供的注释,则通常会出现错误。。问题是,如果我们不提供dispatcherservlet.xml的路径,则测试类不知道自动连线问题,组件扫描在该路径中是关键识别自动布线类..测试代码在哪里..您还需要从测试类配置自动布线..并且需要使用诸如runwithjunit4class和contextconfiguration(“locationof dispatcherservlet.xml”)之类的注释。如果您正在为使用自动连线的类进行测试,则应使用pawelccb建议的代码。。如果您不提供pawelccb提供的注释,则通常会出现错误。。问题是,如果我们不提供dispatcherservlet.xml的路径,则测试类不知道自动连线问题,组件扫描在该路径中是关键识别自动布线类..请参见编辑。我添加了
@RunWidth
注释。原来问题出在数据源配置上。请参阅编辑。我添加了
@RunWidth
注释。原来问题出在数据源配置上。
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductCountTest {

    @Autowired
    protected BrowserService browserSvc;

    @Test
    public void testGetProductCount() {
        long count = browserSvc.getProductCount();

        assertTrue(count > 0);
    }

}
@ContextConfiguration(locations = { 
        "classpath:/com/comp/team/proj/context.xml" // give the correct path 
        })
@RunWith(SpringJUnit4ClassRunner.class)
public class BrowserServiceTest {

@Autowired
BrowserServiceImpl browserService;

@Test
public void shouldTestSmth(){

}
<bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="com.comp.team.proj.persistence.ProductMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>