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

Java错误:';不支持抛出新异常';,java,exception,junit,exception-handling,throw,Java,Exception,Junit,Exception Handling,Throw,我环顾四周,有点困惑它是否有效。调试时的结果给我的印象是它失败了 Statement 'throw new SessionTimeoutException("Session Timeout"); ' not supported 我正在Junit中运行一个测试用例,以前已经使用过很多异常,但是它们是以与下面的方法相同的方式实现的 private void checkSessionTimeout(HtmlPage page) throws SessionTimeoutException { 我不

我环顾四周,有点困惑它是否有效。调试时的结果给我的印象是它失败了

Statement 'throw new SessionTimeoutException("Session Timeout"); ' not supported
我正在Junit中运行一个测试用例,以前已经使用过很多异常,但是它们是以与下面的方法相同的方式实现的

private void checkSessionTimeout(HtmlPage page) throws SessionTimeoutException {
我不熟悉异常,不理解为什么它只在测试期间的运行时出错

我的测试在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());
}
}

我看过其他关于堆栈溢出的文章,但是在Java中找不到类似的文章。

如果要测试抛出的异常,必须捕获它并验证它是正确的异常

@Test
public void scrape() throws Exception {
    boolean exceptionHappend = false;
    try {
        SessionTimoutClassWithGettersSettersresult = 
            testScraper("/scrape/timeoutsession.html", "https://cantShow.html");
    } catch (SessionTimeoutException e) {
        exceptionHappend = true;
    }

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

如果您想测试是否抛出了异常,您必须捕获它并验证它是否是正确的异常

@Test
public void scrape() throws Exception {
    boolean exceptionHappend = false;
    try {
        SessionTimoutClassWithGettersSettersresult = 
            testScraper("/scrape/timeoutsession.html", "https://cantShow.html");
    } catch (SessionTimeoutException e) {
        exceptionHappend = true;
    }

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

我不明白你在问什么。那是什么方法?它是测试的一部分,还是测试验证的生产代码?你期望发生什么,确切地说,会发生什么?测试验证的生产代码我希望它抛出异常,因为它是真的。但是我不确定它是否工作,因为它失败并且测试应该通过什么是
SessionTimeoutException
的导入?它是否在junit测试的类路径中?如果测试失败,是否存在堆栈跟踪?您的代码在
checkSessionTimeout
中引发异常,但如果未在
ScrapeService.scrapeString
中处理或在
AbstractScraperTest.testScraper
中捕获,则它将在测试中引发异常,您的测试将失败。我不明白您在问什么。那是什么方法?它是测试的一部分,还是测试验证的生产代码?你期望发生什么,确切地说,会发生什么?测试验证的生产代码我希望它抛出异常,因为它是真的。但是我不确定它是否工作,因为它失败并且测试应该通过什么是
SessionTimeoutException
的导入?它是否在junit测试的类路径中?如果测试失败,是否存在堆栈跟踪?您的代码在
checkSessionTimeout
中引发异常,但如果它未在
ScrapeService.scrapeString
中处理或在
AbstractScraperTest.testScraper
中捕获,则它将在测试中引发异常,您的测试将失败。