Mockito进行有效的Junit测试失败

时间:2018-02-26 09:38:38

标签: java junit mockito

我有一个测试类测试在向命令行参数添加无效术语时是否抛出ParseExeption(或者如果缺少某些参数)。如果没有将Mockito的软件包安装到项目中,测试就会通过。一旦我通过gradle安装Mockito,测试就会失败并显示以下消息。

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V

at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at org.junit.rules.ExpectedException.handleException(ExpectedException.java:252)
at org.junit.rules.ExpectedException.access$000(ExpectedException.java:106)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:241)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

测试代码:

import org.apache.commons.cli.ParseException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.File;


public class CliTest {

    private File testFile = new File(System.getProperty("user.dir")
            + File.separator
            + "src/test/resources/xsbf161.r1r2.43bp.breakseq.excl.Xsam");

    private File validRangeFile = new File(System.getProperty("user.dir")
            + File.separator
            + "src/test/resources/60XMF009-1-UX.csv");

    private File validRangeFile2 = new File(System.getProperty("user.dir")
            + File.separator
            + "src/test/resources/hellod.csv");

    Cli cli;

    @Before
    public void setup(){

    }

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void noInputFilethrowsException() throws ParseException{
        String[] args = {"--st1=PPE....,XSam"};

        thrown.expect(ParseException.class);
        thrown.expectMessage("No input file");

        cli = new Cli(args);
        cli.parse();
    }

    @Test
    public void invalidFileThrowsParseException() throws ParseException{
        String[] args = {"--if=doesntexist.Xsam","--st1=PPE....,XSam"};
        thrown.expect(ParseException.class);
        thrown.expectMessage("Input file does not exist");

        cli = new Cli(args);
        cli.parse();
    }

    @Test
    public void noStreamsThrowsParseException() throws ParseException{
        String[] args = {String.format("--if=%s", testFile.toString())};

        thrown.expect(ParseException.class);
        thrown.expectMessage("No streams selected");

        cli = new Cli(args);
        cli.parse();

    }


}

gradle import order:

compile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/commons-cli/commons-cli
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
// https://mvnrepository.com/artifact/org.mockito/mockito-all
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'

0 个答案:

没有答案