Skip to content

Commit e2aabe8

Browse files
Tian Baoqiangpivovarit
authored andcommitted
#BAEL-913 serenitybdd and spring (eugenp#1845)
1 parent 9f88ff0 commit e2aabe8

19 files changed

Lines changed: 802 additions & 13 deletions

‎libraries/pom.xml‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,31 @@
169169
<groupId>net.serenity-bdd</groupId>
170170
<artifactId>serenity-core</artifactId>
171171
<version>${serenity.version}</version>
172+
<scope>test</scope>
172173
</dependency>
173174
<dependency>
174175
<groupId>net.serenity-bdd</groupId>
175176
<artifactId>serenity-junit</artifactId>
176177
<version>${serenity.version}</version>
178+
<scope>test</scope>
177179
</dependency>
178180
<dependency>
179181
<groupId>net.serenity-bdd</groupId>
180182
<artifactId>serenity-jbehave</artifactId>
181183
<version>${serenity.jbehave.version}</version>
184+
<scope>test</scope>
182185
</dependency>
183186
<dependency>
184187
<groupId>net.serenity-bdd</groupId>
185188
<artifactId>serenity-rest-assured</artifactId>
186189
<version>${serenity.version}</version>
190+
<scope>test</scope>
187191
</dependency>
188192
<dependency>
189193
<groupId>net.serenity-bdd</groupId>
190194
<artifactId>serenity-jira-requirements-provider</artifactId>
191195
<version>${serenity.jira.version}</version>
196+
<scope>test</scope>
192197
</dependency>
193198
<dependency>
194199
<groupId>com.fasterxml.jackson.core</groupId>
@@ -231,17 +236,36 @@
231236
<artifactId>datanucleus-xml</artifactId>
232237
<version>5.0.0-release</version>
233238
</dependency>
234-
<dependency>
235-
<groupId>com.zaxxer</groupId>
236-
<artifactId>HikariCP</artifactId>
237-
<version>2.6.1</version>
238-
<scope>compile</scope>
239-
</dependency>
240-
<dependency>
241-
<groupId>org.postgresql</groupId>
242-
<artifactId>postgresql</artifactId>
243-
<version>42.0.0</version>
244-
</dependency>
239+
240+
<dependency>
241+
<groupId>org.springframework</groupId>
242+
<artifactId>spring-web</artifactId>
243+
<version>4.3.8.RELEASE</version>
244+
</dependency>
245+
<dependency>
246+
<groupId>net.serenity-bdd</groupId>
247+
<artifactId>serenity-spring</artifactId>
248+
<version>${serenity.version}</version>
249+
<scope>test</scope>
250+
</dependency>
251+
<dependency>
252+
<groupId>io.rest-assured</groupId>
253+
<artifactId>spring-mock-mvc</artifactId>
254+
<version>3.0.3</version>
255+
<scope>test</scope>
256+
</dependency>
257+
<dependency>
258+
<groupId>com.zaxxer</groupId>
259+
<artifactId>HikariCP</artifactId>
260+
<version>2.6.1</version>
261+
<scope>compile</scope>
262+
</dependency>
263+
<dependency>
264+
<groupId>org.postgresql</groupId>
265+
<artifactId>postgresql</artifactId>
266+
<version>42.0.0</version>
267+
</dependency>
268+
245269
</dependencies>
246270

247271
<properties>
@@ -261,10 +285,10 @@
261285
<commons.io.version>2.5</commons.io.version>
262286
<flink.version>1.2.0</flink.version>
263287
<jackson.version>2.8.5</jackson.version>
264-
<serenity.version>1.2.5-rc.11</serenity.version>
288+
<serenity.version>1.4.0</serenity.version>
265289
<serenity.jbehave.version>1.24.0</serenity.jbehave.version>
266290
<serenity.jira.version>1.1.3-rc.5</serenity.jira.version>
267-
<serenity.plugin.version>1.2.5-rc.6</serenity.plugin.version>
291+
<serenity.plugin.version>1.4.0</serenity.plugin.version>
268292
</properties>
269293

270294
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import org.springframework.http.MediaType;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* @author aiet
11+
*/
12+
@RequestMapping(value = "/konamicode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
13+
@RestController
14+
public class KonamiCodeController {
15+
16+
private final String classicCode = "↑↑↓↓←→←→BA";
17+
18+
@GetMapping("/classic")
19+
public String classicCode() {
20+
return classicCode;
21+
}
22+
23+
@GetMapping("/cheatable")
24+
public boolean cheatCheck(@RequestParam String cheatcode){
25+
return classicCode.equals(cheatcode);
26+
}
27+
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* refer to <a href="https://en.wikipedia.org/wiki/Konami_Code">Konami Code</a>
7+
*/
8+
@Service
9+
public class KonamiCodeService {
10+
11+
private String classicCode = "↑↑↓↓←→←→BA";
12+
13+
public String getClassicCode() {
14+
return classicCode;
15+
}
16+
17+
public void alterClassicCode(String newCode) {
18+
classicCode = newCode;
19+
}
20+
21+
public boolean cheatWith(String cheatcode) {
22+
if ("↑↑↓↓←→←→BA".equals(cheatcode)) {
23+
stageLeft++;
24+
return true;
25+
}
26+
return false;
27+
}
28+
29+
private int stageLeft = 1;
30+
31+
public void clearStage() {
32+
stageLeft = 0;
33+
}
34+
35+
public int stageLeft() {
36+
return stageLeft;
37+
}
38+
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import org.springframework.http.MediaType;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
/**
7+
* @author aiet
8+
*/
9+
@RequestMapping(value = "/konamicode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
10+
@RestController
11+
public class KonamiCodeServiceInjectionController {
12+
13+
private KonamiCodeService konamiCodeService;
14+
15+
public KonamiCodeServiceInjectionController(KonamiCodeService konamiCodeService) {
16+
this.konamiCodeService = konamiCodeService;
17+
}
18+
19+
@PutMapping("/stages")
20+
public void clearStage(@RequestParam String action) {
21+
if ("clear".equals(action)) {
22+
konamiCodeService.clearStage();
23+
}
24+
}
25+
26+
@GetMapping("/classic")
27+
public String classicCode() {
28+
return konamiCodeService.getClassicCode();
29+
}
30+
31+
@GetMapping("/cheatable")
32+
public boolean cheatCheck(@RequestParam String cheatcode) {
33+
return konamiCodeService.cheatWith(cheatcode);
34+
}
35+
36+
@GetMapping("/stages")
37+
public int stageLeft() {
38+
return konamiCodeService.stageLeft();
39+
}
40+
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import com.baeldung.serenity.spring.steps.KonamiCodeServiceInjectionSteps;
4+
import net.serenitybdd.junit.runners.SerenityRunner;
5+
import net.serenitybdd.junit.spring.integration.SpringIntegrationClassRule;
6+
import net.thucydides.core.annotations.Steps;
7+
import net.thucydides.core.annotations.Title;
8+
import org.junit.ClassRule;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.Suite;
12+
import org.springframework.test.annotation.DirtiesContext;
13+
import org.springframework.test.context.ContextConfiguration;
14+
15+
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
16+
17+
/**
18+
* @author aiet
19+
*/
20+
21+
@RunWith(Suite.class)
22+
@Suite.SuiteClasses({
23+
KonamCheatClassDirtiesContextTest.DirtiesContextTest.class, KonamCheatClassDirtiesContextTest.AnotherDirtiesContextTest.class
24+
})
25+
public class KonamCheatClassDirtiesContextTest {
26+
27+
@RunWith(SerenityRunner.class)
28+
@ContextConfiguration(classes = KonamiCodeService.class)
29+
public static abstract class Base {
30+
31+
@Steps KonamiCodeServiceInjectionSteps cheatSteps;
32+
33+
@ClassRule public static SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule();
34+
35+
void hiddenStageShouldBeUnlockedAfterCheating() {
36+
fetchAndCheat();
37+
cheatSteps.aStageRemains();
38+
39+
cheatSteps.letsHack();
40+
41+
fetchAndCheat();
42+
cheatSteps.noStageRemains();
43+
}
44+
45+
private void fetchAndCheat() {
46+
cheatSteps.gameStageCleared();
47+
cheatSteps.fetchLatestCheatcode();
48+
cheatSteps.cheatWithLatestcode();
49+
}
50+
51+
}
52+
53+
@DirtiesContext(classMode = AFTER_CLASS)
54+
public static class AnotherDirtiesContextTest extends Base {
55+
56+
@Test
57+
@Title("altering the cheatcode after unlocking would stop others from cheating, not affected by other tests (another)")
58+
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
59+
super.hiddenStageShouldBeUnlockedAfterCheating();
60+
}
61+
}
62+
63+
@DirtiesContext(classMode = AFTER_CLASS)
64+
public static class DirtiesContextTest extends Base {
65+
66+
@Test
67+
@Title("altering the cheatcode after unlocking would stop others from cheating, not affected by other tests")
68+
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
69+
super.hiddenStageShouldBeUnlockedAfterCheating();
70+
}
71+
}
72+
73+
74+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import com.baeldung.serenity.spring.steps.KonamiCodeConstructorDependencySteps;
4+
import net.serenitybdd.junit.runners.SerenityRunner;
5+
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
6+
import net.thucydides.core.annotations.Title;
7+
import org.junit.Before;
8+
import org.junit.FixMethodOrder;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.MethodSorters;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.test.annotation.DirtiesContext;
15+
import org.springframework.test.context.ContextConfiguration;
16+
17+
/**
18+
* @author aiet
19+
*/
20+
@RunWith(SerenityRunner.class)
21+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
22+
@ContextConfiguration(classes = KonamiCodeService.class)
23+
public class KonamCheatWithDirtyActionTest {
24+
25+
private KonamiCodeConstructorDependencySteps cheatSteps;
26+
27+
@Autowired private KonamiCodeService codeService;
28+
29+
@Before
30+
public void init() {
31+
cheatSteps = new KonamiCodeConstructorDependencySteps(codeService);
32+
}
33+
34+
@Test
35+
@Title("hidden stage should be unlocked after cheating (run in service with dirty action)")
36+
public void givenGameStageCleared_whenCheat_thenHiddenStageUnlocked() {
37+
fetchCodeAndCheat();
38+
cheatSteps.aStageRemains();
39+
}
40+
41+
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
42+
43+
@DirtiesContext
44+
@Test
45+
@Title("altering the cheatcode after unlocking would stop others from cheating")
46+
public void givenGameStageCleared_whenCheatAndHack_thenAnotherCheatFail() {
47+
fetchCodeAndCheat();
48+
cheatSteps.aStageRemains();
49+
50+
cheatSteps.letsHack();
51+
52+
fetchCodeAndCheat();
53+
cheatSteps.noStageRemains();
54+
}
55+
56+
private void fetchCodeAndCheat() {
57+
cheatSteps.gameStageCleared();
58+
cheatSteps.fetchLatestCheatcode();
59+
cheatSteps.cheatWithLatestcode();
60+
}
61+
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.serenity.spring;
2+
3+
import com.baeldung.serenity.spring.steps.KonamiCodeServiceInjectionSteps;
4+
import net.serenitybdd.junit.runners.SerenityRunner;
5+
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
6+
import net.thucydides.core.annotations.Steps;
7+
import net.thucydides.core.annotations.Title;
8+
import org.junit.FixMethodOrder;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.MethodSorters;
13+
import org.springframework.test.annotation.DirtiesContext;
14+
import org.springframework.test.context.ContextConfiguration;
15+
16+
/**
17+
* @author aiet
18+
*/
19+
@RunWith(SerenityRunner.class)
20+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
21+
@ContextConfiguration(classes = KonamiCodeService.class)
22+
public class KonamCheatWithDirtyActionWithImplicitInjectionTest {
23+
24+
@Steps private KonamiCodeServiceInjectionSteps cheatSteps;
25+
26+
@Test
27+
@Title("hidden stage is not unlocked after cheating (cheatcode hacked)")
28+
public void givenGameStageCleared_whenCheat_thenCheatFail() {
29+
fetchCodeAndCheat();
30+
cheatSteps.noStageRemains();
31+
}
32+
33+
private void fetchCodeAndCheat() {
34+
cheatSteps.gameStageCleared();
35+
cheatSteps.fetchLatestCheatcode();
36+
cheatSteps.cheatWithLatestcode();
37+
}
38+
39+
@Rule public SpringIntegrationMethodRule springIntegration = new SpringIntegrationMethodRule();
40+
41+
@DirtiesContext
42+
@Test
43+
@Title("altering the cheatcode after unlocking would stop others from cheating")
44+
public void givenGameStageCleared_whenCheatAndHack_thenAnotherCheatFail() {
45+
fetchCodeAndCheat();
46+
cheatSteps.aStageRemains();
47+
48+
cheatSteps.letsHack();
49+
50+
fetchCodeAndCheat();
51+
cheatSteps.noStageRemains();
52+
}
53+
54+
}

0 commit comments

Comments
 (0)