Skip to content

Commit 9376e8c

Browse files
committed
Refine test using Hamcrest and refactor JUnit rule
1 parent b8f4338 commit 9376e8c

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

‎persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
import java.util.List;
2424

25-
import static org.junit.Assert.assertTrue;
25+
import static org.hamcrest.Matchers.greaterThan;
26+
import static org.hamcrest.core.Is.is;
27+
import static org.hamcrest.core.IsEqual.equalTo;
28+
import static org.junit.Assert.assertThat;
2629

2730
@RunWith(SpringJUnit4ClassRunner.class)
2831
@SpringBootTest(classes = Application.class)
@@ -71,7 +74,7 @@ public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() {
7174
repository.save(productInfo);
7275

7376
List<ProductInfo> result = (List<ProductInfo>) repository.findAll();
74-
assertTrue("Not empty", result.size() > 0);
75-
assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST));
77+
assertThat(result.size(), is(greaterThan(0)));
78+
assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
7679
}
7780
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
package com.baeldung.spring.data.dynamodb.repository.rule;
22

3-
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
43
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
54
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
65
import org.junit.rules.ExternalResource;
7-
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import java.util.Optional;
88

99
public class LocalDynamoDBCreationRule extends ExternalResource {
1010

1111
protected DynamoDBProxyServer server;
12-
protected AmazonDynamoDB amazonDynamoDB;
12+
13+
public LocalDynamoDBCreationRule() {
14+
System.setProperty("sqlite4java.library.path", "native-libs");
15+
}
1316

1417
@Override
1518
protected void before() throws Exception {
16-
System.setProperty("sqlite4java.library.path", "native-libs");
1719
String port = "8000";
1820
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
1921
server.start();
2022
}
2123

2224
@Override
2325
protected void after() {
26+
Optional.ofNullable(server).ifPresent(this::stopUnchecked);
27+
}
28+
29+
protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) {
2430
try {
25-
server.stop();
31+
dynamoDbServer.stop();
2632
} catch (Exception e) {
2733
throw new RuntimeException(e);
2834
}
2935
}
3036

31-
@Autowired
32-
public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) {
33-
this.amazonDynamoDB = amazonDynamoDB;
34-
}
35-
3637
}

0 commit comments

Comments
 (0)