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,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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -17,19 +18,14 @@
|
|||
|
||||
<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>
|
||||
<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>
|
||||
|
@ -42,18 +38,25 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<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()
|
||||
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)
|
||||
Mockito
|
||||
.doReturn(null)
|
||||
.when(cls)
|
||||
.abstractFunc();
|
||||
assertEquals("Default", cls.defaultImpl());
|
||||
Assertions.assertEquals("Default", cls.defaultImpl());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,9 +3,8 @@
|
|||
*/
|
||||
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 {
|
||||
|
@ -15,12 +14,12 @@ 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,11 +9,12 @@ 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()
|
||||
Mockito
|
||||
.doCallRealMethod()
|
||||
.when(instClass)
|
||||
.testFunc();
|
||||
|
||||
|
@ -22,19 +22,21 @@ public class AbstractInstanceFieldsUnitTest {
|
|||
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()
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,10 @@
|
|||
*/
|
||||
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;
|
||||
|
@ -24,17 +23,19 @@ public class AbstractPrivateMethodsUnitTest {
|
|||
@Test
|
||||
public void givenNonAbstractMethodAndCallPrivateMethod_whenMockPrivateMethod_thenVerifyBehaviour() throws Exception {
|
||||
AbstractPrivateMethods mockClass = PowerMockito.mock(AbstractPrivateMethods.class);
|
||||
PowerMockito.doCallRealMethod()
|
||||
|
||||
String dateTime = LocalDateTime
|
||||
.now()
|
||||
.toString();
|
||||
PowerMockito
|
||||
.doCallRealMethod()
|
||||
.when(mockClass)
|
||||
.defaultImpl();
|
||||
|
||||
String dateTime = LocalDateTime.now()
|
||||
.toString();
|
||||
PowerMockito.doReturn(dateTime)
|
||||
.when(mockClass, "getCurrentDateTime");
|
||||
|
||||
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