#BAEL-913 serenitybdd and spring (#1845)
This commit is contained in:
parent
9f88ff0d2e
commit
e2aabe819e
|
@ -169,26 +169,31 @@
|
|||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-core</artifactId>
|
||||
<version>${serenity.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-junit</artifactId>
|
||||
<version>${serenity.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-jbehave</artifactId>
|
||||
<version>${serenity.jbehave.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-rest-assured</artifactId>
|
||||
<version>${serenity.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-jira-requirements-provider</artifactId>
|
||||
<version>${serenity.jira.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
@ -231,17 +236,36 @@
|
|||
<artifactId>datanucleus-xml</artifactId>
|
||||
<version>5.0.0-release</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>2.6.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>4.3.8.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.serenity-bdd</groupId>
|
||||
<artifactId>serenity-spring</artifactId>
|
||||
<version>${serenity.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>spring-mock-mvc</artifactId>
|
||||
<version>3.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>2.6.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -261,10 +285,10 @@
|
|||
<commons.io.version>2.5</commons.io.version>
|
||||
<flink.version>1.2.0</flink.version>
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
<serenity.version>1.2.5-rc.11</serenity.version>
|
||||
<serenity.version>1.4.0</serenity.version>
|
||||
<serenity.jbehave.version>1.24.0</serenity.jbehave.version>
|
||||
<serenity.jira.version>1.1.3-rc.5</serenity.jira.version>
|
||||
<serenity.plugin.version>1.2.5-rc.6</serenity.plugin.version>
|
||||
<serenity.plugin.version>1.4.0</serenity.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RequestMapping(value = "/konamicode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@RestController
|
||||
public class KonamiCodeController {
|
||||
|
||||
private final String classicCode = "↑↑↓↓←→←→BA";
|
||||
|
||||
@GetMapping("/classic")
|
||||
public String classicCode() {
|
||||
return classicCode;
|
||||
}
|
||||
|
||||
@GetMapping("/cheatable")
|
||||
public boolean cheatCheck(@RequestParam String cheatcode){
|
||||
return classicCode.equals(cheatcode);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* refer to <a href="https://en.wikipedia.org/wiki/Konami_Code">Konami Code</a>
|
||||
*/
|
||||
@Service
|
||||
public class KonamiCodeService {
|
||||
|
||||
private String classicCode = "↑↑↓↓←→←→BA";
|
||||
|
||||
public String getClassicCode() {
|
||||
return classicCode;
|
||||
}
|
||||
|
||||
public void alterClassicCode(String newCode) {
|
||||
classicCode = newCode;
|
||||
}
|
||||
|
||||
public boolean cheatWith(String cheatcode) {
|
||||
if ("↑↑↓↓←→←→BA".equals(cheatcode)) {
|
||||
stageLeft++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private int stageLeft = 1;
|
||||
|
||||
public void clearStage() {
|
||||
stageLeft = 0;
|
||||
}
|
||||
|
||||
public int stageLeft() {
|
||||
return stageLeft;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RequestMapping(value = "/konamicode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@RestController
|
||||
public class KonamiCodeServiceInjectionController {
|
||||
|
||||
private KonamiCodeService konamiCodeService;
|
||||
|
||||
public KonamiCodeServiceInjectionController(KonamiCodeService konamiCodeService) {
|
||||
this.konamiCodeService = konamiCodeService;
|
||||
}
|
||||
|
||||
@PutMapping("/stages")
|
||||
public void clearStage(@RequestParam String action) {
|
||||
if ("clear".equals(action)) {
|
||||
konamiCodeService.clearStage();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/classic")
|
||||
public String classicCode() {
|
||||
return konamiCodeService.getClassicCode();
|
||||
}
|
||||
|
||||
@GetMapping("/cheatable")
|
||||
public boolean cheatCheck(@RequestParam String cheatcode) {
|
||||
return konamiCodeService.cheatWith(cheatcode);
|
||||
}
|
||||
|
||||
@GetMapping("/stages")
|
||||
public int stageLeft() {
|
||||
return konamiCodeService.stageLeft();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeServiceInjectionSteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.serenitybdd.junit.spring.integration.SpringIntegrationClassRule;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
KonamCheatClassDirtiesContextTest.DirtiesContextTest.class, KonamCheatClassDirtiesContextTest.AnotherDirtiesContextTest.class
|
||||
})
|
||||
public class KonamCheatClassDirtiesContextTest {
|
||||
|
||||
@RunWith(SerenityRunner.class)
|
||||
@ContextConfiguration(classes = KonamiCodeService.class)
|
||||
public static abstract class Base {
|
||||
|
||||
@Steps KonamiCodeServiceInjectionSteps cheatSteps;
|
||||
|
||||
@ClassRule public static SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule();
|
||||
|
||||
void hiddenStageShouldBeUnlockedAfterCheating() {
|
||||
fetchAndCheat();
|
||||
cheatSteps.aStageRemains();
|
||||
|
||||
cheatSteps.letsHack();
|
||||
|
||||
fetchAndCheat();
|
||||
cheatSteps.noStageRemains();
|
||||
}
|
||||
|
||||
private void fetchAndCheat() {
|
||||
cheatSteps.gameStageCleared();
|
||||
cheatSteps.fetchLatestCheatcode();
|
||||
cheatSteps.cheatWithLatestcode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
public static class AnotherDirtiesContextTest extends Base {
|
||||
|
||||
@Test
|
||||
@Title("altering the cheatcode after unlocking would stop others from cheating, not affected by other tests (another)")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
super.hiddenStageShouldBeUnlockedAfterCheating();
|
||||
}
|
||||
}
|
||||
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
public static class DirtiesContextTest extends Base {
|
||||
|
||||
@Test
|
||||
@Title("altering the cheatcode after unlocking would stop others from cheating, not affected by other tests")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
super.hiddenStageShouldBeUnlockedAfterCheating();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeConstructorDependencySteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RunWith(SerenityRunner.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@ContextConfiguration(classes = KonamiCodeService.class)
|
||||
public class KonamCheatWithDirtyActionTest {
|
||||
|
||||
private KonamiCodeConstructorDependencySteps cheatSteps;
|
||||
|
||||
@Autowired private KonamiCodeService codeService;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
cheatSteps = new KonamiCodeConstructorDependencySteps(codeService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Title("hidden stage should be unlocked after cheating (run in service with dirty action)")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.aStageRemains();
|
||||
}
|
||||
|
||||
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
|
||||
|
||||
@DirtiesContext
|
||||
@Test
|
||||
@Title("altering the cheatcode after unlocking would stop others from cheating")
|
||||
public void givenGameStageCleared_whenCheatAndHack_thenAnotherCheatFail() {
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.aStageRemains();
|
||||
|
||||
cheatSteps.letsHack();
|
||||
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.noStageRemains();
|
||||
}
|
||||
|
||||
private void fetchCodeAndCheat() {
|
||||
cheatSteps.gameStageCleared();
|
||||
cheatSteps.fetchLatestCheatcode();
|
||||
cheatSteps.cheatWithLatestcode();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeServiceInjectionSteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RunWith(SerenityRunner.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@ContextConfiguration(classes = KonamiCodeService.class)
|
||||
public class KonamCheatWithDirtyActionWithImplicitInjectionTest {
|
||||
|
||||
@Steps private KonamiCodeServiceInjectionSteps cheatSteps;
|
||||
|
||||
@Test
|
||||
@Title("hidden stage is not unlocked after cheating (cheatcode hacked)")
|
||||
public void givenGameStageCleared_whenCheat_thenCheatFail() {
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.noStageRemains();
|
||||
}
|
||||
|
||||
private void fetchCodeAndCheat() {
|
||||
cheatSteps.gameStageCleared();
|
||||
cheatSteps.fetchLatestCheatcode();
|
||||
cheatSteps.cheatWithLatestcode();
|
||||
}
|
||||
|
||||
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
|
||||
|
||||
@DirtiesContext
|
||||
@Test
|
||||
@Title("altering the cheatcode after unlocking would stop others from cheating")
|
||||
public void givenGameStageCleared_whenCheatAndHack_thenAnotherCheatFail() {
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.aStageRemains();
|
||||
|
||||
cheatSteps.letsHack();
|
||||
|
||||
fetchCodeAndCheat();
|
||||
cheatSteps.noStageRemains();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeServiceInjectionSteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RunWith(SerenityRunner.class)
|
||||
public class KonamCheatWithServiceTest {
|
||||
|
||||
@Steps private KonamiCodeServiceInjectionSteps cheatSteps;
|
||||
|
||||
@Test
|
||||
@Title("hidden stage should be unlocked after cheating (mockmvc)")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
cheatSteps.gameStageCleared();
|
||||
cheatSteps.fetchLatestCheatcode();
|
||||
cheatSteps.cheatWithLatestcode();
|
||||
cheatSteps.aStageRemains();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCheatSteps;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
@RunWith(SerenityRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:konami-cheat-beans.xml")
|
||||
public class KonamiCheatWithIntegrationMethodRulesTest {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationMethodRulesTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void initClass() {
|
||||
LOG.info("static chaincode before test class: {}", staticCheatCode);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroyClass() {
|
||||
LOG.info("static chaincode after test class: {}", staticCheatCode);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
staticCheatCode = cheatCode;
|
||||
LOG.info("cheatcode before test: {}", cheatCode);
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() {
|
||||
LOG.info("cheatcode after test: {}", cheatCode);
|
||||
}
|
||||
|
||||
@Rule public SpringIntegrationMethodRule springMethodIntegration = new SpringIntegrationMethodRule();
|
||||
|
||||
@Steps private KonamiCheatSteps konamiCheatSteps;
|
||||
|
||||
@Value("#{konami_props['code']}") private String cheatCode;
|
||||
|
||||
private static String staticCheatCode;
|
||||
|
||||
@Test
|
||||
@Title("hidden stage should be unlocked after cheating (rule integration)")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
konamiCheatSteps.gameStageCleared();
|
||||
konamiCheatSteps.cheatWith(cheatCode);
|
||||
konamiCheatSteps.aStageRemains();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCheatSteps;
|
||||
import net.serenitybdd.junit.spring.integration.SpringIntegrationSerenityRunner;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import net.thucydides.core.annotations.Title;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
@RunWith(SpringIntegrationSerenityRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:konami-cheat-beans.xml")
|
||||
public class KonamiCheatWithIntegrationRunnerTest {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationRunnerTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void initClass() {
|
||||
LOG.info("static chaincode before test class: {}", staticCheatCode);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroyClass() {
|
||||
LOG.info("static chaincode after test class: {}", staticCheatCode);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
staticCheatCode = cheatCode;
|
||||
LOG.info("cheatcode before test: {}", cheatCode);
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() {
|
||||
LOG.info("cheatcode after test: {}", cheatCode);
|
||||
}
|
||||
|
||||
@Steps private KonamiCheatSteps konamiCheatSteps;
|
||||
|
||||
@Value("#{konami_props['code']}") private String cheatCode;
|
||||
|
||||
private static String staticCheatCode;
|
||||
|
||||
@Test
|
||||
@Title("hidden stage should be unlocked after cheating (with integration runner)")
|
||||
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
|
||||
konamiCheatSteps.gameStageCleared();
|
||||
konamiCheatSteps.cheatWith(cheatCode);
|
||||
konamiCheatSteps.aStageRemains();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeRestSteps;
|
||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
import net.serenitybdd.junit.runners.SerenityRunner;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@RunWith(SerenityRunner.class)
|
||||
public class KonamiCodeMockMvcTest {
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
RestAssuredMockMvc.standaloneSetup(new KonamiCodeController());
|
||||
}
|
||||
|
||||
@Steps KonamiCodeRestSteps steps;
|
||||
|
||||
@Test
|
||||
public void givenOfficialClassicCheatcode_whenCheat_ThenTheCodeShouldDoTheTrick() throws Exception {
|
||||
steps.givenClassicCheatCode();
|
||||
steps.whenCheat();
|
||||
steps.thenClassicCodeCanDoTheTrick();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
import net.serenitybdd.jbehave.SerenityStory;
|
||||
import org.jbehave.core.annotations.BeforeStory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@ContextConfiguration(classes = { KonamiCodeServiceInjectionController.class, KonamiCodeService.class })
|
||||
public class KonamiCodeTest extends SerenityStory {
|
||||
|
||||
@Autowired private KonamiCodeService konamiCodeService;
|
||||
|
||||
@BeforeStory
|
||||
public void init() {
|
||||
RestAssuredMockMvc.standaloneSetup(new KonamiCodeServiceInjectionController(konamiCodeService));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.serenity.spring.steps;
|
||||
|
||||
import net.thucydides.core.annotations.Step;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class KonamiCheatSteps {
|
||||
|
||||
@Step("all stages of the game are cleared")
|
||||
public void gameStageCleared() {
|
||||
}
|
||||
|
||||
@Step("input the classic 'Konami Code': {0} ")
|
||||
public void cheatWith(String cheatcode) {
|
||||
assertEquals("cheatcode wrong", "↑↑↓↓←→←→BA", cheatcode);
|
||||
}
|
||||
|
||||
@Step("there is still a stage left")
|
||||
public void aStageRemains() {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.serenity.spring.steps;
|
||||
|
||||
import com.baeldung.serenity.spring.KonamiCodeService;
|
||||
import net.thucydides.core.annotations.Step;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class KonamiCodeConstructorDependencySteps {
|
||||
|
||||
private String latestCheatcode;
|
||||
private boolean cheatSuccess;
|
||||
|
||||
private KonamiCodeService konamiCodeService;
|
||||
|
||||
public KonamiCodeConstructorDependencySteps(KonamiCodeService konamiCodeService) {
|
||||
this.konamiCodeService = konamiCodeService;
|
||||
}
|
||||
|
||||
@Step("fetch latest cheat code")
|
||||
public void fetchLatestCheatcode() {
|
||||
latestCheatcode = konamiCodeService.getClassicCode();
|
||||
}
|
||||
|
||||
@Step("cheat with latest code")
|
||||
public void cheatWithLatestcode() {
|
||||
cheatSuccess = konamiCodeService.cheatWith(latestCheatcode);
|
||||
}
|
||||
|
||||
@Step("all stages of the game are cleared")
|
||||
public void gameStageCleared() {
|
||||
konamiCodeService.clearStage();
|
||||
}
|
||||
|
||||
@Step("there is still a stage left")
|
||||
public void aStageRemains() {
|
||||
assertTrue("cheatcode wrong", cheatSuccess);
|
||||
assertThat(konamiCodeService.stageLeft(), equalTo(1));
|
||||
}
|
||||
|
||||
// @Rule public SpringIntegrationMethodRule methodRule = new SpringIntegrationMethodRule();
|
||||
|
||||
// @DirtiesContext
|
||||
@Step
|
||||
public void letsHack() {
|
||||
konamiCodeService.alterClassicCode(RandomStringUtils.random(4));
|
||||
}
|
||||
|
||||
@Step("there is no stage left")
|
||||
public void noStageRemains() {
|
||||
assertFalse(cheatSuccess);
|
||||
assertThat(konamiCodeService.stageLeft(), equalTo(0));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.serenity.spring.steps;
|
||||
|
||||
import io.restassured.module.mockmvc.response.MockMvcResponse;
|
||||
import net.thucydides.core.annotations.Step;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class KonamiCodeRestSteps {
|
||||
|
||||
MockMvcResponse mockMvcResponse;
|
||||
String cheatcode;
|
||||
|
||||
@Step("get the classic cheat code")
|
||||
public void givenClassicCheatCode() throws UnsupportedEncodingException {
|
||||
cheatcode = given()
|
||||
.when()
|
||||
.get("/konamicode/classic")
|
||||
.mvcResult()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
}
|
||||
|
||||
@Step("check if the cheat code works")
|
||||
public void whenCheat() {
|
||||
mockMvcResponse = given()
|
||||
.queryParam("cheatcode", cheatcode)
|
||||
.when()
|
||||
.get("/konamicode/cheatable");
|
||||
}
|
||||
|
||||
@Step("classic cheat code matches")
|
||||
public void thenClassicCodeCanDoTheTrick() {
|
||||
mockMvcResponse
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("true"));
|
||||
}
|
||||
|
||||
@Step("all stage cleared")
|
||||
public void stageCleared() {
|
||||
given()
|
||||
.queryParam("action", "clear")
|
||||
.when()
|
||||
.put("/konamicode/stages");
|
||||
given()
|
||||
.when()
|
||||
.get("/konamicode/stages")
|
||||
.then()
|
||||
.body(equalTo("0"));
|
||||
}
|
||||
|
||||
@Step("one more stage to play")
|
||||
public void thenMoreStages() {
|
||||
given()
|
||||
.when()
|
||||
.get("/konamicode/stages")
|
||||
.then()
|
||||
.body(equalTo("1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.serenity.spring.steps;
|
||||
|
||||
import com.baeldung.serenity.spring.KonamiCodeService;
|
||||
import net.thucydides.core.annotations.Step;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@ContextConfiguration(classes = KonamiCodeService.class)
|
||||
public class KonamiCodeServiceInjectionSteps {
|
||||
|
||||
private String latestCheatcode;
|
||||
private boolean cheatSuccess;
|
||||
|
||||
@Autowired private KonamiCodeService konamiCodeService;
|
||||
|
||||
@Step("fetch latest cheat code")
|
||||
public void fetchLatestCheatcode() {
|
||||
latestCheatcode = konamiCodeService.getClassicCode();
|
||||
}
|
||||
|
||||
@Step("cheat with latest code")
|
||||
public void cheatWithLatestcode() {
|
||||
cheatSuccess = konamiCodeService.cheatWith(latestCheatcode);
|
||||
}
|
||||
|
||||
@Step("all stages of the game are cleared")
|
||||
public void gameStageCleared() {
|
||||
konamiCodeService.clearStage();
|
||||
}
|
||||
|
||||
@Step("there is still a stage left")
|
||||
public void aStageRemains() {
|
||||
assertTrue("cheatcode wrong", cheatSuccess);
|
||||
assertThat(konamiCodeService.stageLeft(), equalTo(1));
|
||||
}
|
||||
|
||||
@Step("altering default cheat code")
|
||||
public void letsHack() {
|
||||
konamiCodeService.alterClassicCode(RandomStringUtils.random(4));
|
||||
}
|
||||
|
||||
@Step("there is no stage left")
|
||||
public void noStageRemains() {
|
||||
assertFalse(cheatSuccess);
|
||||
assertThat(konamiCodeService.stageLeft(), equalTo(0));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.serenity.spring.stories;
|
||||
|
||||
import com.baeldung.serenity.spring.steps.KonamiCodeRestSteps;
|
||||
import net.thucydides.core.annotations.Steps;
|
||||
import org.jbehave.core.annotations.Given;
|
||||
import org.jbehave.core.annotations.Then;
|
||||
import org.jbehave.core.annotations.When;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class KonamiCodeStory {
|
||||
|
||||
@Steps KonamiCodeRestSteps restSteps;
|
||||
|
||||
@Given("game stage cleared")
|
||||
public void givenStageCleared(){
|
||||
restSteps.stageCleared();
|
||||
}
|
||||
|
||||
@Given("KONAMI cheat code")
|
||||
public void givenKONAMICheatCode() throws Exception{
|
||||
restSteps.givenClassicCheatCode();
|
||||
}
|
||||
|
||||
@When("I input the cheat code")
|
||||
public void whenIInputTheCheatCode() {
|
||||
restSteps.whenCheat();
|
||||
}
|
||||
|
||||
@Then("a hidden stage will be unlocked")
|
||||
public void thenAHiddenStageWillBeUnlocked() {
|
||||
restSteps.thenClassicCodeCanDoTheTrick();
|
||||
restSteps.thenMoreStages();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<util:properties id="konami_props">
|
||||
<prop key="code">
|
||||
↑↑↓↓←→←→BA
|
||||
</prop>
|
||||
|
||||
</util:properties>
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
Meta:
|
||||
|
||||
Narrative:
|
||||
As a KONAMI player
|
||||
I want to cheat
|
||||
So that I can unlock hidden stages of the game
|
||||
|
||||
Scenario: A KONAMI player can use the cheatcode to unlock hidden stages
|
||||
Given game stage cleared
|
||||
And KONAMI cheat code
|
||||
When I input the cheat code
|
||||
Then a hidden stage will be unlocked
|
Loading…
Reference in New Issue