io.github.theangrydev.yatspecfluent.YatspecFluent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yatspec-fluent Show documentation
Show all versions of yatspec-fluent Show documentation
A plugin for the yatspec acceptance test library used to write acceptance tests in a fluent way, reusing givens, whens and thens.
The newest version!
/*
* Copyright 2016 Liam Williams .
*
* This file is part of yatspec-fluent.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.theangrydev.yatspecfluent;
import com.googlecode.yatspec.state.givenwhenthen.TestState;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
/**
* Use this as the base class for your acceptance tests.
*
* @param The type of test result produced by the {@link When}
*/
@SuppressWarnings("PMD.TooManyMethods") // This is part of the API design
public class YatspecFluent extends TestWatcher implements YatspecFluentCommands {
private final Verification verification = new Verification<>();
private final TestState state = new TestState();
private TestResult testResult;
@Rule
public final TestWatcher testWatcher = this;
@Override
protected void succeeded(Description description) {
verification.checkThenHasBeenUsed();
}
/**
* You should aim to never access the state directly, but you might need to (e.g. global shared state).
* Call {@link #addToGivens(String, Object)} when possible or make use of the {@link WriteOnlyTestItems} interface.
* Call {@link #addToCapturedInputsAndOutputs(String, Object)} when possible or make use of the {@link WriteOnlyTestItems} interface.
*/
@Override
public TestState testState() {
return state;
}
@Override
public void and(Given given) {
given(given);
}
/**
* Prime the given immediately.
*
* @param given The first given in the acceptance test, which should be built up inside the brackets
*/
@Override
public void given(Given given) {
verification.checkGivenIsAllowed(given);
given.prime();
verification.recordGiven(given);
}
@Override
public > void when(T when) {
verification.checkWhenIsAllowed();
testResult = when.execute();
verification.recordWhen(when, testResult);
}
@Override
public void given(When when) {
given((Given) when::execute);
}
@Override
public void and(When when) {
given(when);
}
@Override
public Then then(ThenAssertion thenAssertion) {
verification.checkThenAssertionIsAllowed(thenAssertion);
return thenAssertion.then(testResult);
}
@Override
public Then and(ThenAssertion thenAssertion) {
return then(thenAssertion);
}
@Override
public void and(ThenVerification thenVerification) {
then(thenVerification);
}
@Override
public void then(ThenVerification thenVerification) {
verification.checkThenVerificationIsAllowed(thenVerification);
thenVerification.verify(testResult);
verification.recordThenVerification(thenVerification);
}
}