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,59 +1,62 @@
<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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> 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>
<artifactId>junit-abstract</artifactId> <artifactId>junit-abstract</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>abstractclasses</name> <name>abstractclasses</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath> <relativePath>../../</relativePath>
</parent> </parent>
<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> <java.version>1.8</java.version>
<mockito.all.version>1.10.19</mockito.all.version> <junit.jupiter.version>5.1.0</junit.jupiter.version>
<java.version>1.8</java.version> <junit.platform.version>1.1.0</junit.platform.version>
</properties> <junit.vintage.version>5.2.0</junit.vintage.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.powermock</groupId>
<artifactId>mockito-all</artifactId> <artifactId>powermock-module-junit4</artifactId>
<version>${mockito.all.version}</version> <version>${powermock.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> <exclusions>
<dependency> <exclusion>
<groupId>org.powermock</groupId> <groupId>junit</groupId>
<artifactId>powermock-module-junit4</artifactId> <artifactId>junit</artifactId>
<version>${powermock.version}</version> </exclusion>
<scope>test</scope> </exclusions>
<exclusions> </dependency>
<exclusion> <dependency>
<groupId>junit</groupId> <groupId>org.powermock</groupId>
<artifactId>junit</artifactId> <artifactId>powermock-api-mockito2</artifactId>
</exclusion> <version>${powermock.version}</version>
</exclusions> <scope>test</scope>
</dependency> </dependency>
</dependencies>
<dependency> <build>
<groupId>org.powermock</groupId> <plugins>
<artifactId>powermock-api-mockito</artifactId> <plugin>
<version>${powermock.version}</version> <groupId>org.apache.maven.plugins</groupId>
<scope>test</scope> <artifactId>maven-surefire-plugin</artifactId>
</dependency> <version>2.22.0</version>
</plugin>
</dependencies> </plugins>
<finalName>junit-abstract</finalName>
<build> </build>
<finalName>junit-abstract</finalName>
</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(); .when(cls)
Mockito.doCallRealMethod() .defaultImpl();
.when(cls)
.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
.when(cls) .doReturn(null)
.abstractFunc(); .when(cls)
assertEquals("Default", cls.defaultImpl()); .abstractFunc();
Assertions.assertEquals("Default", cls.defaultImpl());
} }
} }

View File

@ -3,24 +3,23 @@
*/ */
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 {
@Test @Test
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() { 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,31 +9,34 @@ 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
.when(instClass) .doCallRealMethod()
.testFunc(); .when(instClass)
.testFunc();
// set counter greater than 5 // set counter greater than 5
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
.when(instClass) .doCallRealMethod()
.testFunc(); .when(instClass)
.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()
.when(mockClass)
.defaultImpl();
String dateTime = LocalDateTime.now()
.toString();
PowerMockito.doReturn(dateTime)
.when(mockClass, "getCurrentDateTime");
String dateTime = LocalDateTime
.now()
.toString();
PowerMockito
.doCallRealMethod()
.when(mockClass)
.defaultImpl();
PowerMockito
.doReturn(dateTime)
.when(mockClass, "getCurrentDateTime");// .thenReturn(dateTime);
String actual = mockClass.defaultImpl(); String actual = mockClass.defaultImpl();
assertEquals(dateTime + "DEFAULT-1", actual); Assertions.assertEquals(dateTime + "DEFAULT-1", actual);
} }
} }