BAEL-1852 - Testing an Abstract Class with JUnit (#4773)

* BAEL-1852 - Testing an Abstract Class with JUnit

* Fixed test method names and class names according to naming compliances.
This commit is contained in:
Kartik Singla 2018-07-21 20:34:52 +05:30 committed by Predrag Maric
parent 93bc08a7c9
commit c5ae8f98cf
11 changed files with 282 additions and 3 deletions

View File

@ -547,6 +547,7 @@
<module>apache-meecrowave</module>
<module>spring-reactive-kotlin</module>
<module>jnosql</module>
<module>testing-modules/junit-abstract</module>
</modules>
</profile>
@ -666,6 +667,7 @@
<module>spring-amqp-simple</module>
<module>spring-apache-camel</module>
<module>spring-batch</module>
<module>testing-modules/junit-abstract</module>
<!-- group 2 - Pass, 11-16 min, 42 test failures, 4,020 KB -->
@ -1071,7 +1073,7 @@
<module>antlr</module>
<module>maven-archetype</module>
<module>apache-meecrowave</module>
<module>testing-modules/junit-abstract</module>
<!-- problematic -->
<!--

View File

@ -0,0 +1,59 @@
<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>
<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>
<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>
<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>
</project>

View File

@ -0,0 +1,17 @@
/**
*
*/
package org.baeldung.testing.abstractclass.abstractmethod;
/**
* When method calls abstract method.
*/
public abstract class AbstractMethodCalling {
public abstract String abstractFunc();
public String defaultImpl() {
String res = abstractFunc();
return (res == null) ? "Default" : (res + " Default");
}
}

View File

@ -0,0 +1,14 @@
package org.baeldung.testing.abstractclass.indepedentmethod;
/**
* Test Independent Method
*
*/
public abstract class AbstractIndependent {
public abstract int abstractFunc();
public String defaultImpl() {
return "DEFAULT-1";
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.testing.abstractclass.indepedentmethod;
public class ConcreteImpl extends AbstractIndependent {
@Override
public int abstractFunc() {
return 4;
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.testing.abstractclass.instancefields;
/**
* Test Independent Method
*/
public abstract class AbstractInstanceFields {
protected int count;
private boolean active = false;
public abstract int abstractFunc();
public String testFunc() {
String response;
if (count > 5) {
response = "Overflow";
} else {
response = active ? "Added" : "Blocked";
}
return response;
}
}

View File

@ -0,0 +1,17 @@
package org.baeldung.testing.abstractclass.privatemethod;
import java.time.LocalDateTime;
public abstract class AbstractPrivateMethods {
public abstract int abstractFunc();
public String defaultImpl() {
return getCurrentDateTime() + "DEFAULT-1";
}
private String getCurrentDateTime() {
return LocalDateTime.now()
.toString();
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.testing.abstractclass.abstractmethod;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mockito.Mockito;
public class AbstractMethodCallingUnitTest {
@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();
// validate result by mock abstractFunc's behaviour
assertEquals("Abstract Default", cls.defaultImpl());
// check the value with null response from abstract method
Mockito.doReturn(null)
.when(cls)
.abstractFunc();
assertEquals("Default", cls.defaultImpl());
}
}

View File

@ -0,0 +1,26 @@
/**
*
*/
package org.baeldung.testing.abstractclass.indepedentmethod;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mockito.Mockito;
public class AbstractIndependentUnitTest {
@Test
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
ConcreteImpl conClass = new ConcreteImpl();
String actual = conClass.defaultImpl();
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());
}
}

View File

@ -0,0 +1,40 @@
package org.baeldung.testing.abstractclass.instancefields;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;
public class AbstractInstanceFieldsUnitTest {
@Test
public void protectedInstanceField_MockClassCountGt5_testNonAbstractMethod() {
// mock
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
Mockito.doCallRealMethod()
.when(instClass)
.testFunc();
// set counter greater than 5
instClass.count = 7;
// compare the result
assertEquals("Overflow", instClass.testFunc());
}
@Test
public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() {
AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class);
PowerMockito.doCallRealMethod()
.when(instClass)
.testFunc();
Whitebox.setInternalState(instClass, "active", true);
// compare the expected result with actual
assertEquals("Added", instClass.testFunc());
}
}

View File

@ -0,0 +1,40 @@
/**
*
*/
package org.baeldung.testing.abstractclass.privatemethod;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* Providing custom values for private methods using powermock
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(AbstractPrivateMethods.class)
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 actual = mockClass.defaultImpl();
assertEquals(dateTime + "DEFAULT-1", actual);
}
}