默认构造函数无法处理引发的异常类型ioexception

ulmd4ohb  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(306)

抱歉,如果这已经回答之前,但我已经看了,找不到任何有助于我。当我试图运行以下代码时,标题中出现了错误。有人知道什么显式构造函数可以用来修复这个错误吗?

public class HomePageTest extends TestBase{

    LoginPage loginPage;
    HomePage homePage;
    ShopByDepartmentPage shopByDepartmentPage;
    JamiesAmazonPage jamiesAmazonPage;
    TodaysDealsPage todaysDealsPage;
    MyAccountPage myAccountPage;
    BasketPage basketPage;

    public HomePageTest() throws IOException {
        super();
    }

    @BeforeMethod
    public void setUp() throws IOException {
        initialization();
        loginPage = new LoginPage();
        shopByDepartmentPage = new ShopByDepartmentPage();
        jamiesAmazonPage = new JamiesAmazonPage();
        todaysDealsPage = new TodaysDealsPage();
        myAccountPage = new MyAccountPage();
        basketPage = new BasketPage();
        homePage = loginPage.login(prop.getProperty("username"), 
        prop.getProperty("password"));
    }
}

我的测试基本代码是:

public static WebDriver driver;
public static Properties prop;

public TestBase() throws IOException {

    try {
        prop = new Properties();
        FileInputStream fis = new FileInputStream("D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Config\\config.properties");
        prop.load(fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void initialization() {

    if (prop.getProperty("browser").equals("firefox"))
    {
        System.setProperty("webdriver.gecko.driver", "D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Resources\\geckodriver.exe");
        driver = new FirefoxDriver();
    }
    else if (prop.getProperty("browser").equals("chrome"))
    {
        System.setProperty("webdriver.chrome.driver", "D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Resources\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);

    driver.get(prop.getProperty("url"));
}

完整堆栈跟踪代码如下:

FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.Error: Unresolved compilation problem: 
Default constructor cannot handle exception type IOException thrown by 
implicit super constructor. Must define an explicit constructor

at Pages.ShopByDepartmentPage.<init>(ShopByDepartmentPage.java:5)
at TestCases.HomePageTest.setUp(HomePageTest.java:38)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

当我尝试将@before方法放入如下的try catch中时,仍然会收到相同的错误消息:

@BeforeMethod
public void setUp() {
    initialization();
    try {
        loginPage = new LoginPage();
        shopByDepartmentPage = new ShopByDepartmentPage();
        jamiesAmazonPage = new JamiesAmazonPage();
        todaysDealsPage = new TodaysDealsPage();
        myAccountPage = new MyAccountPage();
        basketPage = new BasketPage();
        homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
    }
    catch (Exception e) {
        e.printStackTrace();
    }

ShopByDepartment页面代码:

public class ShopByDepartmentPage extends TestBase{

@FindBy(xpath="//tr//td[1]//div[1]//ul[1]//li[1]")
WebElement videos;

@FindBy(xpath="//li[@class='nav_first']//a[@class='nav_a'][contains(text(),'Books')]")
WebElement books;

@FindBy(xpath="//li[@class='nav_first']//a[@class='nav_a'][contains(text(),'Amazon Music Unlimited')]")
WebElement music;

@FindBy(xpath="//tr//td[2]//div[4]//ul[1]//li[6]//a[1]")
WebElement computerGames;
nhjlsmyf

nhjlsmyf1#

听起来您的单元测试框架无法处理 Exception 从构造函数中抛出。
最明显的建议是-不要扔垃圾 Exception . 如果框架不允许这样做,可能是出于一个很好的原因,即它可能不会显示 Exception 以一种好的方式。
如果您真的想在构造函数中执行此逻辑,另一种方法是重新抛出 IOException 作为一个 ExceptionInInitializerError ,这是未检查的 Exception .

roejwanj

roejwanj2#

报告的直接错误是 Unresolved compilation problem 提示:这种类型的错误可以由eclipse编译器编译的字节码产生,eclipse编译器执行增量编译。
似乎在某个时候,您的派生类中没有显式的默认构造函数,并且您的ide(可能是eclipse,但可能是另一个使用eclipse编译器的ide)和/或构建系统此后无法重新编译该类,因此您仍然在运行旧的字节码。
试着做一个干净的构建,并注意其他编译错误。如果您有一个合适的构建系统,比如maven或gradle,请关闭ide并使用它运行一个干净的构建。构建系统有可能恢复 .class ide动态生成的文件。

相关问题