Skip to content

Commit 7a87aa8

Browse files
committed
Refactor tests
1 parent 8d0cdec commit 7a87aa8

File tree

9 files changed

+98
-17
lines changed

9 files changed

+98
-17
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
java: [ '8' ]
15+
java: [ '17', '23' ]
1616
architecture: [ 'x64' ]
1717
name: Build with JDK ${{ matrix.java }} on ${{ matrix.architecture }}
1818
steps:

.run/Template JUnit.run.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="true" type="JUnit" factoryName="JUnit">
3+
<module name="hazelcast-embedded-springboot" />
4+
<option name="MAIN_CLASS_NAME" value="" />
5+
<option name="METHOD_NAME" value="" />
6+
<option name="TEST_OBJECT" value="class" />
7+
<option name="VM_PARAMETERS" value="-ea -Djava.net.preferIPv4Stack=true" />
8+
<method v="2">
9+
<option name="Make" enabled="true" />
10+
</method>
11+
</configuration>
12+
</component>

docs/modules/ROOT/pages/hazelcast-embedded-springboot.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ If Hazelcast is on the classpath and a suitable configuration is found, Spring B
3030
include::ROOT:example$hazelcast-embedded-springboot/pom.xml[tag=hazelcast-dep]
3131
----
3232

33-
Hazelcast configuration (`hazelcast.yaml`) is placed in the `src/main/resources/` directory. You only need to auto-wire the `HazelcastInstance` bean in the `CommandController` and use it to access to Hazelcast data structures:
33+
Hazelcast configuration (`hazelcast.yaml`) is placed in the `src/main/resources/` directory. You only need to define `map` bean, if you want to autowire the IMap instance:
34+
[source,java,indent=0]
35+
----
36+
include::ROOT:example$hazelcast-embedded-springboot/src/main/java/guides/hazelcast/springboot/HazelcastApplication.java[tag=imap-bean]
37+
----
38+
39+
Now you can autowire the `IMap` bean in the `CommandController` and use it to access the Hazelcast structure:
3440

3541
[source,java,indent=0]
3642
----

pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
</goals>
4545
</execution>
4646
</executions>
47+
<configuration>
48+
<argLine>-Djava.net.preferIPv4Stack=true</argLine>
49+
</configuration>
4750
</plugin>
4851
</plugins>
4952
</build>
@@ -54,6 +57,12 @@
5457
<dependency>
5558
<groupId>org.springframework.boot</groupId>
5659
<artifactId>spring-boot-starter-web</artifactId>
60+
<exclusions>
61+
<exclusion>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-logging</artifactId>
64+
</exclusion>
65+
</exclusions>
5766
</dependency>
5867
<!-- tag::hazelcast-dep[] -->
5968
<dependency>
@@ -73,11 +82,24 @@
7382
<artifactId>spring-boot-starter-webflux</artifactId>
7483
<scope>test</scope>
7584
</dependency>
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-log4j2</artifactId>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.apache.logging.log4j</groupId>
91+
<artifactId>log4j-spring-boot</artifactId>
92+
</dependency>
7693
<dependency>
7794
<groupId>net.minidev</groupId>
7895
<artifactId>json-smart</artifactId>
7996
<version>2.5.1</version>
8097
<scope>test</scope>
8198
</dependency>
99+
<dependency>
100+
<groupId>org.awaitility</groupId>
101+
<artifactId>awaitility</artifactId>
102+
<version>4.2.2</version>
103+
</dependency>
82104
</dependencies>
83105
</project>

src/main/java/guides/hazelcast/springboot/HazelcastApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public static void main(String[] args) {
1414
SpringApplication.run(HazelcastApplication.class, args);
1515
}
1616

17+
//tag::imap-bean[]
1718
@Bean
1819
public IMap<String, String> map(HazelcastInstance instance) {
1920
return instance.getMap("map");
2021
}
22+
//end::imap-bean[]
2123
}

src/main/resources/hazelcast.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
hazelcast:
2-
cluster-name: hazelcast-cluster
2+
cluster-name: hazelcast-cluster
3+
properties:
4+
hazelcast.logging.type: log4j2
5+
network:
6+
join:
7+
multicast:
8+
enabled: true
9+
loopbackModeEnabled: true

src/test/java/guides/hazelcast/springboot/CommandControllerIT.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package guides.hazelcast.springboot;
22

3+
import com.hazelcast.config.Config;
34
import com.hazelcast.core.Hazelcast;
45
import com.hazelcast.core.HazelcastInstance;
56
import org.junit.jupiter.api.Test;
@@ -9,7 +10,9 @@
910
import org.springframework.test.context.junit.jupiter.SpringExtension;
1011
import org.springframework.test.web.reactive.server.WebTestClient;
1112

12-
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import java.time.Duration;
14+
15+
import static org.awaitility.Awaitility.await;
1316
import static org.springframework.http.HttpHeaders.ACCEPT;
1417
import static org.springframework.http.MediaType.APPLICATION_JSON;
1518
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@@ -26,7 +29,11 @@ public class CommandControllerIT {
2629
@Test
2730
public void testPutRequest(){
2831
//when
29-
WebTestClient.ResponseSpec responseSpec = makePostRequest("/put?key={key}&value={value}", "key1", "value1");
32+
WebTestClient.ResponseSpec responseSpec = webTestClient
33+
.post()
34+
.uri("/put?key={key}&value={value}", "key1", "value1")
35+
.header(ACCEPT, APPLICATION_JSON_VALUE)
36+
.exchange();
3037

3138
//then
3239
responseSpec.expectStatus()
@@ -40,7 +47,11 @@ public void testPutRequest(){
4047
@Test
4148
public void testGetRequest(){
4249
//given
43-
makePostRequest("/put?key={key}&value={value}", "key1", "value1");
50+
webTestClient
51+
.post()
52+
.uri("/put?key={key}&value={value}", "key1", "value1")
53+
.header(ACCEPT, APPLICATION_JSON_VALUE)
54+
.exchange();
4455

4556
//when
4657
WebTestClient.ResponseSpec responseSpec = webTestClient
@@ -58,20 +69,21 @@ public void testGetRequest(){
5869
.jsonPath("$.value").isEqualTo("value1");
5970
}
6071

61-
private WebTestClient.ResponseSpec makePostRequest(String uri, Object... parameters) {
62-
return webTestClient
63-
.post()
64-
.uri(uri, parameters)
65-
.header(ACCEPT, APPLICATION_JSON_VALUE)
66-
.exchange();
67-
}
68-
6972
@Test
70-
public void testHazelcastCluster(){
73+
public void testHazelcastCluster() {
74+
Config config = Config.load();
75+
config.setProperty("hazelcast.logging.type", "log4j2");
76+
7177
//given
72-
Hazelcast.newHazelcastInstance();
78+
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
7379

7480
//then
75-
assertEquals(2, hazelcastInstance.getCluster().getMembers().size());
81+
try {
82+
await()
83+
.atMost(Duration.ofMinutes(2))
84+
.until(() -> hazelcastInstance.getCluster().getMembers().size() == 2);
85+
} finally {
86+
hz.shutdown();
87+
}
7688
}
7789
}

src/test/resources/hazelcast.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
hazelcast:
22
cluster-name: hazelcast-cluster
3+
properties:
4+
hazelcast.logging.type: log4j2
35
network:
46
join:
57
multicast:
68
enabled: true
9+
loopbackModeEnabled: true

src/test/resources/log4j2.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="ERROR">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{ABSOLUTE} %5p |%X{test-name}| - [%c{1}] %t - %m%n" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="Console" />
11+
</Root>
12+
13+
<Logger name="com.hazelcast.impl" level="DEBUG"/>
14+
<Logger name="com.hazelcast.cluster" level="TRACE"/>
15+
<Logger name="com.hazelcast.instance" level="TRACE"/>
16+
</Loggers>
17+
</Configuration>

0 commit comments

Comments
 (0)