【问题标题】:Inconvertible types; cannot cast 'io.cucumber.java.Scenario' to 'cucumber.runtime.ScenarioImpl'不可转换的类型;无法将“io.cucumber.java.Scenario”转换为“cucumber.runtime.ScenarioImpl”
【发布时间】:2020-06-25 17:57:42
【问题描述】:

我已将 Cucumber 版本更新到 5.4.2,但由于出现 Inconvertible types; cannot cast 'io.cucumber.java.Scenario' to 'cucumber.runtime.ScenarioImpl' 错误,这段代码停止工作。

  Field field = FieldUtils.getField((scenario).getClass(), "stepResults", true); 

有什么办法可以让它恢复工作?

这是this SO post的整段代码

private static String logError(Scenario scenario) {

    Field field = FieldUtils.getField((scenario).getClass(), "stepResults", true);

    if (field != null) {
        field.setAccessible(true);
        try {
            ArrayList<Result> results = (ArrayList<Result>) field.get(scenario);
            for (Result result : results) {
                if (result.getErrorMessage() != null)
                    if (result.getErrorMessage().length() >= 10000) {
                        return FAILED_COMMENT + "\n" + result.getErrorMessage().substring(0, 10000);
                    } else {
                        return FAILED_COMMENT + "\n" + result.getErrorMessage();
                    }
            }
        } catch (Exception e) {
            return FAILED_COMMENT;
        }
    }

    return FAILED_COMMENT;
}

非常感谢。

【问题讨论】:

    标签: cucumber scenarios


    【解决方案1】:

    通过使用反射进入框架内部,您依赖于实现细节。这是一种不好的做法,当框架更改其实现时,您的代码可能会中断。

    Cucumber 中的 Hooks 旨在在场景之前和之后操纵测试执行上下文。他们不会报告测试执行本身。报告是跨领域的关注点,最好使用插件系统进行管理。

    例如:

    package com.example;
    
    import io.cucumber.plugin.ConcurrentEventListener;
    import io.cucumber.plugin.event.EventPublisher;
    import io.cucumber.plugin.event.Result;
    import io.cucumber.plugin.event.Status;
    import io.cucumber.plugin.event.TestCase;
    import io.cucumber.plugin.event.TestCaseFinished;
    
    public class MyTestListener implements ConcurrentEventListener {
        @Override
        public void setEventPublisher(EventPublisher publisher) {
            publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
        }
    
        private void handleTestCaseFinished(TestCaseFinished event) {
            TestCase testCase = event.getTestCase();
            Result result = event.getResult();
            Status status = result.getStatus();
            Throwable error = result.getError();
            String scenarioName = testCase.getName();
            String id = "" + testCase.getUri() + testCase.getLine();
            System.out.println("Testcase " + id + " - " + status.name());
        }
    }
    

    使用 JUnit 4 和 TestNG 时,您可以使用以下方式激活此插件:

    @CucumberOptions(plugin="com.example.MyTestListener")
    

    使用 JUnit 5,您可以将其添加到 junit-platform.properties:

    cucumber.plugin=com.example.MyTestListener 
    

    或者如果您使用的是 CLI

    --plugin com.example.MyTestListener 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多