我环顾四周,如果工作与否,我有点困惑。调试时的结果给我的印象是它失败了。
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(“Session Timeout”);
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这样的帖子。
如果要测试抛出Exception,则必须捕获它并验证它是否是正确的Exception。
@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.");
}