Add additional AssertJ examples

This commit is contained in:
Grzegorz Piwowarek 2016-06-23 17:33:59 +02:00
parent b3e9b08887
commit 024b4f8f61
2 changed files with 68 additions and 1 deletions

1
.gitignore vendored
View File

@ -27,3 +27,4 @@ target/
spring-openid/src/main/resources/application.properties spring-openid/src/main/resources/application.properties
.recommenders/ .recommenders/

View File

@ -2,17 +2,23 @@ package com.baeldung.assertj.introduction;
import com.baeldung.assertj.introduction.domain.Dog; import com.baeldung.assertj.introduction.domain.Dog;
import com.baeldung.assertj.introduction.domain.Person; import com.baeldung.assertj.introduction.domain.Person;
import org.assertj.core.util.Maps;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.withPrecision; import static org.assertj.core.api.Assertions.withPrecision;
public class AssertJDemoTest { public class AssertJCoreTest {
@Test @Test
public void whenComparingReferences_thenNotEqual() throws Exception { public void whenComparingReferences_thenNotEqual() throws Exception {
@ -42,6 +48,10 @@ public class AssertJDemoTest {
public void whenCheckingForElement_thenMultipleAssertions() throws Exception { public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
List<String> list = Arrays.asList("1", "2", "3"); List<String> list = Arrays.asList("1", "2", "3");
assertThat(list).isNotEmpty();
assertThat(list).startsWith("1");
assertThat(list).doesNotContainNull();
assertThat(list) assertThat(list)
.isNotEmpty() .isNotEmpty()
.contains("1") .contains("1")
@ -55,6 +65,17 @@ public class AssertJDemoTest {
assertThat(Runnable.class).isInterface(); assertThat(Runnable.class).isInterface();
} }
@Test
public void whenCheckingCharacter_thenIsUnicode() throws Exception {
char someCharacter = 'c';
assertThat(someCharacter)
.isNotEqualTo('a')
.inUnicode()
.isGreaterThanOrEqualTo('b')
.isLowerCase();
}
@Test @Test
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
@ -65,6 +86,51 @@ public class AssertJDemoTest {
assertThat(5.1).isEqualTo(5, withPrecision(1d)); assertThat(5.1).isEqualTo(5, withPrecision(1d));
} }
@Test
public void whenCheckingString_then() throws Exception {
assertThat("".isEmpty()).isTrue();
}
@Test
public void whenCheckingFile_then() throws Exception {
final File someFile = File.createTempFile("aaa", "bbb");
someFile.deleteOnExit();
assertThat(someFile)
.exists()
.isFile()
.canRead()
.canWrite();
}
@Test
public void whenCheckingIS_then() throws Exception {
InputStream given = new ByteArrayInputStream("foo".getBytes());
InputStream expected = new ByteArrayInputStream("foo".getBytes());
assertThat(given).hasSameContentAs(expected);
}
@Test
public void whenGivenMap_then() throws Exception {
Map<Integer, String> map = Maps.newHashMap(2, "a");
assertThat(map)
.isNotEmpty()
.containsKey(2)
.doesNotContainKeys(10)
.contains(entry(2, "a"));
}
@Test
public void whenGivenException_then() throws Exception {
Exception ex = new Exception("abc");
assertThat(ex)
.hasNoCause()
.hasMessageEndingWith("c");
}
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE @Ignore // IN ORDER TO TEST, REMOVE THIS LINE
@Test @Test
public void whenRunningAssertion_thenDescribed() throws Exception { public void whenRunningAssertion_thenDescribed() throws Exception {