Skip to content

Commit cc95497

Browse files
154 remove spring sqs integration test exceptions (#158)
* refs #153: Make it not require google imports to be in a separate section * refs #155 refs #154: Fix Exceptions in Spring Tests Removes JUnit4 Rules and JUnit5 extensions in favour of manually using the LocalSqsAsyncClient and ElasticMqSqsAsyncClient * refs #154: Fix builds
1 parent 124ad28 commit cc95497

File tree

61 files changed

+1001
-1354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1001
-1354
lines changed

doc/documentation.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ more in depth understanding take a look at the JavaDoc for the [java-dynamic-sqs
2222
for how build a new ArgumentResolver from scratch
2323
1. [How to provide a custom Object Mapper](how-to-guides/spring/spring-how-to-add-custom-argument-resolver.md): guide for overriding the default
2424
`ObjectMapper` that is used to serialise the message body and attributes
25-
1. [How to add your own queue listener](how-to-guides/spring/spring-how-to-add-own-queue-listener.md): useful for defining your own annotation for
26-
queue listening without the verbosity of the custom queue listener
27-
1. [How to write Spring JUnit 4 Integration Tests](how-to-guides/spring/spring-how-to-write-junit4-integration-tests.md): you actually want to test what you are
25+
1. [How to add your own queue listener](how-to-guides/spring/spring-how-to-add-own-queue-listener.md): useful for defining your own annotation for the
26+
queue listening without the verbosity of a custom queue listener
27+
1. [How to write Spring Integration Tests](how-to-guides/spring/spring-how-to-write-integration-tests.md): you actually want to test what you are
2828
writing right?
29-
1. [How to write Spring JUnit 5 Integration Tests](how-to-guides/spring/spring-how-to-write-junit5-integration-tests.md): guide for testing the
30-
application with Junit 5.
3129
1. [How to Start/Stop Queue Listeners](how-to-guides/spring/spring-how-to-start-stop-queue-listeners.md): guide for starting and stopping the
3230
processing of messages for specific queue listeners
3331
1. [How to connect to multiple AWS Accounts](how-to-guides/spring/spring-how-to-connect-to-multiple-aws-accounts.md): guide for listening to queues
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Spring - How to write Integration Tests
2+
This guide provides details on how to write a simple integration test for a spring boot application. Note that writing integration tests through the SQS queue
3+
can be flaky and therefore you should prefer to call your message listener directly in your integration test when testing normal business logic.
4+
5+
For this guide the [Java Dynamic SQS Listener - Spring Integration Test Example](../../../examples/spring-integration-test-example)
6+
module will be used, which is a very simple application that has a single queue listener that calls out to a service when a message is retrieved. The
7+
tests written includes test on:
8+
- The listener receives a message and was able to be successfully processed
9+
- The listener receives a message, was not able to be processed and through the re-drive policy succeeded the next time
10+
- The listener receives a message, was not able to be processed after the number of times defined by the re-drive policy where it ended up in the
11+
Dead Letter Queue
12+
13+
#### Tools
14+
The [elasticmq-sqs-client](../../../util/elasticmq-sqs-client) is a module that will start up an ElasticMQ server and automatically shut it down the end
15+
of the bean's lifecycle.
16+
17+
### Examples
18+
The main example that should be used as a reference is the
19+
[SqsListenerExampleIntegrationTest](../../../examples/spring-integration-test-example/src/test/java/it/com/jashmore/sqs/examples/integrationtests/SqsListenerExampleIntegrationTest.java)
20+
which will test all of those scenarios described above using the methods described below. Otherwise, any of the other integration tests in the spring starter
21+
module would be good examples.
22+
23+
## Steps
24+
1. Include the `elasticmq-sqs-client` maven dependency in the test scope
25+
```xml
26+
<dependency>
27+
<groupId>com.jashmore</groupId>
28+
<artifactId>elasticmq-sqs-client</artifactId>
29+
<version>${java.dynamic.sqs.listener.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
```
33+
1. Create a configuration class in your test providing an `ElasticMqSqsAsyncClient` bean. Here you can provide some
34+
configuration to set up some initial queues in the SQS Server.
35+
```java
36+
@Configuration
37+
public static class TestConfig {
38+
@Bean
39+
public LocalSqsAsyncClient localSqsAsyncClient() {
40+
return new ElasticMqSqsAsyncClient(ImmutableList.of(
41+
SqsQueuesConfig.QueueConfig.builder().queueName(QUEUE_NAME)
42+
.maxReceiveCount(QUEUE_MAX_RECEIVE_COUNT)
43+
.visibilityTimeout(VISIBILITY_TIMEOUT_IN_SECONDS)
44+
.build()));
45+
}
46+
}
47+
```
48+
1. Include this Configuration class in the `@SpringBootTest` annotation.
49+
```java
50+
@SpringBootTest(classes = {Application.class, IntegrationTest.TestConfig.class })
51+
```
52+
1. Autowire the `SqsAsyncClient` for this test, in this case we can use the `LocalSqsAsyncClient` interface.
53+
```java
54+
@Autowired
55+
private LocalSqsAsyncClient localSqsAsyncClient;
56+
```
57+
58+
1. Write the test using the client, for example sending a message to the queue messages on the queue and assert that they are consumed.
59+
```java
60+
public class MyServiceTest {
61+
62+
// Configuration defined above...
63+
64+
@Autowired
65+
private LocalSqsAsyncClient localSqsAsyncClient;
66+
67+
@Test
68+
public void myTest() {
69+
// arrange
70+
// your setup code
71+
72+
// act
73+
localSqsAsyncClient.sendMessage(QUEUE_NAME, "my message");
74+
75+
// assert
76+
// assertions here that the message was processed correctly
77+
}
78+
}
79+
```
80+
81+
1. If the Integration Test has multiple tests it is best to purge the queues between tests and you can do this with a JUnit4 `After` or JUnit4 `AfterEach`
82+
```java
83+
@AfterEach
84+
void tearDown() throws InterruptedException, ExecutionException, TimeoutException {
85+
localSqsAsyncClient.purgeAllQueues().get(5, TimeUnit.SECONDS);
86+
}
87+
```

doc/how-to-guides/spring/spring-how-to-write-junit4-integration-tests.md

Lines changed: 0 additions & 88 deletions
This file was deleted.

doc/how-to-guides/spring/spring-how-to-write-junit5-integration-tests.md

Lines changed: 0 additions & 80 deletions
This file was deleted.

examples/spring-aws-example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<dependency>
4343
<groupId>com.jashmore</groupId>
44-
<artifactId>local-amazon-sqs</artifactId>
44+
<artifactId>local-sqs-async-client</artifactId>
4545
<version>${project.version}</version>
4646
</dependency>
4747
</dependencies>

examples/spring-cloud-schema-registry-example/spring-cloud-schema-registry-consumer/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
<dependency>
4949
<groupId>com.jashmore</groupId>
50-
<artifactId>local-amazon-sqs</artifactId>
50+
<artifactId>local-sqs-async-client</artifactId>
5151
<version>${project.version}</version>
5252
</dependency>
5353
</dependencies>

examples/spring-cloud-schema-registry-example/spring-cloud-schema-registry-consumer/src/main/java/com/jashmore/sqs/examples/schemaregistry/ConsumerApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.jashmore.sqs.extensions.registry.SpringCloudSchemaRegistryPayload;
55
import com.jashmore.sqs.extensions.registry.avro.EnableSchemaRegistrySqsExtension;
66
import com.jashmore.sqs.spring.container.basic.QueueListener;
7-
import com.jashmore.sqs.util.LocalSqsAsyncClient;
7+
import com.jashmore.sqs.util.LocalSqsAsyncClientImpl;
88
import com.jashmore.sqs.util.SqsQueuesConfig;
99
import lombok.extern.slf4j.Slf4j;
1010
import org.springframework.boot.SpringApplication;
@@ -24,7 +24,7 @@ public static void main(String[] args) {
2424

2525
@Bean
2626
public SqsAsyncClient sqsAsyncClient() {
27-
return new LocalSqsAsyncClient(SqsQueuesConfig.builder()
27+
return new LocalSqsAsyncClientImpl(SqsQueuesConfig.builder()
2828
.sqsServerUrl("http://localhost:9324")
2929
.queue(SqsQueuesConfig.QueueConfig.builder()
3030
.queueName("test")

examples/spring-integration-test-example/pom.xml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,14 @@
6161

6262
<dependency>
6363
<groupId>com.jashmore</groupId>
64-
<artifactId>local-amazon-sqs</artifactId>
64+
<artifactId>local-sqs-async-client</artifactId>
6565
<version>${project.version}</version>
6666
<scope>test</scope>
6767
</dependency>
6868

6969
<dependency>
7070
<groupId>com.jashmore</groupId>
71-
<artifactId>local-sqs-test-utils</artifactId>
72-
<version>${project.version}</version>
73-
<scope>test</scope>
74-
</dependency>
75-
76-
<dependency>
77-
<groupId>com.jashmore</groupId>
78-
<artifactId>local-sqs-test-utils-junit5</artifactId>
71+
<artifactId>elasticmq-sqs-client</artifactId>
7972
<version>${project.version}</version>
8073
<scope>test</scope>
8174
</dependency>

0 commit comments

Comments
 (0)