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:
parent
c765acd1da
commit
d1caea7afb
|
@ -1,59 +1,62 @@
|
|||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>junit-abstract</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>junit-abstract</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>abstractclasses</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
<name>abstractclasses</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<powermock.version>1.7.0</powermock.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.all.version>1.10.19</mockito.all.version>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<powermock.version>1.7.4</powermock.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>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>${mockito.all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>junit-abstract</finalName>
|
||||
</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>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
|
||||
|
|
|
@ -1,32 +1,38 @@
|
|||
package org.baeldung.testing.abstractclass.abstractmethod;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class AbstractMethodCallingUnitTest {
|
||||
|
||||
private AbstractMethodCalling cls;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
cls = Mockito.mock(AbstractMethodCalling.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultImpl_whenMockAbstractFunc_thenExpectedBehaviour() {
|
||||
|
||||
// mock classes and call real methods available
|
||||
AbstractMethodCalling cls = Mockito.mock(AbstractMethodCalling.class);
|
||||
Mockito.doReturn("Abstract")
|
||||
.when(cls)
|
||||
.abstractFunc();
|
||||
Mockito.doCallRealMethod()
|
||||
.when(cls)
|
||||
.defaultImpl();
|
||||
Mockito
|
||||
.when(cls.abstractFunc())
|
||||
.thenReturn("Abstract");
|
||||
Mockito
|
||||
.doCallRealMethod()
|
||||
.when(cls)
|
||||
.defaultImpl();
|
||||
|
||||
// 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
|
||||
Mockito.doReturn(null)
|
||||
.when(cls)
|
||||
.abstractFunc();
|
||||
assertEquals("Default", cls.defaultImpl());
|
||||
Mockito
|
||||
.doReturn(null)
|
||||
.when(cls)
|
||||
.abstractFunc();
|
||||
Assertions.assertEquals("Default", cls.defaultImpl());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,24 +3,23 @@
|
|||
*/
|
||||
package org.baeldung.testing.abstractclass.indepedentmethod;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class AbstractIndependentUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
|
||||
ConcreteImpl conClass = new ConcreteImpl();
|
||||
String actual = conClass.defaultImpl();
|
||||
@Test
|
||||
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
|
||||
ConcreteImpl conClass = new ConcreteImpl();
|
||||
String actual = conClass.defaultImpl();
|
||||
|
||||
assertEquals("DEFAULT-1", actual);
|
||||
}
|
||||
Assertions.assertEquals("DEFAULT-1", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() {
|
||||
AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS);
|
||||
assertEquals("DEFAULT-1", absCls.defaultImpl());
|
||||
Assertions.assertEquals("DEFAULT-1", absCls.defaultImpl());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package org.baeldung.testing.abstractclass.instancefields;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
@ -10,31 +9,34 @@ import org.powermock.reflect.Whitebox;
|
|||
public class AbstractInstanceFieldsUnitTest {
|
||||
|
||||
@Test
|
||||
public void protectedInstanceField_MockClassCountGt5_testNonAbstractMethod() {
|
||||
public void givenProtectedInstanceField_whenMockClassCountGt5_thenTestNonAbstractMethod() {
|
||||
|
||||
// mock
|
||||
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
|
||||
Mockito.doCallRealMethod()
|
||||
.when(instClass)
|
||||
.testFunc();
|
||||
Mockito
|
||||
.doCallRealMethod()
|
||||
.when(instClass)
|
||||
.testFunc();
|
||||
|
||||
// set counter greater than 5
|
||||
instClass.count = 7;
|
||||
|
||||
// compare the result
|
||||
assertEquals("Overflow", instClass.testFunc());
|
||||
Assertions.assertEquals("Overflow", instClass.testFunc());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() {
|
||||
|
||||
AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class);
|
||||
PowerMockito.doCallRealMethod()
|
||||
.when(instClass)
|
||||
.testFunc();
|
||||
PowerMockito
|
||||
.doCallRealMethod()
|
||||
.when(instClass)
|
||||
.testFunc();
|
||||
Whitebox.setInternalState(instClass, "active", true);
|
||||
|
||||
// compare the expected result with actual
|
||||
assertEquals("Added", instClass.testFunc());
|
||||
Assertions.assertEquals("Added", instClass.testFunc());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.baeldung.testing.abstractclass.privatemethod;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
|
@ -15,7 +14,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||
|
||||
/**
|
||||
* Providing custom values for private methods using powermock
|
||||
*
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(AbstractPrivateMethods.class)
|
||||
|
@ -24,17 +23,19 @@ public class AbstractPrivateMethodsUnitTest {
|
|||
@Test
|
||||
public void givenNonAbstractMethodAndCallPrivateMethod_whenMockPrivateMethod_thenVerifyBehaviour() throws Exception {
|
||||
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();
|
||||
assertEquals(dateTime + "DEFAULT-1", actual);
|
||||
Assertions.assertEquals(dateTime + "DEFAULT-1", actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue