美文网首页
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引发

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