美文网首页
maven 添加依赖有test引发

maven 添加依赖有test引发

作者: 小明今晚加班 | 来源:发表于2019-07-19 18:04 被阅读0次

问题描述:
想通过mockito工具来实现模拟对象,我采取的操作是,首先在pom.xml文件中添加mockito依赖

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>

这个我是从maven responsitory中直接拷贝的,然后我就开始写我的mockito测试代码了,如下:

package com.example.demo.test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Iterator;
import java.util.List;

import org.junit.Test;
public class MockitoTest {
    @Test
    //验证行为
    public void verify_behaviour() {
        List<Integer> mock = mock(List.class);
        mock.add(1);
        mock.clear();
        //验证add(1)和clear()行为是否发生
        verify(mock).add(2);
        verify(mock).clear();
    }
    //验证测试的结果
    @Test
    public void testExpected() {
        Iterator<String> mock = mock(Iterator.class);
        //预期 当mock第一次调用next时返回“hello”,第二次调用时返回“world”
        org.mockito.Mockito.when(mock.next()).thenReturn("hello").thenReturn("world");
        String res = mock.next() + "==" + mock.next()+"=="+mock.next();
        System.out.println(res);
        assertThat(res, equalTo("hello==world==world"));
        
    }
}

然后出现的错误如下图所示:

The import org.mockito can not be rsolved
后经过查阅资料得知:博文link
image.png
因此,果断删除mockito依赖中的<scope>test</scope>,至此,代码不再报错,问题解决。

相关文章

  • maven 添加依赖有test引发

    问题描述:想通过mockito工具来实现模拟对象,我采取的操作是,首先在pom.xml文件中添加mockito依赖...

  • Maven Scope 作用说明

    Scope Maven中的scope有compile、test、runtime、provided、system,其...

  • springboot怎么打成war包

    1.添加spring-boot-starter-tomcat的依赖,scope provided2.添加maven...

  • Maven问题集

    一、maven如何不继承parent里面的部分依赖 子类重写,定义scope为test,打包则不会出现

  • 2020-03-17 maven scope

    maven 常用三个scope:compile,test,provided compile和test作用的程序结构...

  • ng-class

    在scope中定义变量(并不推荐) function Ctrl($scope){ $scope.test = ...

  • Maven依赖中scope的含义

    Maven依赖中scope的含义 整理一下Maven中Scope的详细作用,都是抄的别人内容整理了一下。参考:ht...

  • Maven依赖中scope的含义

    Maven依赖中scope的含义 整理一下Maven中Scope的详细作用,都是抄的别人内容整理了一下。参考:ht...

  • Maven Scope(依赖范围)小总结

    Maven Scope(依赖范围)小总结 先来下比较官方解释: 在Maven的世界中,有很多种classpath,...

  • Maven scope依赖范围

    一、Maven scope依赖范围概览 Maven的生命周期存在编译、测试、运行这些过程,那么显然有些依赖只用于测...

网友评论

      本文标题:maven 添加依赖有test引发

      本文链接:https://www.haomeiwen.com/subject/rcgslctx.html