Duplicate base junit4 classes for junit5

- AbstractStateMachineTests to AbstractStateMachineJUnit4Tests
- StateMachineTestPlanBuilderTests to StateMachineTestPlanBuilderJUnit4Tests
- Add AbstractStateMachineJUnit5Tests and StateMachineTestPlanBuilderJUnit5Tests
- Move away from junit4 assertThat methods
- Fixes #780
- Also completes and Fixes #771
This commit is contained in:
Janne Valkealahti
2019-12-24 20:37:11 +00:00
parent 3d71c9606e
commit 49860418f8
16 changed files with 437 additions and 17 deletions

View File

@@ -271,10 +271,10 @@ project('spring-statemachine-test') {
compile 'org.springframework:spring-test'
compile 'org.hamcrest:hamcrest-core'
compile 'org.hamcrest:hamcrest-library'
compile 'junit:junit'
compile 'org.junit.jupiter:junit-jupiter-api'
compile 'org.junit.vintage:junit-vintage-engine'
compile 'org.assertj:assertj-core'
optional 'junit:junit'
optional 'org.junit.jupiter:junit-jupiter-api'
optional 'org.junit.vintage:junit-vintage-engine'
testCompile('org.mockito:mockito-core') { dep ->
exclude group: 'org.hamcrest'
}

View File

@@ -17,7 +17,7 @@ package org.springframework.statemachine.buildtests;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

View File

@@ -16,10 +16,10 @@
package org.springframework.statemachine.buildtests;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

View File

@@ -18,7 +18,7 @@ package org.springframework.statemachine.buildtests;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.ArrayList;

View File

@@ -19,7 +19,7 @@ import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveFactory;

View File

@@ -17,7 +17,7 @@ package org.springframework.statemachine.data.jpa;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;

View File

@@ -17,7 +17,7 @@ package org.springframework.statemachine.data.mongodb;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;

View File

@@ -17,7 +17,7 @@ package org.springframework.statemachine.data.redis;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;

View File

@@ -17,7 +17,7 @@ package org.springframework.statemachine.data;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Iterator;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
* @author Janne Valkealahti
*
*/
public abstract class AbstractStateMachineTests {
public abstract class AbstractStateMachineJUnit4Tests {
protected AnnotationConfigApplicationContext context;

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.statemachine.test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Base helper class for state machine tests.
*
* @author Janne Valkealahti
*
*/
public abstract class AbstractStateMachineJUnit5Tests {
protected AnnotationConfigApplicationContext context;
@BeforeEach
public void setup() {
context = buildContext();
}
@AfterEach
public void clean() {
if (context != null) {
context.close();
}
}
protected AnnotationConfigApplicationContext buildContext() {
return null;
}
protected void registerAndRefresh(Class<?>... annotatedClasses) {
context.register(annotatedClasses);
context.refresh();
}
}

View File

@@ -17,14 +17,14 @@ package org.springframework.statemachine.test;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.List;
import org.junit.Test;
import org.springframework.statemachine.test.StateMachineTestPlanBuilder.StateMachineTestPlanStep;
public class StateMachineTestPlanBuilderTests {
public class StateMachineTestPlanBuilderJUnit4Tests {
@Test
public void testBuilderNoSteps() throws Exception {

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.statemachine.test;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.statemachine.test.StateMachineTestPlanBuilder.StateMachineTestPlanStep;
public class StateMachineTestPlanBuilderJUnit5Tests {
@Test
public void testBuilderNoSteps() throws Exception {
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.build();
assertThat(plan, notNullValue());
List<StateMachineTestPlanStep<?, ?>> steps = TestUtils.readField("steps", plan);
assertThat(steps.size(), is(0));
}
@Test
public void testBuilderOneStep() throws Exception {
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.step().expectState("SI").and()
.build();
assertThat(plan, notNullValue());
List<StateMachineTestPlanStep<?, ?>> steps = TestUtils.readField("steps", plan);
assertThat(steps.size(), is(1));
}
}

View File

@@ -31,7 +31,7 @@ import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
public class StateMachineTestingTests extends AbstractStateMachineTests {
public class StateMachineTestingJUnit4Tests extends AbstractStateMachineJUnit4Tests {
@Test
public void testSimpleTestingConcept() throws Exception {

View File

@@ -0,0 +1,315 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.statemachine.test;
import static org.hamcrest.CoreMatchers.not;
import static org.springframework.statemachine.TestUtils.resolveMachine;
import org.hamcrest.collection.IsMapContaining;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
public class StateMachineTestingJUnit5Tests extends AbstractStateMachineJUnit5Tests {
@Test
public void testSimpleTestingConcept() throws Exception {
registerAndRefresh(Config1.class);
StateMachine<String, String> machine = resolveMachine(context);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectState("SI").and()
.step().sendEvent("E1").expectStateChanged(1).expectState("S1").and()
.step().sendEvent("E2").expectStateChanged(1).expectState("S2").and()
.build();
plan.test();
}
@Test
public void testStarted() throws Exception {
registerAndRefresh(Config2.class);
StateMachine<String, String> machine = resolveMachine(context);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectStateMachineStarted(1).and()
.step().expectState("SI").and()
.step().sendEvent("E1").expectStateChanged(1).expectState("S1").and()
.step().sendEvent("E2").expectStateChanged(1).expectState("S2").and()
.build();
plan.test();
}
@Test
public void testMultipleEvents() throws Exception {
registerAndRefresh(Config3.class);
StateMachine<String, String> machine = resolveMachine(context);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectStateMachineStarted(1).and()
.step().expectState("READY").and()
.step()
.sendEvent("DEPLOY")
.sendEvent("DEPLOY")
.expectStateChanged(6)
.expectState("READY")
.and()
.build();
plan.test();
}
@Test
public void testIntermediate() throws Exception {
registerAndRefresh(Config4.class);
StateMachine<String, String> machine = resolveMachine(context);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectStateMachineStarted(1).and()
.step().expectState("SI").and()
.step().sendEvent("E1").expectStateChanged(1).expectState("S1").and()
.step().sendEvent("E2").expectStateChanged(2).expectStateEntered("S2", "S3").expectStateExited("S1", "S2").expectState("S3").and()
.build();
plan.test();
}
@Test
public void testVariables() throws Exception {
registerAndRefresh(Config5.class);
StateMachine<String, String> machine = resolveMachine(context);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectStateMachineStarted(1).and()
.step().expectState("SI").and()
.step()
.sendEvent("E1")
.expectStateChanged(1)
.expectState("S1")
.expectVariable("V1Key")
.expectVariable("V1Key", "V1Value")
.expectVariableWith(IsMapContaining.hasKey("V1Key"))
.expectVariableWith(IsMapContaining.hasValue("V1Value"))
.expectVariableWith(IsMapContaining.hasEntry("V1Key", "V1Value"))
.expectVariableWith(not(IsMapContaining.hasKey("V2Key")))
.and()
.step()
.sendEvent("E2")
.expectStateChanged(1)
.expectState("S2")
.expectVariable("V1Key")
.expectVariable("V1Key", "V1Value")
.expectVariable("V2Key")
.expectVariable("V2Key", "V2Value")
.expectVariableWith(IsMapContaining.hasKey("V1Key"))
.expectVariableWith(IsMapContaining.hasValue("V1Value"))
.expectVariableWith(IsMapContaining.hasEntry("V1Key", "V1Value"))
.expectVariableWith(IsMapContaining.hasKey("V2Key"))
.expectVariableWith(IsMapContaining.hasValue("V2Value"))
.expectVariableWith(IsMapContaining.hasEntry("V2Key", "V2Value"))
.and()
.build();
plan.test();
}
@Override
protected AnnotationConfigApplicationContext buildContext() {
return new AnnotationConfigApplicationContext();
}
@Configuration
@EnableStateMachine
static class Config1 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.state("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2");
}
}
@Configuration
@EnableStateMachine
static class Config2 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.state("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2");
}
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(1);
return taskExecutor;
}
}
@Configuration
@EnableStateMachine
static class Config3 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states)
throws Exception {
states
.withStates()
.initial("READY")
.state("DEPLOYPREPARE", "DEPLOY")
.state("DEPLOYEXECUTE", "DEPLOY");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions)
throws Exception {
transitions
.withExternal()
.source("READY").target("DEPLOYPREPARE")
.event("DEPLOY")
.and()
.withExternal()
.source("DEPLOYPREPARE").target("DEPLOYEXECUTE")
.and()
.withExternal()
.source("DEPLOYEXECUTE").target("READY");
}
}
@Configuration
@EnableStateMachine
static class Config4 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.state("S1")
.state("S2")
.state("S3");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2")
.and()
.withExternal()
.source("S2")
.target("S3");
}
}
@Configuration
@EnableStateMachine
static class Config5 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.state("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.action(c -> {
c.getExtendedState().getVariables().put("V1Key", "V1Value");
})
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2")
.action(c -> {
c.getExtendedState().getVariables().put("V2Key", "V2Value");
});
}
}
}

View File

@@ -19,10 +19,12 @@ package org.springframework.statemachine.test.docs;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.collection.IsMapContaining.hasKey;
import static org.hamcrest.collection.IsMapContaining.hasValue;
import org.junit.jupiter.api.Test;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
//end::snippetC[]
import org.junit.Test;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.test.StateMachineTestPlan;