5.0.0以降のio.cucumber.junit.Cucumber.javaでgetDescription()をオーバーライドする方法はありますか?

ジョンS:

既存のプロジェクトを3.0.2から5.5.0にアップグレードしようとしています。プロジェクトのCucumber.javaランナークラスにgetDescription()のカスタム実装があり、他のプロジェクトに依存関係があるため、機能を維持する必要があります。JUnitのバージョンは4.12です。

3.0.2では、Cucumber.javaランナークラスはcucumber.api.junit.Cucumberで次のように定義されています。

public class Cucumber extends ParentRunner<FeatureRunner>

これにより、カスタムランナーがかなり簡単になります。

public class DescriptiveCucumberRunner extends Cucumber
{
    public DescriptiveCucumberRunner(Class<?> testClass) throws InitializationError, IOException
    {
        super(testClass);
    }

    @Override
    public Description getDescription()
    {
        String text = "Cucumber tests running on " + new Date();
        Description originalDescription = super.getDescription();
        Description newDescription = originalDescription.childlessCopy();
        newDescription.addChild(Description.createSuiteDescription(text));

        for(Description childDescription : originalDescription.getChildren())
        {
            newDescription.getChildren().get(0).addChild(childDescription);
        }
        return newDescription;
    }
}

5.0.0では、Cucumber.javaランナークラスがio.cucumber.junit.Cucumberに移動され、定義が

public final class Cucumber extends ParentRunner<ParentRunner<?>>

だから私はもう延長することができません。

私はorg.junit.runners.ParentRunnerを拡張しようとしました

public class DescriptiveCucumberRunner extends ParentRunner<Cucumber>

デリゲートで集約しますが、Cucumber.javaのメソッドが保護されているため、基本的にクラス全体をコピーして現在の機能を保持する必要がありますが、これは実際にはまったくソリューションではありません。リフレクションを使用してCucumber.javaで定義されたメソッドを呼び出すこともできますが、それもあまりクリーンではないようです。

他のプロジェクトに変更を加えることなくこれを行う方法を知っている人はいますか?たぶん私が気付かないキュウリでそれを行うための意図された方法はありますか?うまくいけば、私はこれに関する明白な解決策を見逃しているだけですが、今のところ私は空白を描いています。

MPの使用:

通常、継承よりもコンポジションを使用することをお勧めします。そのため、既存のCucumberランナークラスを拡張するのではなく、ランナーのインスタンスをCucumber子の1つとして持つ独自のランナークラスを実装することを検討できます

package com.example;

import io.cucumber.junit.Cucumber;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.InitializationError;

import java.util.ArrayList;
import java.util.List;

public class MyCustomRunner extends ParentRunner<Cucumber> {

    private final List<Cucumber> children = new ArrayList<>(1);

    protected MyCustomRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
        this.children.add(new Cucumber(testClass));
    }

    @Override
    public Description getDescription() {
        return // your custom description;
    }

    @Override
    protected List<Cucumber> getChildren() {
        return children;
    }

    @Override
    protected Description describeChild(Cucumber child) {
        return child.getDescription();
    }

    @Override
    protected void runChild(Cucumber cucumber, RunNotifier runNotifier) {
        cucumber.run(runNotifier);
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ