Group testing modules (#3014)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules * group testing modules * try fix conflict
This commit is contained in:
committed by
GitHub
parent
b383d83bf4
commit
776a01429e
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.introductionjukito;
|
||||
|
||||
import org.jukito.All;
|
||||
import org.jukito.JukitoModule;
|
||||
import org.jukito.JukitoRunner;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(JukitoRunner.class)
|
||||
public class CalculatorTest {
|
||||
|
||||
public static class Module extends JukitoModule {
|
||||
|
||||
@Override
|
||||
protected void configureTest() {
|
||||
bindMany(Calculator.class, SimpleCalculator.class,
|
||||
ScientificCalculator.class);
|
||||
bindManyInstances(AdditionTest.class, new AdditionTest(1, 1, 2),
|
||||
new AdditionTest(10, 10, 20),
|
||||
new AdditionTest(18, 24, 42));
|
||||
bindManyNamedInstances(Integer.class, "even", 2, 4, 6);
|
||||
bindManyNamedInstances(Integer.class, "odd", 1, 3, 5);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AdditionTest {
|
||||
|
||||
int a;
|
||||
int b;
|
||||
int expected;
|
||||
|
||||
public AdditionTest(int a, int b, int expected) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.expected = expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_WhenAdd_ThenSumBoth(@All Calculator calc) {
|
||||
double result = calc.add(1, 1);
|
||||
assertEquals(2, result, .1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_WhenAdd_ThenSumBoth(@All Calculator calc,
|
||||
@All AdditionTest addTest) {
|
||||
double result = calc.add(addTest.a, addTest.b);
|
||||
assertEquals(addTest.expected, result, .1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenNumbers_whenPrint_thenOutput(@All("even") Integer i) {
|
||||
System.out.println("even " + i);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOddNumbers_whenPrint_thenOutput(@All("odd") Integer i) {
|
||||
System.out.println("odd " + i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AdditionTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public class BlockingTestRunner extends BlockJUnit4ClassRunner {
|
||||
public BlockingTestRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Statement methodInvoker(FrameworkMethod method, Object test) {
|
||||
System.out.println("invoking: " + method.getName());
|
||||
return super.methodInvoker(method, test);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class CalculatorTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SubstractionTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void substraction() {
|
||||
assertEquals("substraction", 2, calculator.sub(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
AdditionTest.class,
|
||||
SubstractionTest.class})
|
||||
public class SuiteTest {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.Runner;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestRunner extends Runner {
|
||||
|
||||
private Class testClass;
|
||||
public TestRunner(Class testClass) {
|
||||
super();
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Description getDescription() {
|
||||
return Description.createTestDescription(testClass, "My runner description");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(RunNotifier notifier) {
|
||||
System.out.println("running the tests from MyRunner: " + testClass);
|
||||
try {
|
||||
Object testObject = testClass.newInstance();
|
||||
for (Method method : testClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(Test.class)) {
|
||||
notifier.fireTestStarted(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
method.invoke(testObject);
|
||||
notifier.fireTestFinished(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.junitparams;
|
||||
|
||||
import junitparams.FileParameters;
|
||||
import junitparams.JUnitParamsRunner;
|
||||
import junitparams.Parameters;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnitParamsRunner.class)
|
||||
public class SafeAdditionUtilTest {
|
||||
|
||||
private SafeAdditionUtil serviceUnderTest = new SafeAdditionUtil();
|
||||
|
||||
@Test
|
||||
@Parameters({"1, 2, 3", "-10, 30, 20", "15, -5, 10", "-5, -10, -15"})
|
||||
public void whenWithAnnotationProvidedParams_thenSafeAdd(int a, int b, int expectedValue) {
|
||||
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "parametersToTestAdd")
|
||||
public void whenWithNamedMethod_thendSafeAdd(int a, int b, int expectedValue) {
|
||||
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
|
||||
}
|
||||
|
||||
private Object[] parametersToTestAdd() {
|
||||
return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -8, Integer.MIN_VALUE}};
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters
|
||||
public void whenWithnoParam_thenLoadByNameSafeAdd(int a, int b, int expectedValue) {
|
||||
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
|
||||
}
|
||||
|
||||
private Object[] parametersForWhenWithnoParam_thenLoadByNameSafeAdd() {
|
||||
return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -8, Integer.MIN_VALUE}};
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(source = TestDataProvider.class)
|
||||
public void whenWithNamedClass_thenSafeAdd(int a, int b, int expectedValue) {
|
||||
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
|
||||
}
|
||||
|
||||
@Test
|
||||
@FileParameters("src/test/resources/JunitParamsTestParameters.csv")
|
||||
public void whenWithCsvFile_thenSafeAdd(int a, int b, int expectedValue) {
|
||||
assertEquals(expectedValue, serviceUnderTest.safeAdd(a, b));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.junitparams;
|
||||
|
||||
public class TestDataProvider {
|
||||
|
||||
public static Object[] provideBasicData() {
|
||||
return new Object[]{new Object[]{1, 2, 3}, new Object[]{-10, 30, 20}, new Object[]{15, -5, 10}, new Object[]{-5, -10, -15}};
|
||||
}
|
||||
|
||||
public static Object[] provideEdgeCaseData() {
|
||||
return new Object[]{new Object[]{Integer.MAX_VALUE, 2, Integer.MAX_VALUE}, new Object[]{Integer.MIN_VALUE, -2, Integer.MIN_VALUE},};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.lambdabehave;
|
||||
|
||||
import com.insightfullogic.lambdabehave.JunitSuiteRunner;
|
||||
import com.insightfullogic.lambdabehave.Suite;
|
||||
import com.insightfullogic.lambdabehave.generators.Generator;
|
||||
import com.insightfullogic.lambdabehave.generators.SourceGenerator;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JunitSuiteRunner.class)
|
||||
public class CalculatorTest {
|
||||
|
||||
private Calculator calculator;
|
||||
|
||||
{
|
||||
Suite.describe("Lambda behave example tests", it -> {
|
||||
|
||||
it.isSetupWith(() -> {
|
||||
calculator = new Calculator(1, 2);
|
||||
});
|
||||
it.should("Add the given numbers", expect -> {
|
||||
expect.that(calculator.add()).is(3);
|
||||
});
|
||||
it.should("Throw an exception if divide by 0", expect -> {
|
||||
expect.exception(ArithmeticException.class, () -> {
|
||||
calculator.divide(1, 0);
|
||||
});
|
||||
});
|
||||
it.uses(2, 3, 5)
|
||||
.and(23, 10, 33)
|
||||
.toShow("%d + %d = %d", (expect, a, b, c) -> {
|
||||
expect.that(calculator.add(a, b)).is(c);
|
||||
});
|
||||
it.requires(2)
|
||||
.example(Generator.asciiStrings())
|
||||
.toShow("Reversing a String twice returns the original String", (expect, str) -> {
|
||||
String same = new StringBuilder(str).reverse()
|
||||
.reverse()
|
||||
.toString();
|
||||
expect.that(same)
|
||||
.isEqualTo(str);
|
||||
});
|
||||
it.requires(2)
|
||||
.withSource(SourceGenerator.deterministicNumbers(5626689007407L))
|
||||
.example(Generator.asciiStrings())
|
||||
.toShow("Reversing a String twice returns the original String", (expect, str) -> {
|
||||
String same = new StringBuilder(str).reverse()
|
||||
.reverse()
|
||||
.toString();
|
||||
expect.that(same)
|
||||
.isEqualTo(str);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.mutation.test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.testing.mutation.Palindrome;
|
||||
|
||||
public class PalindromeUnitTest {
|
||||
@Test
|
||||
public void whenEmptyString_thanAccept() {
|
||||
Palindrome palindromeTester = new Palindrome();
|
||||
assertTrue(palindromeTester.isPalindrome("noon"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindrom_thanAccept() {
|
||||
Palindrome palindromeTester = new Palindrome();
|
||||
assertTrue(palindromeTester.isPalindrome("noon"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotPalindrom_thanReject(){
|
||||
Palindrome palindromeTester = new Palindrome();
|
||||
assertFalse(palindromeTester.isPalindrome("box"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNearPalindrom_thanReject(){
|
||||
Palindrome palindromeTester = new Palindrome();
|
||||
assertFalse(palindromeTester.isPalindrome("neon"));
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package com.baeldung.testing.assertj;
|
||||
|
||||
import org.assertj.core.util.Maps;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class AssertJCoreUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenComparingReferences_thenNotEqual() throws Exception {
|
||||
Dog fido = new Dog("Fido", 5.15f);
|
||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
||||
|
||||
assertThat(fido).isNotEqualTo(fidosClone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingFields_thenEqual() throws Exception {
|
||||
Dog fido = new Dog("Fido", 5.15f);
|
||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
||||
|
||||
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_thenContains() throws Exception {
|
||||
List<String> list = Arrays.asList("1", "2", "3");
|
||||
|
||||
assertThat(list).contains("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
|
||||
List<String> list = Arrays.asList("1", "2", "3");
|
||||
|
||||
assertThat(list).isNotEmpty();
|
||||
assertThat(list).startsWith("1");
|
||||
assertThat(list).doesNotContainNull();
|
||||
|
||||
assertThat(list).isNotEmpty().contains("1").startsWith("1").doesNotContainNull().containsSequence("2", "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingRunnable_thenIsInterface() throws Exception {
|
||||
assertThat(Runnable.class).isInterface();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingCharacter_thenIsUnicode() throws Exception {
|
||||
char someCharacter = 'c';
|
||||
|
||||
assertThat(someCharacter).isNotEqualTo('a').inUnicode().isGreaterThanOrEqualTo('b').isLowerCase();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
|
||||
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingWithOffset_thenEquals() throws Exception {
|
||||
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
|
||||
@Test
|
||||
public void whenRunningAssertion_thenDescribed() throws Exception {
|
||||
Person person = new Person("Alex", 34);
|
||||
|
||||
assertThat(person.getAge()).as("%s's age should be equal to 100").isEqualTo(100);
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.testing.assertj;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.TreeRangeMap;
|
||||
import com.google.common.io.Files;
|
||||
import org.assertj.guava.data.MapEntry;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.assertj.guava.api.Assertions.assertThat;
|
||||
import static org.assertj.guava.api.Assertions.entry;
|
||||
|
||||
public class AssertJGuavaUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
||||
final File temp1 = File.createTempFile("bael", "dung1");
|
||||
final File temp2 = File.createTempFile("bael", "dung2");
|
||||
|
||||
assertThat(Files.asByteSource(temp1)).hasSize(0).hasSameContentAs(Files.asByteSource(temp2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
||||
final Multimap<Integer, String> mmap = ArrayListMultimap.create();
|
||||
mmap.put(1, "one");
|
||||
mmap.put(1, "1");
|
||||
|
||||
assertThat(mmap).hasSize(2).containsKeys(1).contains(entry(1, "one")).contains(entry(1, "1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultimaps_whenVerifyingContent_thenCorrect() throws Exception {
|
||||
final Multimap<Integer, String> mmap1 = ArrayListMultimap.create();
|
||||
mmap1.put(1, "one");
|
||||
mmap1.put(1, "1");
|
||||
mmap1.put(2, "two");
|
||||
mmap1.put(2, "2");
|
||||
|
||||
final Multimap<Integer, String> mmap1_clone = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||
mmap1_clone.put(1, "one");
|
||||
mmap1_clone.put(1, "1");
|
||||
mmap1_clone.put(2, "two");
|
||||
mmap1_clone.put(2, "2");
|
||||
|
||||
final Multimap<Integer, String> mmap2 = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||
mmap2.put(1, "one");
|
||||
mmap2.put(1, "1");
|
||||
|
||||
assertThat(mmap1).containsAllEntriesOf(mmap2).containsAllEntriesOf(mmap1_clone).hasSameEntriesAs(mmap1_clone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
||||
final Optional<String> something = Optional.of("something");
|
||||
|
||||
assertThat(something).isPresent().extractingValue().isEqualTo("something");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final Range<String> range = Range.openClosed("a", "g");
|
||||
|
||||
assertThat(range).hasOpenedLowerBound().isNotEmpty().hasClosedUpperBound().contains("b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final TreeRangeMap<Integer, String> map = TreeRangeMap.create();
|
||||
|
||||
map.put(Range.closed(0, 60), "F");
|
||||
map.put(Range.closed(61, 70), "D");
|
||||
|
||||
assertThat(map).isNotEmpty().containsKeys(0).contains(MapEntry.entry(34, "F"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final Table<Integer, String, String> table = HashBasedTable.create(2, 2);
|
||||
|
||||
table.put(1, "A", "PRESENT");
|
||||
table.put(1, "B", "ABSENT");
|
||||
|
||||
assertThat(table).hasRowCount(1).containsValues("ABSENT").containsCell(1, "B", "ABSENT");
|
||||
}
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.testing.assertj;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.time.LocalDate.ofYearDay;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class AssertJJava8UnitTest {
|
||||
|
||||
@Test
|
||||
public void givenOptional_shouldAssert() throws Exception {
|
||||
final Optional<String> givenOptional = Optional.of("something");
|
||||
|
||||
assertThat(givenOptional).isPresent().hasValue("something");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPredicate_shouldAssert() throws Exception {
|
||||
final Predicate<String> predicate = s -> s.length() > 4;
|
||||
|
||||
assertThat(predicate).accepts("aaaaa", "bbbbb").rejects("a", "b").acceptsAll(asList("aaaaa", "bbbbb")).rejectsAll(asList("a", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_shouldAssert() throws Exception {
|
||||
final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
|
||||
final LocalDate todayDate = LocalDate.now();
|
||||
|
||||
assertThat(givenLocalDate).isBefore(LocalDate.of(2020, 7, 8)).isAfterOrEqualTo(LocalDate.of(1989, 7, 8));
|
||||
|
||||
assertThat(todayDate).isAfter(LocalDate.of(1989, 7, 8)).isToday();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTime_shouldAssert() throws Exception {
|
||||
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
|
||||
|
||||
assertThat(givenLocalDate).isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_shouldAssert() throws Exception {
|
||||
final LocalTime givenLocalTime = LocalTime.of(12, 15);
|
||||
|
||||
assertThat(givenLocalTime).isAfter(LocalTime.of(1, 0)).hasSameHourAs(LocalTime.of(12, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtracting() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::getYear).contains(2015);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::isLeapYear).contains(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtractingClass() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(Object::getClass).contains(LocalDate.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth).contains(2015, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_shouldSatisfy() throws Exception {
|
||||
final String givenString = "someString";
|
||||
|
||||
assertThat(givenString).satisfies(s -> {
|
||||
assertThat(s).isNotEmpty();
|
||||
assertThat(s).hasSize(10);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_shouldMatch() throws Exception {
|
||||
final String emptyString = "";
|
||||
|
||||
assertThat(emptyString).matches(String::isEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
|
||||
final List<String> givenList = Arrays.asList("");
|
||||
|
||||
assertThat(givenList).hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.testing.calculator;
|
||||
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(
|
||||
features = {"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}
|
||||
, plugin = {"pretty", "json:target/reports/json/calculator.json"}
|
||||
, glue = {"com.baeldung.cucumber.calculator"}
|
||||
)
|
||||
public class CalculatorIntegrationTest {
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.testing.calculator;
|
||||
|
||||
import com.baeldung.cucumber.Calculator;
|
||||
import cucumber.api.java.Before;
|
||||
import cucumber.api.java.en.Given;
|
||||
import cucumber.api.java.en.Then;
|
||||
import cucumber.api.java.en.When;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class CalculatorRunSteps {
|
||||
|
||||
private int total;
|
||||
|
||||
private Calculator calculator;
|
||||
|
||||
@Before
|
||||
private void init() {
|
||||
total = -999;
|
||||
|
||||
}
|
||||
|
||||
@Given("^I have a calculator$")
|
||||
public void initializeCalculator() throws Throwable {
|
||||
calculator = new Calculator();
|
||||
}
|
||||
|
||||
@When("^I add (-?\\d+) and (-?\\d+)$")
|
||||
public void testAdd(int num1, int num2) throws Throwable {
|
||||
total = calculator.add(num1, num2);
|
||||
}
|
||||
|
||||
@Then("^the result should be (-?\\d+)$")
|
||||
public void validateResult(int result) throws Throwable {
|
||||
Assert.assertThat(total, Matchers.equalTo(result));
|
||||
}
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.testing.jgotesting;
|
||||
|
||||
import java.io.File;
|
||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import org.jgotesting.rule.JGoTestRule;
|
||||
import static org.jgotesting.Assert.*; // same methods as org.junit.Assert.*
|
||||
import static org.jgotesting.Check.*; // ditto, with different names
|
||||
import static org.jgotesting.Testing.*;
|
||||
import org.jgotesting.Checker;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JGoTestingUnitTest {
|
||||
@Rule
|
||||
public final JGoTestRule test = new JGoTestRule();
|
||||
|
||||
@Test
|
||||
public void whenComparingIntegers_thenEqual() {
|
||||
int anInt = 10;
|
||||
|
||||
assertEquals(anInt, 10);
|
||||
checkEquals(anInt, 10);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenComparingNumbers_thenLoggedMessage() {
|
||||
log("There was something wrong when comparing numbers");
|
||||
|
||||
int anInt = 10;
|
||||
int anotherInt = 10;
|
||||
|
||||
checkEquals(anInt, 10);
|
||||
checkTrue("First number should be bigger", 10 > anotherInt);
|
||||
checkSame(anInt, anotherInt);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenComparingNumbers_thenFormattedMessage() {
|
||||
int anInt = 10;
|
||||
int anotherInt = 10;
|
||||
|
||||
logf("There was something wrong when comparing numbers %d and %d", anInt, anotherInt);
|
||||
|
||||
checkEquals(anInt, 10);
|
||||
checkTrue("First number should be bigger", 10 > anotherInt);
|
||||
checkSame(anInt, anotherInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingStrings_thenMultipleAssertions() {
|
||||
String aString = "This is a string";
|
||||
String anotherString = "This Is A String";
|
||||
|
||||
test.check(aString, equalToIgnoringCase(anotherString))
|
||||
.check(aString.length() == 16)
|
||||
.check(aString.startsWith("This"));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenComparingStrings_thenMultipleFailingAssertions() {
|
||||
String aString = "the test string";
|
||||
String anotherString = "The Test String";
|
||||
|
||||
checkEquals("Strings are not equal!", aString, anotherString);
|
||||
checkTrue("String is longer than one character", aString.length() == 1);
|
||||
checkSame("Strings are not the same", aString, anotherString);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void givenFile_whenDoesnotExists_thenTerminated() throws Exception {
|
||||
File aFile = new File("a_dummy_file.txt");
|
||||
terminateIf(aFile.exists(), is(false));
|
||||
|
||||
// This doesn't get executed
|
||||
checkEquals(aFile.getName(), "a_dummy_file.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChecker_whenComparingStrings_thenEqual() throws Exception {
|
||||
Checker<String> aChecker = s -> s.matches("\\d+");
|
||||
|
||||
String aString = "1235";
|
||||
test.check(aString, aChecker);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.testing.shopping;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(features = { "classpath:features/shopping.feature" })
|
||||
public class ShoppingIntegrationTest {
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.testing.shopping;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import cucumber.api.java8.En;
|
||||
|
||||
public class ShoppingStepsDef implements En {
|
||||
|
||||
private int budget = 0;
|
||||
|
||||
public ShoppingStepsDef() {
|
||||
|
||||
Given("I have (\\d+) in my wallet", (Integer money) -> budget = money);
|
||||
|
||||
When("I buy .* with (\\d+)", (Integer price) -> budget -= price);
|
||||
|
||||
Then("I should have (\\d+) in my wallet", (Integer finalBudget) -> {
|
||||
assertEquals(budget, finalBudget.intValue());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+561
@@ -0,0 +1,561 @@
|
||||
package com.baeldung.testing.truth;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.TreeBasedTable;
|
||||
import com.google.common.collect.TreeMultiset;
|
||||
import static com.baeldung.testing.truth.UserSubject.*;
|
||||
import static com.google.common.truth.Truth.*;
|
||||
import static com.google.common.truth.Truth8.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GoogleTruthUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenComparingInteger_thenEqual() {
|
||||
int anInt = 10;
|
||||
|
||||
assertThat(anInt).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingFloat_thenIsBigger() {
|
||||
float aFloat = 10.0f;
|
||||
|
||||
assertThat(aFloat).isGreaterThan(1.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingDouble_thenIsSmaller() {
|
||||
double aDouble = 10.0f;
|
||||
|
||||
assertThat(aDouble).isLessThan(20.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingFloat_thenWithinPrecision() {
|
||||
float aFloat = 23.04f;
|
||||
|
||||
assertThat(aFloat).isWithin(1.3f)
|
||||
.of(23.3f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingFloat_thenNotWithinPrecision() {
|
||||
float aFloat = 23.04f;
|
||||
|
||||
assertThat(aFloat).isNotWithin(1.3f)
|
||||
.of(100f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingDouble_thenWithinPrecision() {
|
||||
double aDouble = 22.18;
|
||||
|
||||
assertThat(aDouble).isWithin(2)
|
||||
.of(23d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingDouble_thenNotWithinPrecision() {
|
||||
double aDouble = 22.08;
|
||||
|
||||
assertThat(aDouble).isNotWithin(2)
|
||||
.of(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingBigDecimal_thenEqualIgnoringScale() {
|
||||
BigDecimal aBigDecimal = BigDecimal.valueOf(1000, 3);
|
||||
|
||||
assertThat(aBigDecimal).isEqualToIgnoringScale(new BigDecimal(1.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingBoolean_thenTrue() {
|
||||
boolean aBoolean = true;
|
||||
|
||||
assertThat(aBoolean).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingBoolean_thenFalse() {
|
||||
boolean aBoolean = false;
|
||||
|
||||
assertThat(aBoolean).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingArrays_thenEqual() {
|
||||
String[] firstArrayOfStrings = { "one", "two", "three" };
|
||||
String[] secondArrayOfStrings = { "one", "two", "three" };
|
||||
|
||||
assertThat(firstArrayOfStrings).isEqualTo(secondArrayOfStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingArrays_thenNotEqual() {
|
||||
String[] firstArrayOfStrings = { "one", "two", "three" };
|
||||
String[] secondArrayOfStrings = { "three", "two", "one" };
|
||||
|
||||
assertThat(firstArrayOfStrings).isNotEqualTo(secondArrayOfStrings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingArray_thenEmpty() {
|
||||
Object[] anArray = {};
|
||||
|
||||
assertThat(anArray).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingArray_thenNotEmpty() {
|
||||
String[] arrayOfStrings = { "One String " };
|
||||
|
||||
assertThat(arrayOfStrings).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingArrayOfDoubles_thenWithinPrecision() {
|
||||
double[] arrayOfDoubles = { 1, 2, 3, 4, 5 };
|
||||
|
||||
assertThat(arrayOfDoubles).hasValuesWithin(5)
|
||||
.of(6, 7, 8, 9, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUsers_thenEqual() {
|
||||
User aUser = new User("John Doe");
|
||||
User anotherUser = new User("John Doe");
|
||||
|
||||
assertThat(aUser).isEqualTo(anotherUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUser_thenIsNull() {
|
||||
User aUser = null;
|
||||
|
||||
assertThat(aUser).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUser_thenNotNull() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUser_thenInstanceOf() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).isInstanceOf(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUser_thenInList() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).isIn(Arrays.asList(1, 3, aUser, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUser_thenNotInList() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).isNotIn(Arrays.asList(1, 3, "Three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingNullUser_thenInList() {
|
||||
User aUser = null;
|
||||
User anotherUser = new User();
|
||||
|
||||
assertThat(aUser).isIn(Arrays.asList(1, 3, anotherUser, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenStartsWithString() {
|
||||
String aString = "This is a string";
|
||||
|
||||
assertThat(aString).startsWith("This");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenContainsString() {
|
||||
String aString = "This is a string";
|
||||
|
||||
assertThat(aString).contains("is a");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenEndsWithString() {
|
||||
String aString = "This is a string";
|
||||
|
||||
assertThat(aString).endsWith("string");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenExpectedLength() {
|
||||
String aString = "This is a string";
|
||||
|
||||
assertThat(aString).hasLength(16);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenEmpty() {
|
||||
String aString = "";
|
||||
|
||||
assertThat(aString).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_thenMatches() {
|
||||
String aString = "The string to match";
|
||||
|
||||
assertThat(aString).matches(Pattern.compile("[a-zA-Z\\s]+"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingComparable_thenAtLeast() {
|
||||
Comparable<Integer> aComparable = 5;
|
||||
|
||||
assertThat(aComparable).isAtLeast(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingComparable_thenAtMost() {
|
||||
Comparable<Integer> aComparable = 5;
|
||||
|
||||
assertThat(aComparable).isAtMost(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingComparable_thenInList() {
|
||||
Comparable<Integer> aComparable = 5;
|
||||
|
||||
assertThat(aComparable).isIn(Arrays.asList(4, 5, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingComparable_thenInRange() {
|
||||
Comparable<Integer> aComparable = 5;
|
||||
|
||||
assertThat(aComparable).isIn(Range.closed(1, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingComparable_thenNotInRange() {
|
||||
Comparable<Integer> aComparable = 5;
|
||||
|
||||
assertThat(aComparable).isNotIn(Range.closed(10, 15));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingUsers_thenEquivalent() {
|
||||
User aUser = new User();
|
||||
aUser.setName("John Doe");
|
||||
|
||||
User anotherUser = new User();
|
||||
anotherUser.setName("john doe");
|
||||
|
||||
assertThat(aUser).isEquivalentAccordingToCompareTo(anotherUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContains() {
|
||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
||||
|
||||
assertThat(aList).contains(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenDoesNotContains() {
|
||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
||||
|
||||
assertThat(aList).doesNotContain(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContainsAny() {
|
||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
||||
|
||||
assertThat(aList).containsAnyOf(0, 5, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContainsAnyInList() {
|
||||
List<Integer> aList = Arrays.asList(1, 2, 3);
|
||||
|
||||
assertThat(aList).containsAnyIn(Arrays.asList(1, 5, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenNoDuplicates() {
|
||||
List<Integer> aList = Arrays.asList(-2, -1, 0, 1, 2);
|
||||
|
||||
assertThat(aList).containsNoDuplicates();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContainsNoneOf() {
|
||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
||||
|
||||
assertThat(aList).containsNoneOf(9, 8, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContainsNoneIn() {
|
||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
||||
|
||||
assertThat(aList).containsNoneIn(Arrays.asList(9, 10, 11));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenContainsExactElements() {
|
||||
List<String> aList = Arrays.asList("10", "20", "30");
|
||||
List<String> anotherList = Arrays.asList("10", "20", "30");
|
||||
|
||||
assertThat(aList).containsExactlyElementsIn(anotherList)
|
||||
.inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIterable_thenOrdered() {
|
||||
Set<String> aSet = new LinkedHashSet<>(Arrays.asList("one", "three", "two"));
|
||||
|
||||
assertThat(aSet).isOrdered();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComparator_whenCheckingIterable_thenOrdered() {
|
||||
Comparator<String> aComparator = (a, b) -> new Float(a).compareTo(new Float(b));
|
||||
|
||||
List<String> aList = Arrays.asList("1", "012", "0020", "100");
|
||||
|
||||
assertThat(aList).isOrdered(aComparator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingMap_thenContainsEntry() {
|
||||
Map<String, Object> aMap = new HashMap<>();
|
||||
aMap.put("one", 1L);
|
||||
|
||||
assertThat(aMap).containsEntry("one", 1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingMap_thenContainsKey() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("one", 1L);
|
||||
|
||||
assertThat(map).containsKey("one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingMap_thenContainsEntries() {
|
||||
Map<String, Object> aMap = new HashMap<>();
|
||||
aMap.put("first", 1L);
|
||||
aMap.put("second", 2.0);
|
||||
aMap.put("third", 3f);
|
||||
|
||||
Map<String, Object> anotherMap = new HashMap<>(aMap);
|
||||
|
||||
assertThat(aMap).containsExactlyEntriesIn(anotherMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingException_thenInstanceOf() {
|
||||
Exception anException = new IllegalArgumentException(new NumberFormatException());
|
||||
|
||||
assertThat(anException).hasCauseThat()
|
||||
.isInstanceOf(NumberFormatException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingException_thenCauseMessageIsKnown() {
|
||||
Exception anException = new IllegalArgumentException("Bad value");
|
||||
|
||||
assertThat(anException).hasMessageThat()
|
||||
.startsWith("Bad");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingClass_thenIsAssignable() {
|
||||
Class<Double> aClass = Double.class;
|
||||
|
||||
assertThat(aClass).isAssignableTo(Number.class);
|
||||
}
|
||||
|
||||
// Java 8 Tests
|
||||
@Test
|
||||
public void whenCheckingJavaOptional_thenHasValue() {
|
||||
Optional<Integer> anOptional = Optional.of(1);
|
||||
|
||||
assertThat(anOptional).hasValue(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingJavaOptional_thenPresent() {
|
||||
Optional<String> anOptional = Optional.of("Baeldung");
|
||||
|
||||
assertThat(anOptional).isPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingJavaOptional_thenEmpty() {
|
||||
Optional anOptional = Optional.empty();
|
||||
|
||||
assertThat(anOptional).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingStream_thenContainsInOrder() {
|
||||
Stream<Integer> anStream = Stream.of(1, 2, 3);
|
||||
|
||||
assertThat(anStream).containsAllOf(1, 2, 3)
|
||||
.inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingStream_thenDoesNotContain() {
|
||||
Stream<Integer> anStream = IntStream.range(1, 100)
|
||||
.boxed();
|
||||
|
||||
assertThat(anStream).doesNotContain(0);
|
||||
}
|
||||
|
||||
// Guava Tests
|
||||
@Test
|
||||
public void whenCheckingGuavaOptional_thenIsAbsent() {
|
||||
com.google.common.base.Optional anOptional = com.google.common.base.Optional.absent();
|
||||
|
||||
assertThat(anOptional).isAbsent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaMultimap_thenExpectedSize() {
|
||||
Multimap<String, Object> aMultimap = ArrayListMultimap.create();
|
||||
aMultimap.put("one", 1L);
|
||||
aMultimap.put("one", 2.0);
|
||||
|
||||
assertThat(aMultimap).valuesForKey("one")
|
||||
.hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaMultiset_thenExpectedCount() {
|
||||
TreeMultiset<String> aMultiset = TreeMultiset.create();
|
||||
aMultiset.add("baeldung", 10);
|
||||
|
||||
assertThat(aMultiset).hasCount("baeldung", 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaTable_thenContains() {
|
||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
||||
|
||||
assertThat(aTable).contains("firstRow", "firstColumn");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaTable_thenContainsCell() {
|
||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
||||
|
||||
assertThat(aTable).containsCell("firstRow", "firstColumn", "baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaTable_thenContainsRow() {
|
||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
||||
|
||||
assertThat(aTable).containsRow("firstRow");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaTable_thenContainsColumn() {
|
||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
||||
|
||||
assertThat(aTable).containsColumn("firstColumn");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingGuavaTable_thenContainsValue() {
|
||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
||||
|
||||
assertThat(aTable).containsValue("baeldung");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenFailingAssertion_thenMessagePrefix() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).named("User [%s]", aUser.getName())
|
||||
.isNull();
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenFailingAssertion_thenCustomMessage() {
|
||||
User aUser = new User();
|
||||
|
||||
assertWithMessage("TEST-985: Secret user subject was NOT null!").that(aUser)
|
||||
.isNull();
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenFailingAssertion_thenCustomMessageAndPrefix() {
|
||||
User aUser = new User();
|
||||
|
||||
assertWithMessage("TEST-985: Secret user subject was NOT null!").that(aUser)
|
||||
.named("User [%s]", aUser.getName())
|
||||
.isNull();
|
||||
}
|
||||
|
||||
private Table<String, String, String> getDummyGuavaTable() {
|
||||
Table<String, String, String> aTable = TreeBasedTable.create();
|
||||
aTable.put("firstRow", "firstColumn", "baeldung");
|
||||
return aTable;
|
||||
}
|
||||
|
||||
// Custom User type
|
||||
@Test
|
||||
public void whenCheckingUser_thenHasName() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).hasName("John Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingUser_thenHasNameIgnoringCase() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).hasNameIgnoringCase("john doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUser_whenCheckingEmails_thenExpectedSize() {
|
||||
User aUser = new User();
|
||||
|
||||
assertThat(aUser).emails()
|
||||
.hasSize(2);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user