zoukankan      html  css  js  c++  java
  • Maven简单的配置Junit测试及使用简单的mock

    1、maven依赖配置如下

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>1.9.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
       <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>RunJob</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>assembly</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
            </plugins>
        </build>

    2、编写测试代码

    public class SimpleTest {
        @Test
        public void test() {
            Assert.assertTrue("a".equals("a"));
        }
    
        @Test
        public void testMock() {
            List m = mock(List.class);
            m.add(1);
            m.clear();
    
            verify(m).add(1);//验证执行过add(1)操作
            verify(m).clear();//验证执行过clear操作
        }
    
        @Test
        public void when_thenReturn() {
            //mock一个Iterator类
            Iterator iterator = mock(Iterator.class);
            //预设当iterator调用next()时第一次返回hello,第n次都返回world
            when(iterator.next()).thenReturn("hello").thenReturn("world");
            //使用mock的对象
            String result = iterator.next() + " " + iterator.next() + " " + iterator.next();
            //验证结果
            assertEquals("hello world world", result);
        }
    }
  • 相关阅读:
    30-语言入门-30-分数加减法
    29-语言入门-29-两点距离
    bootstrapcss3触屏滑块轮播图
    input输入样式,动画
    HTML5夜空烟花绽放动画效果
    精美留言、评论框,带微博表情
    Sublime Text 3汉化中文版
    直播英国脱欧各国反应?谁将是最大赢家?
    品牌关键字的重要性?是什么呢
    网站收录之网络推广
  • 原文地址:https://www.cnblogs.com/maxiaofang/p/6608081.html
Copyright © 2011-2022 走看看