All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.test4j.module.spec.internal.ScenarioThen Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package org.test4j.module.spec.internal;

import org.test4j.functions.ReturnExecutor;
import org.test4j.functions.SExecutor;

import java.util.function.Consumer;

/**
 * ScenarioThen
 *
 * @author wudarui
 */
@SuppressWarnings({"unchecked"})
public class ScenarioThen implements IThen {
    private final ScenarioResult scenario;

    ScenarioThen(ScenarioResult scenario) {
        this.scenario = scenario;
    }

    @Override
    public IThen then(String description, SExecutor lambda) throws RuntimeException {
        try {
            this.scenario.doStep(StepType.Then, description, lambda, null);
            return this;
        } catch (Throwable e) {
            throw new RuntimeException("步骤 - " + description + ", 验证错误:" + e.getMessage(), e);
        }
    }

    @Override
    public IThen then(SExecutor lambda) throws RuntimeException {
        try {
            this.scenario.doStep(StepType.Then, lambda, null);
            return this;
        } catch (Throwable e) {
            throw new RuntimeException("步骤 - 验证错误:" + e.getMessage(), e);
        }
    }

    @Override
    public  IThen want(String description, Consumer consumer) throws RuntimeException {
        SExecutor lambda = () -> {
            E expected = (E) SpecContext.getExpectedException();
            if (expected == null) {
                throw new AssertionError("Expecting an exception, but not!");
            }
            try {
                consumer.accept(expected);
            } catch (Throwable e) {
                throw new AssertionError("expected an Exception error: " + e.getMessage(), expected);
            }
        };
        try {
            this.scenario.doStep(StepType.Then, description, lambda, null);
            return this;
        } catch (Throwable e) {
            throw new RuntimeException("步骤 - 异常验证错误:" + e.getMessage(), e);
        }
    }

    @Override
    public  IThen wantResult(String description, Consumer consumer) throws RuntimeException {
        ReturnExecutor lambda = () -> {
            try {
                consumer.accept((T) SpecContext.getWhenResult());
                return null;
            } catch (Throwable e) {
                throw new AssertionError("assert result error: " + e.getMessage(), e);
            }
        };
        try {
            this.scenario.doStep(StepType.Then, description, lambda, null);
            return this;
        } catch (Throwable e) {
            throw new RuntimeException("步骤 - 执行结果验证错误:" + e.getMessage(), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy