replace #BAEL-913 test scenario with simple add and sum test (#1895)

This commit is contained in:
Tian Baoqiang 2017-05-22 13:46:43 -05:00 committed by Grzegorz Piwowarek
parent 28adbeb53d
commit 4fd99ca7a5
36 changed files with 680 additions and 738 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.serenity.spring;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
* @author aiet
*/
@RequestMapping(value = "/adder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class AdderController {
private AdderService adderService;
public AdderController(AdderService adderService) {
this.adderService = adderService;
}
@GetMapping("/current")
public int currentNum() {
return adderService.currentBase();
}
@PostMapping
public int add(@RequestParam int num) {
return adderService.add(num);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.serenity.spring;
import org.springframework.stereotype.Service;
@Service
public class AdderService {
private int num;
public void baseNum(int base) {
this.num = base;
}
public int currentBase() {
return num;
}
public int add(int adder) {
return this.num + adder;
}
public int accumulate(int adder) {
return this.num += adder;
}
}

View File

@ -1,28 +0,0 @@
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);
}
}

View File

@ -1,39 +0,0 @@
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;
}
}

View File

@ -1,41 +0,0 @@
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();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.serenity.spring;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
* @author aiet
*/
@RequestMapping(value = "/adder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class PlainAdderController {
private final int currentNumber = RandomUtils.nextInt();
@GetMapping("/current")
public int currentNum() {
return currentNumber;
}
@PostMapping
public int add(@RequestParam int num) {
return currentNumber + num;
}
}

View File

@ -0,0 +1,76 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderServiceSteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationClassRule;
import net.thucydides.core.annotations.Steps;
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 com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
/**
* @author aiet
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
AdderClassDirtiesContextIntegrationTest.DirtiesContextTest.class, AdderClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class
})
public class AdderClassDirtiesContextIntegrationTest {
@RunWith(SerenityRunner.class)
@ContextConfiguration(classes = AdderService.class)
public static abstract class Base {
@Steps AdderServiceSteps adderServiceSteps;
@ClassRule public static SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule();
void whenAccumulate_thenSummedUp() {
adderServiceSteps.whenAccumulate();
adderServiceSteps.summedUp();
}
void whenAdd_thenSumWrong() {
adderServiceSteps.whenAdd();
adderServiceSteps.sumWrong();
}
void whenAdd_thenSummedUp() {
adderServiceSteps.whenAdd();
adderServiceSteps.summedUp();
}
}
@DirtiesContext(classMode = AFTER_CLASS)
public static class AnotherDirtiesContextTest extends Base {
@Test
public void givenNumber_whenAdd_thenSumWrong() {
super.whenAdd_thenSummedUp(); //expecting zero
adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt());
super.whenAccumulate_thenSummedUp();
super.whenAdd_thenSumWrong();
}
}
@DirtiesContext(classMode = AFTER_CLASS)
public static class DirtiesContextTest extends Base {
@Test
public void givenNumber_whenAdd_thenSumWrong() {
super.whenAdd_thenSummedUp(); //expecting zero
adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt());
super.whenAccumulate_thenSummedUp();
super.whenAdd_thenSumWrong();
}
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderConstructorDependencySteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
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;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
@RunWith(SerenityRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = AdderService.class)
public class AdderMethodDirtiesContextDependencyWorkaroundIntegrationTest {
private AdderConstructorDependencySteps adderSteps;
@Autowired private AdderService adderService;
@Before
public void init() {
adderSteps = new AdderConstructorDependencySteps(adderService);
}
@Test
public void _1_givenNumber_whenAdd_thenSumWrong() {
adderSteps.whenAdd();
adderSteps.summedUp();
}
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
@DirtiesContext
@Test
public void _0_givenNumber_whenAddAndAccumulate_thenSummedUp() {
adderSteps.givenBaseAndAdder(randomInt(), randomInt());
adderSteps.whenAccumulate();
adderSteps.summedUp();
adderSteps.whenAdd();
adderSteps.sumWrong();
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderServiceSteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
import net.thucydides.core.annotations.Steps;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
@RunWith(SerenityRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = AdderService.class)
public class AdderMethodDirtiesContextInitWorkaroundIntegrationTest {
@Steps private AdderServiceSteps adderServiceSteps;
@Before
public void init() {
adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt());
}
@Test
public void _1_givenNumber_whenAdd_thenSumWrong() {
adderServiceSteps.whenAdd();
adderServiceSteps.summedUp();
}
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
@DirtiesContext
@Test
public void _0_givenNumber_whenAddAndAccumulate_thenSummedUp() {
adderServiceSteps.whenAccumulate();
adderServiceSteps.summedUp();
adderServiceSteps.whenAdd();
adderServiceSteps.sumWrong();
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderServiceSteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
import net.thucydides.core.annotations.Steps;
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;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
@RunWith(SerenityRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = AdderService.class)
public class AdderMethodDirtiesContextIntegrationTest {
@Steps private AdderServiceSteps adderServiceSteps;
@Test
public void _1_givenNumber_whenAdd_thenSumWrong() {
adderServiceSteps.whenAdd();
adderServiceSteps.sumWrong();
}
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
@DirtiesContext
@Test
public void _0_givenNumber_whenAddAndAccumulate_thenSummedUp() {
adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt());
adderServiceSteps.whenAccumulate();
adderServiceSteps.summedUp();
adderServiceSteps.whenAdd();
adderServiceSteps.sumWrong();
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderSteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
import net.thucydides.core.annotations.Steps;
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:adder-beans.xml")
public class AdderMethodRuleIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(AdderMethodRuleIntegrationTest.class);
@BeforeClass
public static void initClass() {
LOG.info("static adder before test class: {}", staticAdder);
}
@AfterClass
public static void destroyClass() {
LOG.info("static adder after test class: {}", staticAdder);
}
@Before
public void init() {
LOG.info("adder before test: {}", adder);
staticAdder = adder;
}
@After
public void destroy() {
LOG.info("adder after test: {}", adder);
}
@Rule public SpringIntegrationMethodRule springMethodIntegration = new SpringIntegrationMethodRule();
@Steps private AdderSteps adderSteps;
@Value("#{props['adder']}") private int adder;
private static int staticAdder;
@Test
public void givenNumber_whenAdd_thenSummedUp() {
adderSteps.givenNumber();
adderSteps.whenAdd(adder);
adderSteps.thenSummedUp();
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderRestSteps;
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;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
@RunWith(SerenityRunner.class)
public class AdderMockMvcIntegrationTest {
@Before
public void init() {
RestAssuredMockMvc.standaloneSetup(new PlainAdderController());
}
@Steps AdderRestSteps steps;
@Test
public void givenNumber_whenAdd_thenSummedUp() throws Exception {
steps.givenCurrentNumber();
steps.whenAddNumber(randomInt());
steps.thenSummedUp();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderServiceSteps;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.thucydides.core.annotations.Steps;
import org.junit.Test;
import org.junit.runner.RunWith;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
@RunWith(SerenityRunner.class)
public class AdderServiceIntegrationTest {
@Steps private AdderServiceSteps adderServiceSteps;
@Test
public void givenNumber_whenAdd_thenSummedUp() {
adderServiceSteps.givenBaseAndAdder(randomInt(), randomInt());
adderServiceSteps.whenAdd();
adderServiceSteps.summedUp();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.serenity.spring;
import com.baeldung.serenity.spring.steps.AdderSteps;
import net.serenitybdd.junit.spring.integration.SpringIntegrationSerenityRunner;
import net.thucydides.core.annotations.Steps;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
/**
* Unit test for simple App.
*/
@RunWith(SpringIntegrationSerenityRunner.class)
@ContextConfiguration(locations = "classpath:adder-beans.xml")
public class AdderSpringSerenityRunnerIntegrationTest {
@Steps private AdderSteps adderSteps;
@Value("#{props['adder']}") private int adder;
@Test
public void givenNumber_whenAdd_thenSummedUp() {
adderSteps.givenNumber();
adderSteps.whenAdd(adder);
adderSteps.thenSummedUp();
}
}

View File

@ -9,14 +9,14 @@ import org.springframework.test.context.ContextConfiguration;
/**
* @author aiet
*/
@ContextConfiguration(classes = { KonamiCodeServiceInjectionController.class, KonamiCodeService.class })
public class KonamiCodeTest extends SerenityStory {
@ContextConfiguration(classes = { AdderController.class, AdderService.class })
public class AdderTest extends SerenityStory {
@Autowired private KonamiCodeService konamiCodeService;
@Autowired private AdderService adderService;
@BeforeStory
public void init() {
RestAssuredMockMvc.standaloneSetup(new KonamiCodeServiceInjectionController(konamiCodeService));
RestAssuredMockMvc.standaloneSetup(new AdderController(adderService));
}
}

View File

@ -1,74 +0,0 @@
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({
KonamCheatClassDirtiesContextIntegrationTest.DirtiesContextTest.class, KonamCheatClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class
})
public class KonamCheatClassDirtiesContextIntegrationTest {
@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();
}
}
}

View File

@ -1,62 +0,0 @@
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 KonamCheatWithDirtyActionIntegrationTest {
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();
}
}

View File

@ -1,54 +0,0 @@
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 KonamCheatWithDirtyActionWithImplicitInjectionIntegrationTest {
@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();
}
}

View File

@ -1,27 +0,0 @@
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 KonamCheatWithServiceIntegrationTest {
@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();
}
}

View File

@ -1,61 +0,0 @@
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 KonamiCheatWithIntegrationMethodRulesIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationMethodRulesIntegrationTest.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();
}
}

View File

@ -1,58 +0,0 @@
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 KonamiCheatWithIntegrationRunnerIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationRunnerIntegrationTest.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();
}
}

View File

@ -1,31 +0,0 @@
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 KonamiCodeMockMvcIntegrationTest {
@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();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.serenity.spring;
import org.apache.commons.lang3.RandomUtils;
/**
* @author aiet
*/
public class RandomNumberUtil {
public static int randomInt() {
return RandomUtils.nextInt(1, 10);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.serenity.spring.steps;
import com.baeldung.serenity.spring.AdderService;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* @author aiet
*/
public class AdderConstructorDependencySteps {
private AdderService adderService;
public AdderConstructorDependencySteps(AdderService adderService) {
this.adderService = adderService;
}
private int givenNumber;
private int base;
private int sum;
public void givenBaseAndAdder(int base, int adder) {
this.base = base;
adderService.baseNum(base);
this.givenNumber = adder;
}
public void whenAdd() {
sum = adderService.add(givenNumber);
}
public void summedUp() {
assertEquals(base + givenNumber, sum);
}
public void sumWrong() {
assertNotEquals(base + givenNumber, sum);
}
public void whenAccumulate() {
sum = adderService.accumulate(givenNumber);
}
}

View File

@ -0,0 +1,46 @@
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 AdderRestSteps {
private MockMvcResponse mockMvcResponse;
private int currentNum;
@Step("get the current number")
public void givenCurrentNumber() throws UnsupportedEncodingException {
currentNum = Integer.valueOf(given()
.when()
.get("/adder/current")
.mvcResult()
.getResponse()
.getContentAsString());
}
@Step("adding {0}")
public void whenAddNumber(int num) {
mockMvcResponse = given()
.queryParam("num", num)
.when()
.post("/adder");
currentNum += num;
}
@Step("got the sum")
public void thenSummedUp() {
mockMvcResponse
.then()
.statusCode(200)
.body(equalTo(currentNum + ""));
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.serenity.spring.steps;
import com.baeldung.serenity.spring.AdderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* @author aiet
*/
@ContextConfiguration(classes = AdderService.class)
public class AdderServiceSteps {
@Autowired private AdderService adderService;
private int givenNumber;
private int base;
private int sum;
public void givenBaseAndAdder(int base, int adder) {
this.base = base;
adderService.baseNum(base);
this.givenNumber = adder;
}
public void whenAdd() {
sum = adderService.add(givenNumber);
}
public void summedUp() {
assertEquals(base + givenNumber, sum);
}
public void sumWrong() {
assertNotEquals(base + givenNumber, sum);
}
public void whenAccumulate() {
sum = adderService.accumulate(givenNumber);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.serenity.spring.steps;
import net.thucydides.core.annotations.Step;
import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt;
/**
* @author aiet
*/
public class AdderSteps {
int currentNumber;
int sum;
@Step("given current number")
public void givenNumber() {
currentNumber = randomInt();
}
@Step("add up {0}")
public void whenAdd(int adder) {
sum = currentNumber + adder;
}
@Step("summed up")
public void thenSummedUp() {
}
}

View File

@ -1,25 +0,0 @@
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() {
}
}

View File

@ -1,58 +0,0 @@
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));
}
}

View File

@ -1,68 +0,0 @@
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"));
}
}

View File

@ -1,55 +0,0 @@
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));
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.serenity.spring.stories;
import com.baeldung.serenity.spring.steps.AdderRestSteps;
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 AdderStory {
@Steps AdderRestSteps restSteps;
@Given("a number")
public void givenANumber() throws Exception {
restSteps.givenCurrentNumber();
}
@When("I submit another number $num to adder")
public void whenISubmitToAdderWithNumber(int num) {
restSteps.whenAddNumber(num);
}
@Then("I get a sum of the numbers")
public void thenIGetTheSum() {
restSteps.thenSummedUp();
}
}

View File

@ -1,37 +0,0 @@
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();
}
}

View File

@ -3,11 +3,11 @@
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
<util:properties id="props">
<prop key="adder">
4
</prop>
</util:properties>
</beans>

View File

@ -0,0 +1,11 @@
Meta:
Narrative:
As user
I want to add a number
So that I can have the sum
Scenario: A user can submit a number to adder and get current sum
Given a number
When I submit another number 5 to adder
Then I get a sum of the numbers

View File

@ -1,12 +0,0 @@
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