Refine test using Hamcrest and refactor JUnit rule

This commit is contained in:
Diaz Novandi 2018-02-13 13:34:03 +01:00 committed by Diaz Novandi
parent b8f433859e
commit 9376e8ce69
2 changed files with 17 additions and 13 deletions

View File

@ -22,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List; 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) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
@ -71,7 +74,7 @@ public class ProductInfoRepositoryIntegrationTest {
repository.save(productInfo); repository.save(productInfo);
List<ProductInfo> result = (List<ProductInfo>) repository.findAll(); List<ProductInfo> result = (List<ProductInfo>) repository.findAll();
assertTrue("Not empty", result.size() > 0); assertThat(result.size(), is(greaterThan(0)));
assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
} }
} }

View File

@ -1,19 +1,21 @@
package com.baeldung.spring.data.dynamodb.repository.rule; 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.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
public class LocalDynamoDBCreationRule extends ExternalResource { public class LocalDynamoDBCreationRule extends ExternalResource {
protected DynamoDBProxyServer server; protected DynamoDBProxyServer server;
protected AmazonDynamoDB amazonDynamoDB;
public LocalDynamoDBCreationRule() {
System.setProperty("sqlite4java.library.path", "native-libs");
}
@Override @Override
protected void before() throws Exception { protected void before() throws Exception {
System.setProperty("sqlite4java.library.path", "native-libs");
String port = "8000"; String port = "8000";
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
server.start(); server.start();
@ -21,16 +23,15 @@ public class LocalDynamoDBCreationRule extends ExternalResource {
@Override @Override
protected void after() { protected void after() {
Optional.ofNullable(server).ifPresent(this::stopUnchecked);
}
protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) {
try { try {
server.stop(); dynamoDbServer.stop();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Autowired
public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) {
this.amazonDynamoDB = amazonDynamoDB;
}
} }