Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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
Java JUnit“;“预期”;不';好像不行_Java_Exception_Testing_Junit - Fatal编程技术网

Java JUnit“;“预期”;不';好像不行

Java JUnit“;“预期”;不';好像不行,java,exception,testing,junit,Java,Exception,Testing,Junit,我有一个wierd问题,我正在尝试测试一个方法,并期望它抛出java.util.NoSuchElementException。以下是我的测试代码: @Test(expected = NoSuchElementException.class) public void testLineWithoutCommas(){ String lineToTest = "fdgsdgfdfMarkOwenmark@mark.com"; List<String> linesTo

我有一个wierd问题,我正在尝试测试一个方法,并期望它抛出java.util.NoSuchElementException。以下是我的测试代码:

@Test(expected = NoSuchElementException.class)
    public void testLineWithoutCommas(){
    String lineToTest = "fdgsdgfdfMarkOwenmark@mark.com";
    List<String> linesToTest = new ArrayList<String>();
    linesToTest.add(lineToTest);

    applicationChecker = new ApplicationChecker();
    applicationChecker.getLinesFromFile(linesToTest);
    applicationChecker.getDetailsFromLine(applicationChecker.getLines().get(0));
}
最后是jUnit:

java.lang.AssertionError: Expected exception: java.util.NoSuchElementException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
and blah blah blah...

你知道怎么了吗?谢谢您的帮助。

检查您是否没有捕获和处理代码中的NoTouchElementException。第一个stacktrace可能是由于您的代码捕获异常,然后记录它,并将其作为其他对象抛出,或者只是捕获它而根本不抛出它。“expected”只捕获从测试本身抛出的异常,它不处理异常抛出,并在代码中部分处理。

试试看

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

@Test
public void testLineWithoutCommas(){

exc.expect(NoSuchElementException.class);
// method have exception

}

如何获得两个堆栈跟踪?我希望看到一个或另一个,但不是两个。没错,对吗?我不知道这有什么问题……卢卡斯,他的意思是——“你是怎么弄到的?”。不是“不是很奇怪”这就是我想弄明白的。我不知道。据我所知,这个测试应该可以正常工作,就这样。更具体地说,其中一个是控制台输出,另一个是jUnit故障跟踪
@Rule
public ExpectedException exc = ExpectedException.none();

@Test
public void testLineWithoutCommas(){

exc.expect(NoSuchElementException.class);
// method have exception