From 9ed70233ff537b0793b89323944642e4c0aa77a4 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Mon, 25 Feb 2019 11:42:51 +0530 Subject: [PATCH 01/14] BAEL2489 - Avoid check for null statement in Java --- core-java/pom.xml | 40 +++++++++++++------ .../java/com/baeldung/nulls/APIContracts.java | 29 ++++++++++++++ .../java/com/baeldung/nulls/Assertions.java | 13 ++++++ .../com/baeldung/nulls/EmptyCollections.java | 24 +++++++++++ .../baeldung/nulls/FindBugsAnnotations.java | 28 +++++++++++++ .../com/baeldung/nulls/Preconditions.java | 32 +++++++++++++++ .../baeldung/nulls/PrimitivesAndWrapper.java | 24 +++++++++++ .../java/com/baeldung/nulls/UsingLombok.java | 10 +++++ .../java/com/baeldung/nulls/UsingObjects.java | 24 +++++++++++ .../com/baeldung/nulls/UsingOptional.java | 23 +++++++++++ .../com/baeldung/nulls/UsingStringUtils.java | 17 ++++++++ 11 files changed, 251 insertions(+), 13 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/nulls/APIContracts.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/Assertions.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/Preconditions.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/UsingLombok.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/UsingObjects.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/UsingOptional.java create mode 100644 core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java diff --git a/core-java/pom.xml b/core-java/pom.xml index 463b65a4ce..e8548600f8 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -129,6 +129,13 @@ system ${java.home}/../lib/tools.jar + + org.jetbrains + annotations + ${intellij.annotations.version} + + + @@ -209,7 +216,8 @@ true - + org.baeldung.executable.ExecutableMavenJar @@ -264,12 +272,12 @@ -Xmx300m -XX:+UseParallelGC -classpath - + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - + org.apache.maven.plugins maven-javadoc-plugin @@ -330,7 +338,7 @@ java -classpath - + org.openjdk.jmh.Main .* @@ -363,7 +371,8 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + @@ -397,12 +406,14 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + com/baeldung/instrumentation/application/MyAtm.class - com/baeldung/instrumentation/application/MyAtmApplication.class + com/baeldung/instrumentation/application/MyAtmApplication.class + com/baeldung/instrumentation/application/Launcher.class @@ -432,12 +443,14 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + com/baeldung/instrumentation/agent/AtmTransformer.class - com/baeldung/instrumentation/agent/MyInstrumentationAgent.class + com/baeldung/instrumentation/agent/MyInstrumentationAgent.class + @@ -449,7 +462,7 @@ - + 2.8.2 @@ -470,12 +483,12 @@ 2.21.0 - + 1.1 1.4.197 2.1.0.1 1.19 - + 1.19 3.0.0-M1 3.0.2 @@ -486,7 +499,8 @@ 61.1 3.21.0-GA - + 1.8.0 + 16.0.2 diff --git a/core-java/src/main/java/com/baeldung/nulls/APIContracts.java b/core-java/src/main/java/com/baeldung/nulls/APIContracts.java new file mode 100644 index 0000000000..f8dfba25dd --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/APIContracts.java @@ -0,0 +1,29 @@ +package com.baeldung.nulls; + +public class APIContracts { + + /** + * Prints the value of {@code param} if not null. Prints {@code null} otherwise. + * @param param + */ + public void print(Object param) { + System.out.println("Printing " + param); + } + + /** + * + * @return non null result + * @throws Exception - if result is null + */ + public Object process() throws Exception { + Object result = doSomething(); + if (result == null) + throw new Exception("Processing fail. Got a null response"); + else + return result; + } + + private Object doSomething() { + return null; + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/Assertions.java b/core-java/src/main/java/com/baeldung/nulls/Assertions.java new file mode 100644 index 0000000000..a0d692623f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/Assertions.java @@ -0,0 +1,13 @@ +package com.baeldung.nulls; + +public class Assertions { + + public void accept(Object param){ + assert param != null; + + doSomething(param); + } + + private void doSomething(Object param) { + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java b/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java new file mode 100644 index 0000000000..95e49e4e91 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java @@ -0,0 +1,24 @@ +package com.baeldung.nulls; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class EmptyCollections { + + public List names(){ + if (userExist()) + return Stream.of(readName()).collect(Collectors.toList()); + else + return Collections.emptyList(); + } + + private boolean userExist() { + return false; + } + + private String readName() { + return "test"; + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java b/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java new file mode 100644 index 0000000000..f879f033c9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java @@ -0,0 +1,28 @@ +package com.baeldung.nulls; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class FindBugsAnnotations { + + public void accept(@Nonnull Object param) { + System.out.println(param.toString()); + } + + public void print(@Nullable Object param) { + System.out.println("Printing " + param); + } + + @Nonnull + public Object process() throws Exception { + Object result = doSomething(); + if (result == null) + throw new Exception("Processing fail. Got a null response"); + else + return result; + } + + private Object doSomething() { + return null; + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java new file mode 100644 index 0000000000..ff32db46dd --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java @@ -0,0 +1,32 @@ +package com.baeldung.nulls; + +public class Preconditions { + + public void goodAccept(String one, String two, String three) { + if (null == one || null == two || three == null) + throw new IllegalArgumentException(); + } + + public void badAccept(String one, String two, String three){ + if (null == one) + throw new IllegalArgumentException(); + else + process(one); + + if (null == two) + throw new IllegalArgumentException(); + else + process(two); + + if (null == three) + throw new IllegalArgumentException(); + else + process(three); + + } + + private void process(String one) { + } + + +} diff --git a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java new file mode 100644 index 0000000000..dce7a05308 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java @@ -0,0 +1,24 @@ +package com.baeldung.nulls; + +public class PrimitivesAndWrapper { + + public static int sum(int a, int b) { + return a + b; + } + + public static Integer sum(Integer a, Integer b) { + return a + b; + } + + public static Integer goodSum(Integer a, Integer b) { + if (a != null && b != null) + return a + b; + else + throw new IllegalArgumentException(); + } + + public static void main(String[] args) { + sum(0, 0); + sum(null, null); + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingLombok.java b/core-java/src/main/java/com/baeldung/nulls/UsingLombok.java new file mode 100644 index 0000000000..73db0742c8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/UsingLombok.java @@ -0,0 +1,10 @@ +package com.baeldung.nulls; + +import lombok.NonNull; + +public class UsingLombok { + + public void accept(@NonNull Object param){ + System.out.println(param); + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java new file mode 100644 index 0000000000..e8a3262ce7 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java @@ -0,0 +1,24 @@ +package com.baeldung.nulls; + +import java.util.Objects; + +public class UsingObjects { + + private String checked; + + public void accept(Object param) { + try { + Objects.requireNonNull(param); + } catch (NullPointerException e) { + //doSomethingElseToo + e.printStackTrace(); + } + } + + public void caller() throws Exception { + if (Objects.nonNull(checked)) + accept(checked); + else + throw new Exception(); + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java b/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java new file mode 100644 index 0000000000..626afc311d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java @@ -0,0 +1,23 @@ +package com.baeldung.nulls; + +import java.util.Optional; + +public class UsingOptional { + + public Optional process() { + if (isProcessed()) + return Optional.of("dummy"); + else + return Optional.empty(); + } + + public void caller() { + Optional result = process(); + result.ifPresent(p -> System.out.println(p)); + result.orElseThrow(() -> new IllegalArgumentException()); + } + + private boolean isProcessed() { + return false; + } +} diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java b/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java new file mode 100644 index 0000000000..8cbf3752e1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java @@ -0,0 +1,17 @@ +package com.baeldung.nulls; + + +import org.apache.commons.lang3.StringUtils; + +public class UsingStringUtils { + + public void accept(String param) { + if (StringUtils.isNotEmpty(param)) + System.out.println(param); + } + + public void acceptOnlyNonBlank(String param) { + if (StringUtils.isNotBlank(param)) + System.out.println(param); + } +} From b1a4f6bb219a26f652a9820ca94ab07602780119 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 16 Mar 2019 11:35:29 +0530 Subject: [PATCH 02/14] BAEL2489 Avoid check for null statement in Java --- core-java/pom.xml | 26 ++++------- .../java/com/baeldung/nulls/APIContracts.java | 7 +-- .../com/baeldung/nulls/Preconditions.java | 15 ++++--- .../baeldung/nulls/PrimitivesAndWrapper.java | 8 +--- .../java/com/baeldung/nulls/UsingObjects.java | 16 ++----- .../com/baeldung/nulls/UsingOptional.java | 26 ++++++----- .../com/baeldung/nulls/UsingStringUtils.java | 11 +++-- .../nulls/PrimitivesAndWrapperTest.java | 35 +++++++++++++++ .../com/baeldung/nulls/UsingLombokTest.java | 25 +++++++++++ .../com/baeldung/nulls/UsingObjectsTest.java | 31 +++++++++++++ .../com/baeldung/nulls/UsingOptionalTest.java | 37 ++++++++++++++++ .../baeldung/nulls/UsingStringUtilsTest.java | 43 +++++++++++++++++++ 12 files changed, 221 insertions(+), 59 deletions(-) create mode 100644 core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java create mode 100644 core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java create mode 100644 core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java create mode 100644 core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java create mode 100644 core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java diff --git a/core-java/pom.xml b/core-java/pom.xml index e8548600f8..9c5a17d25c 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -216,8 +216,7 @@ true - + org.baeldung.executable.ExecutableMavenJar @@ -272,7 +271,7 @@ -Xmx300m -XX:+UseParallelGC -classpath - + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed @@ -338,7 +337,7 @@ java -classpath - + org.openjdk.jmh.Main .* @@ -371,8 +370,7 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - + ${project.build.outputDirectory}/META-INF/MANIFEST.MF @@ -406,14 +404,12 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - + ${project.build.outputDirectory}/META-INF/MANIFEST.MF com/baeldung/instrumentation/application/MyAtm.class - com/baeldung/instrumentation/application/MyAtmApplication.class - + com/baeldung/instrumentation/application/MyAtmApplication.class com/baeldung/instrumentation/application/Launcher.class @@ -443,14 +439,12 @@ true - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - + ${project.build.outputDirectory}/META-INF/MANIFEST.MF com/baeldung/instrumentation/agent/AtmTransformer.class - com/baeldung/instrumentation/agent/MyInstrumentationAgent.class - + com/baeldung/instrumentation/agent/MyInstrumentationAgent.class @@ -462,7 +456,6 @@ - 2.8.2 @@ -483,12 +476,10 @@ 2.21.0 - 1.1 1.4.197 2.1.0.1 1.19 - 1.19 3.0.0-M1 3.0.2 @@ -499,7 +490,6 @@ 61.1 3.21.0-GA - 1.8.0 16.0.2 diff --git a/core-java/src/main/java/com/baeldung/nulls/APIContracts.java b/core-java/src/main/java/com/baeldung/nulls/APIContracts.java index f8dfba25dd..7d6abf53b8 100644 --- a/core-java/src/main/java/com/baeldung/nulls/APIContracts.java +++ b/core-java/src/main/java/com/baeldung/nulls/APIContracts.java @@ -4,6 +4,7 @@ public class APIContracts { /** * Prints the value of {@code param} if not null. Prints {@code null} otherwise. + * * @param param */ public void print(Object param) { @@ -11,16 +12,16 @@ public class APIContracts { } /** - * * @return non null result * @throws Exception - if result is null */ public Object process() throws Exception { Object result = doSomething(); - if (result == null) + if (result == null) { throw new Exception("Processing fail. Got a null response"); - else + } else { return result; + } } private Object doSomething() { diff --git a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java index ff32db46dd..447026f1e3 100644 --- a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java +++ b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java @@ -3,22 +3,26 @@ package com.baeldung.nulls; public class Preconditions { public void goodAccept(String one, String two, String three) { - if (null == one || null == two || three == null) + if (one == null || two == null || three == null) throw new IllegalArgumentException(); + + process(one); + process(two); + process(three); } - public void badAccept(String one, String two, String three){ - if (null == one) + public void badAccept(String one, String two, String three) { + if (one == null) throw new IllegalArgumentException(); else process(one); - if (null == two) + if (two == null) throw new IllegalArgumentException(); else process(two); - if (null == three) + if (three == null) throw new IllegalArgumentException(); else process(three); @@ -28,5 +32,4 @@ public class Preconditions { private void process(String one) { } - } diff --git a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java index dce7a05308..8f662b3760 100644 --- a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java +++ b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java @@ -2,11 +2,11 @@ package com.baeldung.nulls; public class PrimitivesAndWrapper { - public static int sum(int a, int b) { + public static int primitiveSum(int a, int b) { return a + b; } - public static Integer sum(Integer a, Integer b) { + public static Integer wrapperSum(Integer a, Integer b) { return a + b; } @@ -17,8 +17,4 @@ public class PrimitivesAndWrapper { throw new IllegalArgumentException(); } - public static void main(String[] args) { - sum(0, 0); - sum(null, null); - } } diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java index e8a3262ce7..7383ae84a7 100644 --- a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java +++ b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java @@ -4,21 +4,13 @@ import java.util.Objects; public class UsingObjects { - private String checked; - - public void accept(Object param) { + public void accept(Object param) throws Exception { try { Objects.requireNonNull(param); } catch (NullPointerException e) { - //doSomethingElseToo - e.printStackTrace(); - } - } - - public void caller() throws Exception { - if (Objects.nonNull(checked)) - accept(checked); - else throw new Exception(); + } + + //doSomething() } } diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java b/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java index 626afc311d..6c17290a72 100644 --- a/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java +++ b/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java @@ -4,20 +4,24 @@ import java.util.Optional; public class UsingOptional { - public Optional process() { - if (isProcessed()) - return Optional.of("dummy"); - else + public Optional process(boolean processed) { + + String response = doSomething(processed); + + if (response == null) { return Optional.empty(); + } + + return Optional.of(response); } - public void caller() { - Optional result = process(); - result.ifPresent(p -> System.out.println(p)); - result.orElseThrow(() -> new IllegalArgumentException()); + private String doSomething(boolean processed) { + + if (processed) { + return "passed"; + } else { + return null; + } } - private boolean isProcessed() { - return false; - } } diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java b/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java index 8cbf3752e1..c7c73b73eb 100644 --- a/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java +++ b/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java @@ -1,17 +1,22 @@ package com.baeldung.nulls; - import org.apache.commons.lang3.StringUtils; public class UsingStringUtils { public void accept(String param) { - if (StringUtils.isNotEmpty(param)) + if (StringUtils.isNotEmpty(param)) { System.out.println(param); + } else { + throw new IllegalArgumentException(); + } } public void acceptOnlyNonBlank(String param) { - if (StringUtils.isNotBlank(param)) + if (StringUtils.isNotBlank(param)) { System.out.println(param); + } else { + throw new IllegalArgumentException(); + } } } diff --git a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java new file mode 100644 index 0000000000..4e0f44e555 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java @@ -0,0 +1,35 @@ +package com.baeldung.nulls; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class PrimitivesAndWrapperTest { + + @Test + public void givenWrappers_whenBothArgsNonNull_thenReturnResult() { + + Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0); + + assertEquals(0, sum.intValue()); + } + + @Test() + public void givenWrappers_whenOneArgIsNull_thenThrowNullPointerException() { + assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2)); + } + + @Test() + public void givenWrappers_whenBothArgsAreNull_thenThrowNullPointerException() { + assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null)); + } + + @Test() + public void givenWrappersWithNullCheck_whenAnyArgIsNull_thenThrowIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2)); + } + + + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java new file mode 100644 index 0000000000..5a41d444a4 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java @@ -0,0 +1,25 @@ +package com.baeldung.nulls; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class UsingLombokTest { + + private UsingLombok classUnderTest; + + @BeforeEach + public void setup() { + classUnderTest = new UsingLombok(); + } + + @Test + public void whenNullArg_thenThrowNullPointerException() { + + assertThrows(NullPointerException.class, () -> classUnderTest.accept(null)); + + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java new file mode 100644 index 0000000000..74170d980b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java @@ -0,0 +1,31 @@ +package com.baeldung.nulls; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class UsingObjectsTest { + + private UsingObjects classUnderTest; + + @BeforeEach + public void setup() { + classUnderTest = new UsingObjects(); + } + + @Test + public void whenArgIsNull_thenThrowException() { + + assertThrows(Exception.class, () -> classUnderTest.accept(null)); + } + + @Test + public void whenArgIsNonNull_thenDoesNotThrowException() { + + assertDoesNotThrow(() -> classUnderTest.accept("test ")); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java new file mode 100644 index 0000000000..5c42b9dff1 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java @@ -0,0 +1,37 @@ +package com.baeldung.nulls; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class UsingOptionalTest { + + private UsingOptional classUnderTest; + + @BeforeEach + public void setup() { + classUnderTest = new UsingOptional(); + } + + @Test + public void whenArgIsFalse_thenReturnEmptyResponse() { + Optional result = classUnderTest.process(false); + assertFalse(result.isPresent()); + } + + @Test + public void whenArgIsTrue_thenReturnValidResponse() { + Optional result = classUnderTest.process(true); + assertTrue(result.isPresent()); + } + + @Test + public void whenArgIsFalse_thenChainResponseAndThrowException() { + assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception())); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java new file mode 100644 index 0000000000..d10364b85e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java @@ -0,0 +1,43 @@ +package com.baeldung.nulls; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class UsingStringUtilsTest { + + private UsingStringUtils classUnderTest; + + @BeforeEach + public void setup() { + classUnderTest = new UsingStringUtils(); + } + + @Test + public void givenAccept_whenArgIsNull_throwIllegalArgumentException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null)); + } + + @Test + public void givenAccept_whenArgIsEmpty_throwIllegalArgumentException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept("")); + } + + @Test + public void givenAcceptOnlyNonBlank_whenArgIsNull_throwIllegalArgumentException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null)); + } + + @Test + public void givenAcceptOnlyNonBlank_whenArgIsEmpty_throwIllegalArgumentException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank("")); + } + + @Test + public void givenAcceptOnlyNonBlank_whenArgIsBlank_throwIllegalArgumentException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" ")); + } + +} \ No newline at end of file From 55f7da49485868a475e191f504bd1166d655f8b0 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 16 Mar 2019 11:51:03 +0530 Subject: [PATCH 03/14] BAEL2489 Avoid check for null statement in Java --- ...vesAndWrapperTest.java => PrimitivesAndWrapperUnitTest.java} | 2 +- .../nulls/{UsingLombokTest.java => UsingLombokUnitTest.java} | 2 +- .../nulls/{UsingObjectsTest.java => UsingObjectsUnitTest.java} | 2 +- .../{UsingOptionalTest.java => UsingOptionalUnitTest.java} | 2 +- ...{UsingStringUtilsTest.java => UsingStringUtilsUnitTest.java} | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename core-java/src/test/java/com/baeldung/nulls/{PrimitivesAndWrapperTest.java => PrimitivesAndWrapperUnitTest.java} (96%) rename core-java/src/test/java/com/baeldung/nulls/{UsingLombokTest.java => UsingLombokUnitTest.java} (94%) rename core-java/src/test/java/com/baeldung/nulls/{UsingObjectsTest.java => UsingObjectsUnitTest.java} (95%) rename core-java/src/test/java/com/baeldung/nulls/{UsingOptionalTest.java => UsingOptionalUnitTest.java} (97%) rename core-java/src/test/java/com/baeldung/nulls/{UsingStringUtilsTest.java => UsingStringUtilsUnitTest.java} (97%) diff --git a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java similarity index 96% rename from core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java rename to core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java index 4e0f44e555..67fe173883 100644 --- a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; -class PrimitivesAndWrapperTest { +class PrimitivesAndWrapperUnitTest { @Test public void givenWrappers_whenBothArgsNonNull_thenReturnResult() { diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java similarity index 94% rename from core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java rename to core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java index 5a41d444a4..c157ad6630 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingLombokTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -class UsingLombokTest { +class UsingLombokUnitTest { private UsingLombok classUnderTest; diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java similarity index 95% rename from core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java rename to core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java index 74170d980b..6939caa4fb 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingObjectsTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class UsingObjectsTest { +class UsingObjectsUnitTest { private UsingObjects classUnderTest; diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java similarity index 97% rename from core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java rename to core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java index 5c42b9dff1..45367204b7 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java @@ -9,7 +9,7 @@ import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class UsingOptionalTest { +class UsingOptionalUnitTest { private UsingOptional classUnderTest; diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java similarity index 97% rename from core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java rename to core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java index d10364b85e..451775abe1 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; -class UsingStringUtilsTest { +class UsingStringUtilsUnitTest { private UsingStringUtils classUnderTest; From 66146ea761ecb3ef0fb01ea26807da0ca41dd3ac Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Tue, 19 Mar 2019 20:40:26 +0530 Subject: [PATCH 04/14] BAEL2489 Avoid check for null statement in Java --- .../com/baeldung/nulls/EmptyCollections.java | 7 ++++--- .../baeldung/nulls/FindBugsAnnotations.java | 5 +++-- .../com/baeldung/nulls/Preconditions.java | 18 ++++++++++------- .../baeldung/nulls/PrimitivesAndWrapper.java | 5 +++-- .../java/com/baeldung/nulls/UsingObjects.java | 11 +++------- .../nulls/PrimitivesAndWrapperUnitTest.java | 8 ++++---- .../baeldung/nulls/UsingOptionalUnitTest.java | 5 +++++ .../nulls/UsingStringUtilsUnitTest.java | 20 +++++++++---------- 8 files changed, 43 insertions(+), 36 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java b/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java index 95e49e4e91..5958cf8dc6 100644 --- a/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java +++ b/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java @@ -7,11 +7,12 @@ import java.util.stream.Stream; public class EmptyCollections { - public List names(){ - if (userExist()) + public List names() { + if (userExist()) { return Stream.of(readName()).collect(Collectors.toList()); - else + } else { return Collections.emptyList(); + } } private boolean userExist() { diff --git a/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java b/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java index f879f033c9..a3804f6af5 100644 --- a/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java +++ b/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java @@ -16,10 +16,11 @@ public class FindBugsAnnotations { @Nonnull public Object process() throws Exception { Object result = doSomething(); - if (result == null) + if (result == null) { throw new Exception("Processing fail. Got a null response"); - else + } else { return result; + } } private Object doSomething() { diff --git a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java index 447026f1e3..9d9633a13e 100644 --- a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java +++ b/core-java/src/main/java/com/baeldung/nulls/Preconditions.java @@ -3,8 +3,9 @@ package com.baeldung.nulls; public class Preconditions { public void goodAccept(String one, String two, String three) { - if (one == null || two == null || three == null) + if (one == null || two == null || three == null) { throw new IllegalArgumentException(); + } process(one); process(two); @@ -12,20 +13,23 @@ public class Preconditions { } public void badAccept(String one, String two, String three) { - if (one == null) + if (one == null) { throw new IllegalArgumentException(); - else + } else { process(one); + } - if (two == null) + if (two == null) { throw new IllegalArgumentException(); - else + } else { process(two); + } - if (three == null) + if (three == null) { throw new IllegalArgumentException(); - else + } else { process(three); + } } diff --git a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java index 8f662b3760..c75918c486 100644 --- a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java +++ b/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java @@ -11,10 +11,11 @@ public class PrimitivesAndWrapper { } public static Integer goodSum(Integer a, Integer b) { - if (a != null && b != null) + if (a != null && b != null) { return a + b; - else + } else { throw new IllegalArgumentException(); + } } } diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java index 7383ae84a7..5384edee5e 100644 --- a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java +++ b/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java @@ -4,13 +4,8 @@ import java.util.Objects; public class UsingObjects { - public void accept(Object param) throws Exception { - try { - Objects.requireNonNull(param); - } catch (NullPointerException e) { - throw new Exception(); - } - - //doSomething() + public void accept(Object param) { + Objects.requireNonNull(param); + // doSomething() } } diff --git a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java index 67fe173883..3c524387a4 100644 --- a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test; class PrimitivesAndWrapperUnitTest { @Test - public void givenWrappers_whenBothArgsNonNull_thenReturnResult() { + public void givenBothArgsNonNull_whenCallingWrapperSum_thenReturnSum() { Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0); @@ -16,17 +16,17 @@ class PrimitivesAndWrapperUnitTest { } @Test() - public void givenWrappers_whenOneArgIsNull_thenThrowNullPointerException() { + public void givenOneArgIsNull_whenCallingWrapperSum_thenThrowNullPointerException() { assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2)); } @Test() - public void givenWrappers_whenBothArgsAreNull_thenThrowNullPointerException() { + public void givenBothArgsNull_whenCallingWrapperSum_thenThrowNullPointerException() { assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null)); } @Test() - public void givenWrappersWithNullCheck_whenAnyArgIsNull_thenThrowIllegalArgumentException() { + public void givenOneArgNull_whenCallingGoodSum_thenThrowIllegalArgumentException() { assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2)); } diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java index 45367204b7..d9d245369b 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java @@ -20,18 +20,23 @@ class UsingOptionalUnitTest { @Test public void whenArgIsFalse_thenReturnEmptyResponse() { + Optional result = classUnderTest.process(false); + assertFalse(result.isPresent()); } @Test public void whenArgIsTrue_thenReturnValidResponse() { + Optional result = classUnderTest.process(true); + assertTrue(result.isPresent()); } @Test public void whenArgIsFalse_thenChainResponseAndThrowException() { + assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception())); } } \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java index 451775abe1..99b1dbe6b4 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java +++ b/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java @@ -16,28 +16,28 @@ class UsingStringUtilsUnitTest { } @Test - public void givenAccept_whenArgIsNull_throwIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null)); + public void givenArgIsNull_whenCallingAccept_throwIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null)); } @Test - public void givenAccept_whenArgIsEmpty_throwIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept("")); + public void givenArgIsEmpty_whenCallingAccept_throwIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept("")); } @Test - public void givenAcceptOnlyNonBlank_whenArgIsNull_throwIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null)); + public void givenArgIsNull_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null)); } @Test - public void givenAcceptOnlyNonBlank_whenArgIsEmpty_throwIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank("")); + public void givenArgIsEmpty_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank("")); } @Test - public void givenAcceptOnlyNonBlank_whenArgIsBlank_throwIllegalArgumentException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" ")); + public void givenArgIsBlank_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" ")); } } \ No newline at end of file From c7c5d56eb392038c8f577537c44eeedcdf1ae7c5 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Tue, 26 Mar 2019 21:47:51 +0530 Subject: [PATCH 05/14] BAEL2489 Avoid check for null statement in Java --- patterns/design-patterns-2/pom.xml | 18 ++++++++++++++++++ .../java/com/baeldung/nulls/APIContracts.java | 0 .../java/com/baeldung/nulls/Assertions.java | 0 .../com/baeldung/nulls/EmptyCollections.java | 0 .../baeldung/nulls/FindBugsAnnotations.java | 9 +++++---- .../java/com/baeldung/nulls/Preconditions.java | 0 .../baeldung/nulls/PrimitivesAndWrapper.java | 0 .../java/com/baeldung/nulls/UsingLombok.java | 0 .../java/com/baeldung/nulls/UsingObjects.java | 0 .../java/com/baeldung/nulls/UsingOptional.java | 0 .../com/baeldung/nulls/UsingStringUtils.java | 0 .../nulls/PrimitivesAndWrapperUnitTest.java | 4 ++-- .../baeldung/nulls/UsingLombokUnitTest.java | 3 +-- .../baeldung/nulls/UsingObjectsUnitTest.java | 9 ++++----- .../baeldung/nulls/UsingOptionalUnitTest.java | 10 +++++----- .../nulls/UsingStringUtilsUnitTest.java | 3 +-- 16 files changed, 36 insertions(+), 20 deletions(-) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/APIContracts.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/Assertions.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/EmptyCollections.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java (78%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/Preconditions.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/UsingLombok.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/UsingObjects.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/UsingOptional.java (100%) rename {core-java => patterns/design-patterns-2}/src/main/java/com/baeldung/nulls/UsingStringUtils.java (100%) rename {core-java => patterns/design-patterns-2}/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java (99%) rename {core-java => patterns/design-patterns-2}/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java (82%) rename {core-java => patterns/design-patterns-2}/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java (84%) rename {core-java => patterns/design-patterns-2}/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java (99%) rename {core-java => patterns/design-patterns-2}/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java (93%) diff --git a/patterns/design-patterns-2/pom.xml b/patterns/design-patterns-2/pom.xml index 2a0213065b..5c3e70b046 100644 --- a/patterns/design-patterns-2/pom.xml +++ b/patterns/design-patterns-2/pom.xml @@ -15,11 +15,29 @@ + + org.jetbrains + annotations + ${intellij.annotations.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + UTF-8 1.8 1.8 + 16.0.2 + 3.5 diff --git a/core-java/src/main/java/com/baeldung/nulls/APIContracts.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/APIContracts.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/APIContracts.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/APIContracts.java diff --git a/core-java/src/main/java/com/baeldung/nulls/Assertions.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/Assertions.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/Assertions.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/Assertions.java diff --git a/core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/EmptyCollections.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/EmptyCollections.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/EmptyCollections.java diff --git a/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java similarity index 78% rename from core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java index a3804f6af5..697d5e4959 100644 --- a/core-java/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/FindBugsAnnotations.java @@ -1,11 +1,12 @@ package com.baeldung.nulls; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + public class FindBugsAnnotations { - public void accept(@Nonnull Object param) { + public void accept(@NotNull Object param) { System.out.println(param.toString()); } @@ -13,7 +14,7 @@ public class FindBugsAnnotations { System.out.println("Printing " + param); } - @Nonnull + @NotNull public Object process() throws Exception { Object result = doSomething(); if (result == null) { diff --git a/core-java/src/main/java/com/baeldung/nulls/Preconditions.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/Preconditions.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/Preconditions.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/Preconditions.java diff --git a/core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/PrimitivesAndWrapper.java diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingLombok.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingLombok.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/UsingLombok.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingLombok.java diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingObjects.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingObjects.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/UsingObjects.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingObjects.java diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingOptional.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/UsingOptional.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java diff --git a/core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingStringUtils.java similarity index 100% rename from core-java/src/main/java/com/baeldung/nulls/UsingStringUtils.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingStringUtils.java diff --git a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java similarity index 99% rename from core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java index 3c524387a4..49655619f6 100644 --- a/core-java/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/PrimitivesAndWrapperUnitTest.java @@ -1,10 +1,10 @@ package com.baeldung.nulls; +import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.Test; - class PrimitivesAndWrapperUnitTest { @Test diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java similarity index 82% rename from core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java index c157ad6630..b322344aba 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingLombokUnitTest.java @@ -1,10 +1,9 @@ package com.baeldung.nulls; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; class UsingLombokUnitTest { diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java similarity index 84% rename from core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java index 6939caa4fb..e1f5a288e6 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingObjectsUnitTest.java @@ -1,12 +1,11 @@ package com.baeldung.nulls; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - class UsingObjectsUnitTest { private UsingObjects classUnderTest; @@ -19,7 +18,7 @@ class UsingObjectsUnitTest { @Test public void whenArgIsNull_thenThrowException() { - assertThrows(Exception.class, () -> classUnderTest.accept(null)); + assertThrows(NullPointerException.class, () -> classUnderTest.accept(null)); } @Test diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java similarity index 99% rename from core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java index d9d245369b..8f896cedfa 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingOptionalUnitTest.java @@ -1,14 +1,14 @@ package com.baeldung.nulls; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Optional; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - class UsingOptionalUnitTest { private UsingOptional classUnderTest; diff --git a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java similarity index 93% rename from core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java index 99b1dbe6b4..f7c51a7dc5 100644 --- a/core-java/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/nulls/UsingStringUtilsUnitTest.java @@ -1,10 +1,9 @@ package com.baeldung.nulls; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; class UsingStringUtilsUnitTest { From 187199f924c6d3b4fc2c0387176f8bfab33f4751 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Wed, 27 Mar 2019 21:25:19 +0530 Subject: [PATCH 06/14] BAEL2489 Avoid check for null statement in Java - Removing unused pom changes --- core-java/pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index 9c5a17d25c..ff5235096a 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -129,12 +129,6 @@ system ${java.home}/../lib/tools.jar - - org.jetbrains - annotations - ${intellij.annotations.version} - - @@ -491,6 +485,5 @@ 3.21.0-GA 1.8.0 - 16.0.2 From de1c689114edd968f3d37c4baa127e8b06a3f2f4 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Thu, 28 Mar 2019 22:03:57 +0530 Subject: [PATCH 07/14] BAEL2489 Avoid check for null statement in Java - Removing unused pom changes --- core-java/pom.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index ff5235096a..06f126e1d0 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -129,7 +129,6 @@ system ${java.home}/../lib/tools.jar - @@ -450,6 +449,7 @@ + 2.8.2 @@ -470,10 +470,12 @@ 2.21.0 + 1.1 1.4.197 2.1.0.1 1.19 + 1.19 3.0.0-M1 3.0.2 @@ -484,6 +486,7 @@ 61.1 3.21.0-GA + 1.8.0 From 0aeed3e745a08294bcbb716cea42b02e12178819 Mon Sep 17 00:00:00 2001 From: dev-chirag <41482403+dev-chirag@users.noreply.github.com> Date: Thu, 28 Mar 2019 22:28:14 +0530 Subject: [PATCH 08/14] Delete pom.xml Removing unused changes in core-java --- core-java/pom.xml | 492 ---------------------------------------------- 1 file changed, 492 deletions(-) delete mode 100644 core-java/pom.xml diff --git a/core-java/pom.xml b/core-java/pom.xml deleted file mode 100644 index 06f126e1d0..0000000000 --- a/core-java/pom.xml +++ /dev/null @@ -1,492 +0,0 @@ - - 4.0.0 - core-java - 0.1.0-SNAPSHOT - core-java - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../parent-java - - - - - - commons-io - commons-io - ${commons-io.version} - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - org.unix4j - unix4j-command - ${unix4j.version} - - - com.googlecode.grep4j - grep4j - ${grep4j.version} - - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - com.google.code.gson - gson - ${gson.version} - - - - log4j - log4j - ${log4j.version} - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - org.javamoney - moneta - ${javamoney.moneta.version} - - - org.owasp.esapi - esapi - ${esapi.version} - - - com.sun.messaging.mq - fscontext - ${fscontext.version} - - - com.codepoetics - protonpack - ${protonpack.version} - - - one.util - streamex - ${streamex.version} - - - io.vavr - vavr - ${vavr.version} - - - org.openjdk.jmh - jmh-core - ${jmh-core.version} - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh-generator-annprocess.version} - - - com.h2database - h2 - ${h2database.version} - - - - org.javassist - javassist - ${javaassist.version} - - - com.sun - tools - ${sun.tools.version} - system - ${java.home}/../lib/tools.jar - - - - - core-java - - - src/main/resources - true - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - - org.apache.maven.plugins - maven-jar-plugin - ${maven-jar-plugin.version} - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - - - - org.apache.maven.plugins - maven-shade-plugin - ${maven-shade-plugin.version} - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - - - - com.jolira - onejar-maven-plugin - ${onejar-maven-plugin.version} - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - - - - org.springframework.boot - spring-boot-maven-plugin - ${spring-boot-maven-plugin.version} - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - - - - org.codehaus.mojo - exec-maven-plugin - ${exec-maven-plugin.version} - - java - com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - - -Xmx300m - -XX:+UseParallelGC - -classpath - - com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - 1.8 - 1.8 - - - - - - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - **/*IntTest.java - - - - - - - json - - - - - org.codehaus.mojo - exec-maven-plugin - ${exec-maven-plugin.version} - - - run-benchmarks - - none - - exec - - - test - java - - -classpath - - org.openjdk.jmh.Main - .* - - - - - - - - - - - - buildAgentLoader - - - - org.apache.maven.plugins - maven-jar-plugin - - - package - - jar - - - agentLoader - target/classes - - - true - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - com/baeldung/instrumentation/application/AgentLoader.class - com/baeldung/instrumentation/application/Launcher.class - - - - - - - - - - buildApplication - - - - org.apache.maven.plugins - maven-jar-plugin - - - package - - jar - - - application - target/classes - - - true - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - com/baeldung/instrumentation/application/MyAtm.class - com/baeldung/instrumentation/application/MyAtmApplication.class - com/baeldung/instrumentation/application/Launcher.class - - - - - - - - - - buildAgent - - - - org.apache.maven.plugins - maven-jar-plugin - - - package - - jar - - - agent - target/classes - - - true - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - com/baeldung/instrumentation/agent/AtmTransformer.class - com/baeldung/instrumentation/agent/MyInstrumentationAgent.class - - - - - - - - - - - - - - 2.8.2 - - - 3.5 - 2.5 - 3.6.1 - 1.0.3 - 0.4 - 1.8.7 - 4.6-b01 - 1.13 - 0.6.5 - 0.9.0 - - - 3.10.0 - - - 2.21.0 - - 1.1 - 1.4.197 - 2.1.0.1 - 1.19 - - 1.19 - 3.0.0-M1 - 3.0.2 - 1.4.4 - 3.1.1 - 2.0.3.RELEASE - 1.6.0 - 61.1 - - 3.21.0-GA - - 1.8.0 - - From 4eb3c8567a65ef3fb7b7626130f21b9c1b0cfceb Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Thu, 28 Mar 2019 22:43:31 +0530 Subject: [PATCH 09/14] BAEL2489 Avoid check for null statement in Java - Removing unused pom changes --- core-java/pom.xml | 492 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 492 insertions(+) create mode 100644 core-java/pom.xml diff --git a/core-java/pom.xml b/core-java/pom.xml new file mode 100644 index 0000000000..463b65a4ce --- /dev/null +++ b/core-java/pom.xml @@ -0,0 +1,492 @@ + + 4.0.0 + core-java + 0.1.0-SNAPSHOT + core-java + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + commons-io + commons-io + ${commons-io.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.unix4j + unix4j-command + ${unix4j.version} + + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.google.code.gson + gson + ${gson.version} + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + org.javamoney + moneta + ${javamoney.moneta.version} + + + org.owasp.esapi + esapi + ${esapi.version} + + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + + com.codepoetics + protonpack + ${protonpack.version} + + + one.util + streamex + ${streamex.version} + + + io.vavr + vavr + ${vavr.version} + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator-annprocess.version} + + + com.h2database + h2 + ${h2database.version} + + + + org.javassist + javassist + ${javaassist.version} + + + com.sun + tools + ${sun.tools.version} + system + ${java.home}/../lib/tools.jar + + + + + core-java + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + + + + com.jolira + onejar-maven-plugin + ${onejar-maven-plugin.version} + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot-maven-plugin.version} + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + 1.8 + 1.8 + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + **/*IntTest.java + + + + + + + json + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + + + + buildAgentLoader + + + + org.apache.maven.plugins + maven-jar-plugin + + + package + + jar + + + agentLoader + target/classes + + + true + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + com/baeldung/instrumentation/application/AgentLoader.class + com/baeldung/instrumentation/application/Launcher.class + + + + + + + + + + buildApplication + + + + org.apache.maven.plugins + maven-jar-plugin + + + package + + jar + + + application + target/classes + + + true + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + com/baeldung/instrumentation/application/MyAtm.class + com/baeldung/instrumentation/application/MyAtmApplication.class + com/baeldung/instrumentation/application/Launcher.class + + + + + + + + + + buildAgent + + + + org.apache.maven.plugins + maven-jar-plugin + + + package + + jar + + + agent + target/classes + + + true + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + com/baeldung/instrumentation/agent/AtmTransformer.class + com/baeldung/instrumentation/agent/MyInstrumentationAgent.class + + + + + + + + + + + + + + 2.8.2 + + + 3.5 + 2.5 + 3.6.1 + 1.0.3 + 0.4 + 1.8.7 + 4.6-b01 + 1.13 + 0.6.5 + 0.9.0 + + + 3.10.0 + + + 2.21.0 + + 1.1 + 1.4.197 + 2.1.0.1 + 1.19 + + 1.19 + 3.0.0-M1 + 3.0.2 + 1.4.4 + 3.1.1 + 2.0.3.RELEASE + 1.6.0 + 61.1 + + 3.21.0-GA + + 1.8.0 + + From 6b104db2bbb3e90af6ac94ffe8cd78c0b08765bc Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Tue, 2 Apr 2019 17:14:37 +0530 Subject: [PATCH 10/14] BAEL2489 Avoid check for null statement in Java - adding ofnullable to Optional example --- .../src/main/java/com/baeldung/nulls/UsingOptional.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java index 6c17290a72..d5dd56e760 100644 --- a/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/nulls/UsingOptional.java @@ -8,11 +8,7 @@ public class UsingOptional { String response = doSomething(processed); - if (response == null) { - return Optional.empty(); - } - - return Optional.of(response); + return Optional.ofNullable(response); } private String doSomething(boolean processed) { From f0277ca4b0cafb0ef4a670b05e08ebe6319fc600 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 27 Apr 2019 22:36:54 +0530 Subject: [PATCH 11/14] BAEL2848 JUnit Tagging and Filtering Tests --- testing-modules/junit-5-2/pom.xml | 167 ++++++++++++++++++ .../baeldung/junit/tags/example/Employee.java | 44 +++++ .../junit/tags/example/EmployeeDAO.java | 66 +++++++ .../junit/tags/example/EmployeeRowMapper.java | 21 +++ .../junit/tags/example/SpringJdbcConfig.java | 20 +++ .../src/main/resources/jdbc/schema.sql | 7 + .../main/resources/jdbc/springJdbc-config.xml | 19 ++ .../src/main/resources/jdbc/test-data.sql | 7 + .../EmployeeDAOCategoryIntegrationTest.java | 67 +++++++ .../categories/EmployeeDAOUnitTestSuite.java | 12 ++ .../baeldung/categories/IntegrationTest.java | 4 + .../com/baeldung/categories/UnitTest.java | 4 + .../example/EmployeeDAOIntegrationTest.java | 42 +++++ .../junit/tags/example/EmployeeUnitTest.java | 41 +++++ .../EmployeeDAOCategoryIntegrationTest.java | 69 ++++++++ .../tags/EmployeeDAOIntegrationTest.java | 70 ++++++++ .../baeldung/tags/EmployeeDAOTestSuite.java | 12 ++ testing-modules/pom.xml | 1 + 18 files changed, 673 insertions(+) create mode 100644 testing-modules/junit-5-2/pom.xml create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java create mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml create mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java diff --git a/testing-modules/junit-5-2/pom.xml b/testing-modules/junit-5-2/pom.xml new file mode 100644 index 0000000000..ec535f8413 --- /dev/null +++ b/testing-modules/junit-5-2/pom.xml @@ -0,0 +1,167 @@ + + + 4.0.0 + + junit-5-2 + 1.0-SNAPSHOT + junit-5-2 + JUnit-5 extended + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.platform + junit-platform-engine + ${junit.platform.version} + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit.vintage.version} + test + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + + + com.h2database + h2 + ${h2.version} + + + org.springframework + spring-test + ${spring.version} + test + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + + + + + + src/test/resources + true + + + src/main/resources + true + + + + + + + filtering + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + **/*IntegrationTest.java + + + + + + + + category + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + com.baeldung.categories.UnitTest + com.baeldung.categories.IntegrationTest + + + + + + + tags + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + UnitTest + IntegrationTest + + + + + + + + + + 5.3.1 + 2.23.0 + 1.2.0 + 5.2.0 + 2.8.2 + 1.4.196 + 2.21.0 + 1.6.0 + 5.0.6.RELEASE + + + diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java new file mode 100644 index 0000000000..66f41ee885 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.junit.tags.example; + +public class Employee { + private int id; + + private String firstName; + + private String lastName; + + private String address; + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(final String lastName) { + this.lastName = lastName; + } + + public String getAddress() { + return address; + } + + public void setAddress(final String address) { + this.address = address; + } + +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java new file mode 100644 index 0000000000..21375dbed1 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java @@ -0,0 +1,66 @@ +package com.baeldung.junit.tags.example; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BatchPreparedStatementSetter; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.namedparam.SqlParameterSource; +import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +@Repository +public class EmployeeDAO { + + private JdbcTemplate jdbcTemplate; + + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + private SimpleJdbcInsert simpleJdbcInsert; + + @Autowired + public void setDataSource(final DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE"); + + } + + public int getCountOfEmployees() { + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class); + } + + public List getAllEmployees() { + return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper()); + } + + public int addEmplyee(final int id) { + return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA"); + } + + public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { + final Map parameters = new HashMap(); + parameters.put("ID", emp.getId()); + parameters.put("FIRST_NAME", emp.getFirstName()); + parameters.put("LAST_NAME", emp.getLastName()); + parameters.put("ADDRESS", emp.getAddress()); + + return simpleJdbcInsert.execute(parameters); + } + + // for testing + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java new file mode 100644 index 0000000000..f480aac9a2 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java @@ -0,0 +1,21 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class EmployeeRowMapper implements RowMapper { + + @Override + public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException { + final Employee employee = new Employee(); + + employee.setId(rs.getInt("ID")); + employee.setFirstName(rs.getString("FIRST_NAME")); + employee.setLastName(rs.getString("LAST_NAME")); + employee.setAddress(rs.getString("ADDRESS")); + + return employee; + } +} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java new file mode 100644 index 0000000000..3ddef7e5d4 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; + +@Configuration +@ComponentScan("com.baeldung.junit.tags.example") +public class SpringJdbcConfig { + + @Bean + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build(); + } +} \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql new file mode 100644 index 0000000000..c86d35cdae --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql @@ -0,0 +1,7 @@ +CREATE TABLE EMPLOYEE +( + ID int NOT NULL PRIMARY KEY, + FIRST_NAME varchar(255), + LAST_NAME varchar(255), + ADDRESS varchar(255), +); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml b/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml new file mode 100644 index 0000000000..5fd2699b41 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql new file mode 100644 index 0000000000..b9ef8fec25 --- /dev/null +++ b/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql @@ -0,0 +1,7 @@ +INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada'); + +INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA'); + +INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland'); + +INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA'); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java new file mode 100644 index 0000000000..202ac728a6 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java @@ -0,0 +1,67 @@ +package com.baeldung.categories; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOCategoryIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Category(IntegrationTest.class) + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Category(UnitTest.class) + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java new file mode 100644 index 0000000000..252e7ffe64 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.categories; + +import org.junit.experimental.categories.Categories; +import org.junit.experimental.categories.Categories.IncludeCategory; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Categories.class) +@IncludeCategory(UnitTest.class) +@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class) +public class EmployeeDAOUnitTestSuite { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java new file mode 100644 index 0000000000..9ced880ffc --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface IntegrationTest { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java new file mode 100644 index 0000000000..3790fc1d91 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface UnitTest { +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..9b95b3701d --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.junit.tags.example; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Test + public void testQueryMethod() { + Assert.assertEquals(employeeDao.getAllEmployees().size(), 4); + } + + @Test + public void testUpdateMethod() { + Assert.assertEquals(employeeDao.addEmplyee(5), 1); + } + + @Test + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(11); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java new file mode 100644 index 0000000000..6ffb5586cc --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.junit.tags.example; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.jdbc.core.JdbcTemplate; + +public class EmployeeUnitTest { + + @Mock + private JdbcTemplate jdbcTemplate; + + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java new file mode 100644 index 0000000000..1b1518337d --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java @@ -0,0 +1,69 @@ +package com.baeldung.tags; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.categories.IntegrationTest; +import com.baeldung.categories.UnitTest; +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOCategoryIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Category(IntegrationTest.class) + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Category(UnitTest.class) + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..7c16f5eb29 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.tags; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Tag("IntegrationTest") + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assertions.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Tag("UnitTest") + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assertions.assertEquals(1, countOfEmployees); + } + +} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java new file mode 100644 index 0000000000..783e5a81a2 --- /dev/null +++ b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.tags; + +import org.junit.platform.runner.JUnitPlatform; +import org.junit.platform.suite.api.IncludeTags; +import org.junit.platform.suite.api.SelectPackages; +import org.junit.runner.RunWith; + +@RunWith(JUnitPlatform.class) +@SelectPackages("com.baeldung.tags") +@IncludeTags("UnitTest") +public class EmployeeDAOTestSuite { +} diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 39047fb756..45952efb00 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -32,5 +32,6 @@ test-containers testing testng + junit-5-2 From 236d9ccc0f136c53df5146020cd118f4bfbce06b Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 4 May 2019 16:10:31 +0530 Subject: [PATCH 12/14] BAEL2848 JUnit Tagging and Filtering Tests --- testing-modules/junit-5-configuration/pom.xml | 118 ++++++++++++++++++ .../baeldung/junit/tags/example/Employee.java | 44 +++++++ .../junit/tags/example/EmployeeDAO.java | 58 +++++++++ .../junit/tags/example/EmployeeRowMapper.java | 21 ++++ .../junit/tags/example/SpringJdbcConfig.java | 19 +++ .../src/main/resources/jdbc/schema.sql | 7 ++ .../main/resources/jdbc/springJdbc-config.xml | 19 +++ .../src/main/resources/jdbc/test-data.sql | 7 ++ .../EmployeeDAOCategoryIntegrationTest.java | 66 ++++++++++ .../categories/EmployeeDAOUnitTestSuite.java | 12 ++ .../baeldung/categories/IntegrationTest.java | 4 + .../com/baeldung/categories/UnitTest.java | 4 + .../example/EmployeeDAOIntegrationTest.java | 41 ++++++ .../baeldung/example/EmployeeUnitTest.java | 40 ++++++ .../tags/EmployeeDAOIntegrationTest.java | 65 ++++++++++ .../baeldung/tags/EmployeeDAOTestSuite.java | 12 ++ 16 files changed, 537 insertions(+) create mode 100644 testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/Employee.java create mode 100644 testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java create mode 100644 testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java create mode 100644 testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java create mode 100644 testing-modules/junit-5-configuration/src/main/resources/jdbc/schema.sql create mode 100644 testing-modules/junit-5-configuration/src/main/resources/jdbc/springJdbc-config.xml create mode 100644 testing-modules/junit-5-configuration/src/main/resources/jdbc/test-data.sql create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/IntegrationTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/UnitTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeUnitTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java create mode 100644 testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java diff --git a/testing-modules/junit-5-configuration/pom.xml b/testing-modules/junit-5-configuration/pom.xml index aa488ebb8b..f420af5429 100644 --- a/testing-modules/junit-5-configuration/pom.xml +++ b/testing-modules/junit-5-configuration/pom.xml @@ -16,11 +16,55 @@ + + org.junit.platform + junit-platform-engine + ${junit.platform.version} + org.junit.jupiter junit-jupiter-params ${junit.jupiter.version} + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit.vintage.version} + test + + + com.h2database + h2 + ${h2.version} + + + org.springframework + spring-test + ${spring.version} + test + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + @@ -29,13 +73,87 @@ src/test/resources true + + src/main/resources + true + + + + + filtering + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + **/*IntegrationTest.java + + + + + + + + category + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + com.baeldung.categories.UnitTest + com.baeldung.categories.IntegrationTest + + + + + + + tags + + false + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + UnitTest + IntegrationTest + + + + + + + + 5.3.1 1.2.0 5.2.0 + 1.4.196 + 5.0.6.RELEASE + 2.21.0 diff --git a/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/Employee.java b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/Employee.java new file mode 100644 index 0000000000..66f41ee885 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.junit.tags.example; + +public class Employee { + private int id; + + private String firstName; + + private String lastName; + + private String address; + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(final String lastName) { + this.lastName = lastName; + } + + public String getAddress() { + return address; + } + + public void setAddress(final String address) { + this.address = address; + } + +} diff --git a/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java new file mode 100644 index 0000000000..2e9016018e --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java @@ -0,0 +1,58 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Repository +public class EmployeeDAO { + + private JdbcTemplate jdbcTemplate; + + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + private SimpleJdbcInsert simpleJdbcInsert; + + @Autowired + public void setDataSource(final DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE"); + + } + + public int getCountOfEmployees() { + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class); + } + + public List getAllEmployees() { + return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper()); + } + + public int addEmplyee(final int id) { + return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA"); + } + + public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { + final Map parameters = new HashMap(); + parameters.put("ID", emp.getId()); + parameters.put("FIRST_NAME", emp.getFirstName()); + parameters.put("LAST_NAME", emp.getLastName()); + parameters.put("ADDRESS", emp.getAddress()); + + return simpleJdbcInsert.execute(parameters); + } + + // for testing + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java new file mode 100644 index 0000000000..f480aac9a2 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java @@ -0,0 +1,21 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class EmployeeRowMapper implements RowMapper { + + @Override + public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException { + final Employee employee = new Employee(); + + employee.setId(rs.getInt("ID")); + employee.setFirstName(rs.getString("FIRST_NAME")); + employee.setLastName(rs.getString("LAST_NAME")); + employee.setAddress(rs.getString("ADDRESS")); + + return employee; + } +} diff --git a/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java new file mode 100644 index 0000000000..dc82409df9 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.junit.tags.example; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; + +@Configuration +@ComponentScan("com.baeldung.junit.tags.example") +public class SpringJdbcConfig { + + @Bean + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build(); + } +} \ No newline at end of file diff --git a/testing-modules/junit-5-configuration/src/main/resources/jdbc/schema.sql b/testing-modules/junit-5-configuration/src/main/resources/jdbc/schema.sql new file mode 100644 index 0000000000..c86d35cdae --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/resources/jdbc/schema.sql @@ -0,0 +1,7 @@ +CREATE TABLE EMPLOYEE +( + ID int NOT NULL PRIMARY KEY, + FIRST_NAME varchar(255), + LAST_NAME varchar(255), + ADDRESS varchar(255), +); \ No newline at end of file diff --git a/testing-modules/junit-5-configuration/src/main/resources/jdbc/springJdbc-config.xml b/testing-modules/junit-5-configuration/src/main/resources/jdbc/springJdbc-config.xml new file mode 100644 index 0000000000..5fd2699b41 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/resources/jdbc/springJdbc-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/junit-5-configuration/src/main/resources/jdbc/test-data.sql b/testing-modules/junit-5-configuration/src/main/resources/jdbc/test-data.sql new file mode 100644 index 0000000000..b9ef8fec25 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/main/resources/jdbc/test-data.sql @@ -0,0 +1,7 @@ +INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada'); + +INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA'); + +INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland'); + +INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA'); \ No newline at end of file diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java new file mode 100644 index 0000000000..6aa1652321 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.categories; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOCategoryIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Category(IntegrationTest.class) + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Category(UnitTest.class) + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } + +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java new file mode 100644 index 0000000000..252e7ffe64 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.categories; + +import org.junit.experimental.categories.Categories; +import org.junit.experimental.categories.Categories.IncludeCategory; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Categories.class) +@IncludeCategory(UnitTest.class) +@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class) +public class EmployeeDAOUnitTestSuite { +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/IntegrationTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/IntegrationTest.java new file mode 100644 index 0000000000..9ced880ffc --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/IntegrationTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface IntegrationTest { +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/UnitTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/UnitTest.java new file mode 100644 index 0000000000..3790fc1d91 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/categories/UnitTest.java @@ -0,0 +1,4 @@ +package com.baeldung.categories; + +public interface UnitTest { +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..d3b6a52726 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeDAOIntegrationTest.java @@ -0,0 +1,41 @@ +package com.baeldung.example; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Test + public void testQueryMethod() { + Assert.assertEquals(employeeDao.getAllEmployees().size(), 4); + } + + @Test + public void testUpdateMethod() { + Assert.assertEquals(employeeDao.addEmplyee(5), 1); + } + + @Test + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(11); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeUnitTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeUnitTest.java new file mode 100644 index 0000000000..f706af94ad --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/example/EmployeeUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.example; + +import com.baeldung.junit.tags.example.EmployeeDAO; +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.jdbc.core.JdbcTemplate; + +public class EmployeeUnitTest { + + @Mock + private JdbcTemplate jdbcTemplate; + + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); + } +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java new file mode 100644 index 0000000000..bcd76cb4c2 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java @@ -0,0 +1,65 @@ +package com.baeldung.tags; + +import com.baeldung.junit.tags.example.Employee; +import com.baeldung.junit.tags.example.EmployeeDAO; +import com.baeldung.junit.tags.example.SpringJdbcConfig; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EmployeeDAOIntegrationTest { + + @Autowired + private EmployeeDAO employeeDao; + + @Mock + private JdbcTemplate jdbcTemplate; + private EmployeeDAO employeeDAO; + + @BeforeEach + public void setup() { + MockitoAnnotations.initMocks(this); + employeeDAO = new EmployeeDAO(); + employeeDAO.setJdbcTemplate(jdbcTemplate); + } + + @Test + @Tag("IntegrationTest") + public void testAddEmployeeUsingSimpelJdbcInsert() { + final Employee emp = new Employee(); + emp.setId(12); + emp.setFirstName("testFirstName"); + emp.setLastName("testLastName"); + emp.setAddress("testAddress"); + + Assertions.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); + } + + @Test + @Tag("UnitTest") + public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { + + // given + Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) + .thenReturn(1); + + // when + int countOfEmployees = employeeDAO.getCountOfEmployees(); + + // then + Assertions.assertEquals(1, countOfEmployees); + } + +} diff --git a/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java new file mode 100644 index 0000000000..783e5a81a2 --- /dev/null +++ b/testing-modules/junit-5-configuration/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java @@ -0,0 +1,12 @@ +package com.baeldung.tags; + +import org.junit.platform.runner.JUnitPlatform; +import org.junit.platform.suite.api.IncludeTags; +import org.junit.platform.suite.api.SelectPackages; +import org.junit.runner.RunWith; + +@RunWith(JUnitPlatform.class) +@SelectPackages("com.baeldung.tags") +@IncludeTags("UnitTest") +public class EmployeeDAOTestSuite { +} From b15ba7ed4b5c8293fc136523f95f5297c7df2e88 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 4 May 2019 16:14:14 +0530 Subject: [PATCH 13/14] BAEL2848 JUnit Tagging and Filtering Tests --- testing-modules/junit-5-2/pom.xml | 167 ------------------ .../baeldung/junit/tags/example/Employee.java | 44 ----- .../junit/tags/example/EmployeeDAO.java | 66 ------- .../junit/tags/example/EmployeeRowMapper.java | 21 --- .../junit/tags/example/SpringJdbcConfig.java | 20 --- .../src/main/resources/jdbc/schema.sql | 7 - .../main/resources/jdbc/springJdbc-config.xml | 19 -- .../src/main/resources/jdbc/test-data.sql | 7 - .../EmployeeDAOCategoryIntegrationTest.java | 67 ------- .../categories/EmployeeDAOUnitTestSuite.java | 12 -- .../baeldung/categories/IntegrationTest.java | 4 - .../com/baeldung/categories/UnitTest.java | 4 - .../example/EmployeeDAOIntegrationTest.java | 42 ----- .../junit/tags/example/EmployeeUnitTest.java | 41 ----- .../EmployeeDAOCategoryIntegrationTest.java | 69 -------- .../tags/EmployeeDAOIntegrationTest.java | 70 -------- .../baeldung/tags/EmployeeDAOTestSuite.java | 12 -- 17 files changed, 672 deletions(-) delete mode 100644 testing-modules/junit-5-2/pom.xml delete mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java delete mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java delete mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java delete mode 100644 testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java delete mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql delete mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml delete mode 100644 testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java delete mode 100644 testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java diff --git a/testing-modules/junit-5-2/pom.xml b/testing-modules/junit-5-2/pom.xml deleted file mode 100644 index ec535f8413..0000000000 --- a/testing-modules/junit-5-2/pom.xml +++ /dev/null @@ -1,167 +0,0 @@ - - - 4.0.0 - - junit-5-2 - 1.0-SNAPSHOT - junit-5-2 - JUnit-5 extended - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - - org.junit.platform - junit-platform-engine - ${junit.platform.version} - - - org.junit.jupiter - junit-jupiter-params - ${junit.jupiter.version} - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - org.junit.vintage - junit-vintage-engine - ${junit.vintage.version} - test - - - org.junit.jupiter - junit-jupiter-migrationsupport - ${junit.vintage.version} - test - - - org.apache.logging.log4j - log4j-core - ${log4j2.version} - - - com.h2database - h2 - ${h2.version} - - - org.springframework - spring-test - ${spring.version} - test - - - org.springframework - spring-context - ${spring.version} - - - org.springframework - spring-orm - ${spring.version} - - - - - - - - src/test/resources - true - - - src/main/resources - true - - - - - - - filtering - - false - - - - - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - - - - **/*IntegrationTest.java - - - - - - - - category - - false - - - - - maven-surefire-plugin - ${maven-surefire-plugin.version} - - com.baeldung.categories.UnitTest - com.baeldung.categories.IntegrationTest - - - - - - - tags - - false - - - - - maven-surefire-plugin - ${maven-surefire-plugin.version} - - UnitTest - IntegrationTest - - - - - - - - - - 5.3.1 - 2.23.0 - 1.2.0 - 5.2.0 - 2.8.2 - 1.4.196 - 2.21.0 - 1.6.0 - 5.0.6.RELEASE - - - diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java deleted file mode 100644 index 66f41ee885..0000000000 --- a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/Employee.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.junit.tags.example; - -public class Employee { - private int id; - - private String firstName; - - private String lastName; - - private String address; - - public int getId() { - return id; - } - - public void setId(final int id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(final String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(final String lastName) { - this.lastName = lastName; - } - - public String getAddress() { - return address; - } - - public void setAddress(final String address) { - this.address = address; - } - -} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java deleted file mode 100644 index 21375dbed1..0000000000 --- a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeDAO.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.baeldung.junit.tags.example; - -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.BatchPreparedStatementSetter; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.jdbc.core.namedparam.SqlParameterSource; -import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; -import org.springframework.stereotype.Repository; - -@Repository -public class EmployeeDAO { - - private JdbcTemplate jdbcTemplate; - - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - private SimpleJdbcInsert simpleJdbcInsert; - - @Autowired - public void setDataSource(final DataSource dataSource) { - jdbcTemplate = new JdbcTemplate(dataSource); - - namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE"); - - } - - public int getCountOfEmployees() { - return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class); - } - - public List getAllEmployees() { - return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper()); - } - - public int addEmplyee(final int id) { - return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA"); - } - - public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { - final Map parameters = new HashMap(); - parameters.put("ID", emp.getId()); - parameters.put("FIRST_NAME", emp.getFirstName()); - parameters.put("LAST_NAME", emp.getLastName()); - parameters.put("ADDRESS", emp.getAddress()); - - return simpleJdbcInsert.execute(parameters); - } - - // for testing - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } -} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java deleted file mode 100644 index f480aac9a2..0000000000 --- a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/EmployeeRowMapper.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.junit.tags.example; - -import org.springframework.jdbc.core.RowMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -public class EmployeeRowMapper implements RowMapper { - - @Override - public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException { - final Employee employee = new Employee(); - - employee.setId(rs.getInt("ID")); - employee.setFirstName(rs.getString("FIRST_NAME")); - employee.setLastName(rs.getString("LAST_NAME")); - employee.setAddress(rs.getString("ADDRESS")); - - return employee; - } -} diff --git a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java b/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java deleted file mode 100644 index 3ddef7e5d4..0000000000 --- a/testing-modules/junit-5-2/src/main/java/com/baeldung/junit/tags/example/SpringJdbcConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.junit.tags.example; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; - -import javax.sql.DataSource; - -@Configuration -@ComponentScan("com.baeldung.junit.tags.example") -public class SpringJdbcConfig { - - @Bean - public DataSource dataSource() { - return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build(); - } -} \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql deleted file mode 100644 index c86d35cdae..0000000000 --- a/testing-modules/junit-5-2/src/main/resources/jdbc/schema.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE EMPLOYEE -( - ID int NOT NULL PRIMARY KEY, - FIRST_NAME varchar(255), - LAST_NAME varchar(255), - ADDRESS varchar(255), -); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml b/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml deleted file mode 100644 index 5fd2699b41..0000000000 --- a/testing-modules/junit-5-2/src/main/resources/jdbc/springJdbc-config.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql b/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql deleted file mode 100644 index b9ef8fec25..0000000000 --- a/testing-modules/junit-5-2/src/main/resources/jdbc/test-data.sql +++ /dev/null @@ -1,7 +0,0 @@ -INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada'); - -INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA'); - -INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland'); - -INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA'); \ No newline at end of file diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java deleted file mode 100644 index 202ac728a6..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOCategoryIntegrationTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.baeldung.categories; - -import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -import com.baeldung.junit.tags.example.Employee; -import com.baeldung.junit.tags.example.EmployeeDAO; -import com.baeldung.junit.tags.example.SpringJdbcConfig; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) -public class EmployeeDAOCategoryIntegrationTest { - - @Autowired - private EmployeeDAO employeeDao; - - @Mock - private JdbcTemplate jdbcTemplate; - private EmployeeDAO employeeDAO; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - employeeDAO = new EmployeeDAO(); - employeeDAO.setJdbcTemplate(jdbcTemplate); - } - - @Test - @Category(IntegrationTest.class) - public void testAddEmployeeUsingSimpelJdbcInsert() { - final Employee emp = new Employee(); - emp.setId(12); - emp.setFirstName("testFirstName"); - emp.setLastName("testLastName"); - emp.setAddress("testAddress"); - - Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); - } - - @Test - @Category(UnitTest.class) - public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { - - // given - Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) - .thenReturn(1); - - // when - int countOfEmployees = employeeDAO.getCountOfEmployees(); - - // then - Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); - } - -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java deleted file mode 100644 index 252e7ffe64..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/EmployeeDAOUnitTestSuite.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.categories; - -import org.junit.experimental.categories.Categories; -import org.junit.experimental.categories.Categories.IncludeCategory; -import org.junit.runner.RunWith; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Categories.class) -@IncludeCategory(UnitTest.class) -@SuiteClasses(EmployeeDAOCategoryIntegrationTest.class) -public class EmployeeDAOUnitTestSuite { -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java deleted file mode 100644 index 9ced880ffc..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/IntegrationTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.baeldung.categories; - -public interface IntegrationTest { -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java deleted file mode 100644 index 3790fc1d91..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/categories/UnitTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.baeldung.categories; - -public interface UnitTest { -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java deleted file mode 100644 index 9b95b3701d..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeDAOIntegrationTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.junit.tags.example; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) -public class EmployeeDAOIntegrationTest { - - @Autowired - private EmployeeDAO employeeDao; - - @Test - public void testQueryMethod() { - Assert.assertEquals(employeeDao.getAllEmployees().size(), 4); - } - - @Test - public void testUpdateMethod() { - Assert.assertEquals(employeeDao.addEmplyee(5), 1); - } - - @Test - public void testAddEmployeeUsingSimpelJdbcInsert() { - final Employee emp = new Employee(); - emp.setId(11); - emp.setFirstName("testFirstName"); - emp.setLastName("testLastName"); - emp.setAddress("testAddress"); - - Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); - } -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java deleted file mode 100644 index 6ffb5586cc..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/junit/tags/example/EmployeeUnitTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.junit.tags.example; - -import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.jdbc.core.JdbcTemplate; - -public class EmployeeUnitTest { - - @Mock - private JdbcTemplate jdbcTemplate; - - private EmployeeDAO employeeDAO; - - @BeforeEach - public void setup() { - MockitoAnnotations.initMocks(this); - employeeDAO = new EmployeeDAO(); - employeeDAO.setJdbcTemplate(jdbcTemplate); - } - - @Test - public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { - - // given - Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) - .thenReturn(1); - - // when - int countOfEmployees = employeeDAO.getCountOfEmployees(); - - // then - Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); - } -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java deleted file mode 100644 index 1b1518337d..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOCategoryIntegrationTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.baeldung.tags; - -import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -import com.baeldung.categories.IntegrationTest; -import com.baeldung.categories.UnitTest; -import com.baeldung.junit.tags.example.Employee; -import com.baeldung.junit.tags.example.EmployeeDAO; -import com.baeldung.junit.tags.example.SpringJdbcConfig; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) -public class EmployeeDAOCategoryIntegrationTest { - - @Autowired - private EmployeeDAO employeeDao; - - @Mock - private JdbcTemplate jdbcTemplate; - private EmployeeDAO employeeDAO; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - employeeDAO = new EmployeeDAO(); - employeeDAO.setJdbcTemplate(jdbcTemplate); - } - - @Test - @Category(IntegrationTest.class) - public void testAddEmployeeUsingSimpelJdbcInsert() { - final Employee emp = new Employee(); - emp.setId(12); - emp.setFirstName("testFirstName"); - emp.setLastName("testLastName"); - emp.setAddress("testAddress"); - - Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); - } - - @Test - @Category(UnitTest.class) - public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { - - // given - Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) - .thenReturn(1); - - // when - int countOfEmployees = employeeDAO.getCountOfEmployees(); - - // then - Assert.assertThat(countOfEmployees, CoreMatchers.is(1)); - } - -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java deleted file mode 100644 index 7c16f5eb29..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOIntegrationTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.baeldung.tags; - -import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -import com.baeldung.junit.tags.example.Employee; -import com.baeldung.junit.tags.example.EmployeeDAO; -import com.baeldung.junit.tags.example.SpringJdbcConfig; - -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class) -public class EmployeeDAOIntegrationTest { - - @Autowired - private EmployeeDAO employeeDao; - - @Mock - private JdbcTemplate jdbcTemplate; - private EmployeeDAO employeeDAO; - - @BeforeEach - public void setup() { - MockitoAnnotations.initMocks(this); - employeeDAO = new EmployeeDAO(); - employeeDAO.setJdbcTemplate(jdbcTemplate); - } - - @Test - @Tag("IntegrationTest") - public void testAddEmployeeUsingSimpelJdbcInsert() { - final Employee emp = new Employee(); - emp.setId(12); - emp.setFirstName("testFirstName"); - emp.setLastName("testLastName"); - emp.setAddress("testAddress"); - - Assertions.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1); - } - - @Test - @Tag("UnitTest") - public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() { - - // given - Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class))) - .thenReturn(1); - - // when - int countOfEmployees = employeeDAO.getCountOfEmployees(); - - // then - Assertions.assertEquals(1, countOfEmployees); - } - -} diff --git a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java b/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java deleted file mode 100644 index 783e5a81a2..0000000000 --- a/testing-modules/junit-5-2/src/test/java/com/baeldung/tags/EmployeeDAOTestSuite.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.tags; - -import org.junit.platform.runner.JUnitPlatform; -import org.junit.platform.suite.api.IncludeTags; -import org.junit.platform.suite.api.SelectPackages; -import org.junit.runner.RunWith; - -@RunWith(JUnitPlatform.class) -@SelectPackages("com.baeldung.tags") -@IncludeTags("UnitTest") -public class EmployeeDAOTestSuite { -} From 478e9f0b53f746f5c74126219682a225508bb593 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 4 May 2019 18:14:59 +0530 Subject: [PATCH 14/14] BAEL2848 JUnit Tagging and Filtering Tests --- testing-modules/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 98c51e7bff..40ed63bc12 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -32,7 +32,6 @@ test-containers testing testng - junit-5-2 junit-5-configuration