From 9376e8ce692612b0269b446bf48f19d15c233520 Mon Sep 17 00:00:00 2001 From: Diaz Novandi Date: Tue, 13 Feb 2018 13:34:03 +0100 Subject: [PATCH] Refine test using Hamcrest and refactor JUnit rule --- .../ProductInfoRepositoryIntegrationTest.java | 9 +++++--- .../rule/LocalDynamoDBCreationRule.java | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 05b21fd2af..8052aba3df 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -22,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @@ -71,7 +74,7 @@ public class ProductInfoRepositoryIntegrationTest { repository.save(productInfo); List result = (List) repository.findAll(); - assertTrue("Not empty", result.size() > 0); - assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); + assertThat(result.size(), is(greaterThan(0))); + assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST))); } } diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java index 5df377c508..62334b6d00 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java @@ -1,19 +1,21 @@ package com.baeldung.spring.data.dynamodb.repository.rule; -import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; import org.junit.rules.ExternalResource; -import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Optional; public class LocalDynamoDBCreationRule extends ExternalResource { protected DynamoDBProxyServer server; - protected AmazonDynamoDB amazonDynamoDB; + + public LocalDynamoDBCreationRule() { + System.setProperty("sqlite4java.library.path", "native-libs"); + } @Override protected void before() throws Exception { - System.setProperty("sqlite4java.library.path", "native-libs"); String port = "8000"; this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); server.start(); @@ -21,16 +23,15 @@ public class LocalDynamoDBCreationRule extends ExternalResource { @Override protected void after() { + Optional.ofNullable(server).ifPresent(this::stopUnchecked); + } + + protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) { try { - server.stop(); + dynamoDbServer.stop(); } catch (Exception e) { throw new RuntimeException(e); } } - @Autowired - public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) { - this.amazonDynamoDB = amazonDynamoDB; - } - }