Bael 1852 - Test case code is aligned to support Junit5 (#4847)

* add prototype bean ex with function

* remove extra classes

* remove extra import

* separate configs

* separate configs

* Update AppConfig.java

* Code update to support Junit5

* BAEL-1979 Added examples for SnakeYAML Library (#4802)

* BAEL-1979 Added examples for SnakeYAML Library

* BAEL-1979 Moved the snakeyaml related code to libraries module

* BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible.

* BAEL-1979 Removed println statements, small formatting fix in pom.xml

* BAEL-1852 Renamed one test method, fixed formatting
This commit is contained in:
Kartik Singla 2018-08-01 15:40:43 +05:30 committed by Predrag Maric
parent c765acd1da
commit d1caea7afb
5 changed files with 115 additions and 104 deletions

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -17,19 +18,14 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powermock.version>1.7.0</powermock.version> <powermock.version>1.7.4</powermock.version>
<junit.version>4.12</junit.version>
<mockito.all.version>1.10.19</mockito.all.version>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.platform.version>1.1.0</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.all.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId> <artifactId>powermock-module-junit4</artifactId>
@ -42,18 +38,25 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version> <version>${powermock.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
<finalName>junit-abstract</finalName> <finalName>junit-abstract</finalName>
</build> </build>
</project> </project>

View File

@ -1,32 +1,38 @@
package org.baeldung.testing.abstractclass.abstractmethod; package org.baeldung.testing.abstractclass.abstractmethod;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
public class AbstractMethodCallingUnitTest { public class AbstractMethodCallingUnitTest {
private AbstractMethodCalling cls;
@BeforeEach
public void setup() {
cls = Mockito.mock(AbstractMethodCalling.class);
}
@Test @Test
public void givenDefaultImpl_whenMockAbstractFunc_thenExpectedBehaviour() { public void givenDefaultImpl_whenMockAbstractFunc_thenExpectedBehaviour() {
Mockito
// mock classes and call real methods available .when(cls.abstractFunc())
AbstractMethodCalling cls = Mockito.mock(AbstractMethodCalling.class); .thenReturn("Abstract");
Mockito.doReturn("Abstract") Mockito
.when(cls) .doCallRealMethod()
.abstractFunc();
Mockito.doCallRealMethod()
.when(cls) .when(cls)
.defaultImpl(); .defaultImpl();
// validate result by mock abstractFunc's behaviour // validate result by mock abstractFunc's behaviour
assertEquals("Abstract Default", cls.defaultImpl()); Assertions.assertEquals("Abstract Default", cls.defaultImpl());
// check the value with null response from abstract method // check the value with null response from abstract method
Mockito.doReturn(null) Mockito
.doReturn(null)
.when(cls) .when(cls)
.abstractFunc(); .abstractFunc();
assertEquals("Default", cls.defaultImpl()); Assertions.assertEquals("Default", cls.defaultImpl());
} }
} }

View File

@ -3,9 +3,8 @@
*/ */
package org.baeldung.testing.abstractclass.indepedentmethod; package org.baeldung.testing.abstractclass.indepedentmethod;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
public class AbstractIndependentUnitTest { public class AbstractIndependentUnitTest {
@ -15,12 +14,12 @@ public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
ConcreteImpl conClass = new ConcreteImpl(); ConcreteImpl conClass = new ConcreteImpl();
String actual = conClass.defaultImpl(); String actual = conClass.defaultImpl();
assertEquals("DEFAULT-1", actual); Assertions.assertEquals("DEFAULT-1", actual);
} }
@Test @Test
public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() { public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() {
AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS); AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS);
assertEquals("DEFAULT-1", absCls.defaultImpl()); Assertions.assertEquals("DEFAULT-1", absCls.defaultImpl());
} }
} }

View File

@ -1,8 +1,7 @@
package org.baeldung.testing.abstractclass.instancefields; package org.baeldung.testing.abstractclass.instancefields;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
@ -10,11 +9,12 @@ import org.powermock.reflect.Whitebox;
public class AbstractInstanceFieldsUnitTest { public class AbstractInstanceFieldsUnitTest {
@Test @Test
public void protectedInstanceField_MockClassCountGt5_testNonAbstractMethod() { public void givenProtectedInstanceField_whenMockClassCountGt5_thenTestNonAbstractMethod() {
// mock // mock
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class); AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
Mockito.doCallRealMethod() Mockito
.doCallRealMethod()
.when(instClass) .when(instClass)
.testFunc(); .testFunc();
@ -22,19 +22,21 @@ public class AbstractInstanceFieldsUnitTest {
instClass.count = 7; instClass.count = 7;
// compare the result // compare the result
assertEquals("Overflow", instClass.testFunc()); Assertions.assertEquals("Overflow", instClass.testFunc());
} }
@Test @Test
public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() { public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() {
AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class); AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class);
PowerMockito.doCallRealMethod() PowerMockito
.doCallRealMethod()
.when(instClass) .when(instClass)
.testFunc(); .testFunc();
Whitebox.setInternalState(instClass, "active", true); Whitebox.setInternalState(instClass, "active", true);
// compare the expected result with actual // compare the expected result with actual
assertEquals("Added", instClass.testFunc()); Assertions.assertEquals("Added", instClass.testFunc());
} }
} }

View File

@ -3,11 +3,10 @@
*/ */
package org.baeldung.testing.abstractclass.privatemethod; package org.baeldung.testing.abstractclass.privatemethod;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
@ -24,17 +23,19 @@ public class AbstractPrivateMethodsUnitTest {
@Test @Test
public void givenNonAbstractMethodAndCallPrivateMethod_whenMockPrivateMethod_thenVerifyBehaviour() throws Exception { public void givenNonAbstractMethodAndCallPrivateMethod_whenMockPrivateMethod_thenVerifyBehaviour() throws Exception {
AbstractPrivateMethods mockClass = PowerMockito.mock(AbstractPrivateMethods.class); AbstractPrivateMethods mockClass = PowerMockito.mock(AbstractPrivateMethods.class);
PowerMockito.doCallRealMethod()
String dateTime = LocalDateTime
.now()
.toString();
PowerMockito
.doCallRealMethod()
.when(mockClass) .when(mockClass)
.defaultImpl(); .defaultImpl();
PowerMockito
String dateTime = LocalDateTime.now() .doReturn(dateTime)
.toString(); .when(mockClass, "getCurrentDateTime");// .thenReturn(dateTime);
PowerMockito.doReturn(dateTime)
.when(mockClass, "getCurrentDateTime");
String actual = mockClass.defaultImpl(); String actual = mockClass.defaultImpl();
assertEquals(dateTime + "DEFAULT-1", actual); Assertions.assertEquals(dateTime + "DEFAULT-1", actual);
} }
} }