Java错误:'抛出新的异常不受支持'

时间:2016-06-17 20:33:28

标签: java exception junit exception-handling throw

我环顾四周,如果工作与否,我会感到有点困惑。调试时的结果给我的印象是它失败了。

Statement 'throw new SessionTimeoutException("Session Timeout"); ' not supported

我正在Junit中运行一个测试用例并且已经使许多人使用了以前的例外,但是它们的实现方式与下面的方法相同。

private void checkSessionTimeout(HtmlPage page) throws SessionTimeoutException {

我是Exceptions的新手,并且不明白它为什么在测试期间只在运行时出错。

我的测试在if语句

中的方法中运行并失败
private void checkSessionTimeout(HtmlPage page) throws SessionTimeoutException {
    HtmlDivision imgDivElement = page.getFirstByXPath("//div[@class='border']");
    HtmlDivision errorDivElement = page.getFirstByXPath("//div[@class='error']");

    String imgUrl = imgDivElement.getFirstElementChild().getAttribute("src");
    String errorMessage = errorDivElement.getTextContent().trim();

    if(page.getWebResponse().getContentAsString().contains(imgUrl) && page.getWebResponse().getContentAsString().contains(errorMessage)){
        throw new SessionTimeoutException("Session Timeout");
    }
}

在这一行:抛出新的SessionTimeoutException("会话超时");

package com.cantShow;

import com.cantShow.htmlunit.html.HtmlPage;
import com.cantShow.TimeoutInformation;
import com.cantShow.AbstractScraperTest;
import com.cantShow.scrape.Scraper;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.cantShow.SessionTimeoutException;

public class ScraperTest extends ScraperTest<HtmlPage, SessionTimoutClassWithGettersSetters> {

@Override
protected Scraper<HtmlPage, SessionTimoutClassWithGettersSetters> getScraper() {
    return new Scraper();
}

@Test
public void scrape() throws Exception {
    SessionTimoutClassWithGettersSettersresult = testScraper("/scrape/timeout-session.html",
            "https://cantShow.html");

    details(result, "imgs/error.png","Your session has timed out. You will need to log back in to continue shopping.");
}

private static void details(SessionTimoutClassWithGettersS setterstimeoutInformation, String errorImgUrl, String errorMsg){
    assertEquals(errorImgUrl, sessionTimoutClassWithGettersSetters.getErrorImageUrl());
    assertEquals(errorMsg, sessionTimoutClassWithGettersSetters.getErrorMessage());
}
}

堆栈追踪:

com.cantShow.Scraper.checkSessionTimeout(Scraper.java:28)
atcom.cantShow.Scraper.Scraper.scrape(Scraper.java:16)
at com.cantShow.Scraper.Scraper.scrape(Scraper.j va:12)
at com.cantShow.Scraper.ScrapeService.scrapeString(ScrapeService.java:406)
at com.cantShow.Scraper.AbstractScraperTest.testScraper(AbstractScraperTest.java:32)
at com.cantShow.Scraper.ScraperTest.scrape(ScraperTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我已经查看了Stack Overflow上的其他帖子,但是找不到像Java这样的帖子。

1 个答案:

答案 0 :(得分:2)

如果要测试引发了异常,则必须捕获它并确认它是正确的异常。

              .orElseGet(() -> process(code));