Skip to content

Commit c5ae8f9

Browse files
kartiksinglapedja4
authored andcommitted
BAEL-1852 - Testing an Abstract Class with JUnit (eugenp#4773)
* BAEL-1852 - Testing an Abstract Class with JUnit * Fixed test method names and class names according to naming compliances.
1 parent 93bc08a commit c5ae8f9

11 files changed

Lines changed: 282 additions & 3 deletions

File tree

‎pom.xml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,9 @@
545545
<module>antlr</module>
546546
<module>maven-archetype</module>
547547
<module>apache-meecrowave</module>
548-
<module>spring-reactive-kotlin</module>
548+
<module>spring-reactive-kotlin</module>
549549
<module>jnosql</module>
550+
<module>testing-modules/junit-abstract</module>
550551
</modules>
551552

552553
</profile>
@@ -666,6 +667,7 @@
666667
<module>spring-amqp-simple</module>
667668
<module>spring-apache-camel</module>
668669
<module>spring-batch</module>
670+
<module>testing-modules/junit-abstract</module>
669671

670672

671673
<!-- group 2 - Pass, 11-16 min, 42 test failures, 4,020 KB -->
@@ -1071,7 +1073,7 @@
10711073
<module>antlr</module>
10721074
<module>maven-archetype</module>
10731075
<module>apache-meecrowave</module>
1074-
1076+
<module>testing-modules/junit-abstract</module>
10751077

10761078
<!-- problematic -->
10771079
<!--
@@ -1220,4 +1222,4 @@
12201222
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
12211223
</properties>
12221224

1223-
</project>
1225+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>junit-abstract</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<name>abstractclasses</name>
10+
<url>http://maven.apache.org</url>
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>../../</relativePath>
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<powermock.version>1.7.0</powermock.version>
21+
<junit.version>4.12</junit.version>
22+
<mockito.all.version>1.10.19</mockito.all.version>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.mockito</groupId>
29+
<artifactId>mockito-all</artifactId>
30+
<version>${mockito.all.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.powermock</groupId>
35+
<artifactId>powermock-module-junit4</artifactId>
36+
<version>${powermock.version}</version>
37+
<scope>test</scope>
38+
<exclusions>
39+
<exclusion>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.powermock</groupId>
48+
<artifactId>powermock-api-mockito</artifactId>
49+
<version>${powermock.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
</dependencies>
54+
55+
<build>
56+
<finalName>junit-abstract</finalName>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
*
3+
*/
4+
package org.baeldung.testing.abstractclass.abstractmethod;
5+
6+
/**
7+
* When method calls abstract method.
8+
*/
9+
public abstract class AbstractMethodCalling {
10+
11+
public abstract String abstractFunc();
12+
13+
public String defaultImpl() {
14+
String res = abstractFunc();
15+
return (res == null) ? "Default" : (res + " Default");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.testing.abstractclass.indepedentmethod;
2+
3+
/**
4+
* Test Independent Method
5+
*
6+
*/
7+
public abstract class AbstractIndependent {
8+
9+
public abstract int abstractFunc();
10+
11+
public String defaultImpl() {
12+
return "DEFAULT-1";
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.testing.abstractclass.indepedentmethod;
2+
3+
public class ConcreteImpl extends AbstractIndependent {
4+
5+
@Override
6+
public int abstractFunc() {
7+
return 4;
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.testing.abstractclass.instancefields;
2+
3+
/**
4+
* Test Independent Method
5+
*/
6+
public abstract class AbstractInstanceFields {
7+
8+
protected int count;
9+
private boolean active = false;
10+
11+
public abstract int abstractFunc();
12+
13+
public String testFunc() {
14+
String response;
15+
if (count > 5) {
16+
response = "Overflow";
17+
} else {
18+
response = active ? "Added" : "Blocked";
19+
}
20+
return response;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.testing.abstractclass.privatemethod;
2+
3+
import java.time.LocalDateTime;
4+
5+
public abstract class AbstractPrivateMethods {
6+
7+
public abstract int abstractFunc();
8+
9+
public String defaultImpl() {
10+
return getCurrentDateTime() + "DEFAULT-1";
11+
}
12+
13+
private String getCurrentDateTime() {
14+
return LocalDateTime.now()
15+
.toString();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.testing.abstractclass.abstractmethod;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
8+
public class AbstractMethodCallingUnitTest {
9+
10+
@Test
11+
public void givenDefaultImpl_whenMockAbstractFunc_thenExpectedBehaviour() {
12+
13+
// mock classes and call real methods available
14+
AbstractMethodCalling cls = Mockito.mock(AbstractMethodCalling.class);
15+
Mockito.doReturn("Abstract")
16+
.when(cls)
17+
.abstractFunc();
18+
Mockito.doCallRealMethod()
19+
.when(cls)
20+
.defaultImpl();
21+
22+
// validate result by mock abstractFunc's behaviour
23+
assertEquals("Abstract Default", cls.defaultImpl());
24+
25+
// check the value with null response from abstract method
26+
Mockito.doReturn(null)
27+
.when(cls)
28+
.abstractFunc();
29+
assertEquals("Default", cls.defaultImpl());
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*
3+
*/
4+
package org.baeldung.testing.abstractclass.indepedentmethod;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
import org.junit.Test;
9+
import org.mockito.Mockito;
10+
11+
public class AbstractIndependentUnitTest {
12+
13+
@Test
14+
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
15+
ConcreteImpl conClass = new ConcreteImpl();
16+
String actual = conClass.defaultImpl();
17+
18+
assertEquals("DEFAULT-1", actual);
19+
}
20+
21+
@Test
22+
public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() {
23+
AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS);
24+
assertEquals("DEFAULT-1", absCls.defaultImpl());
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.testing.abstractclass.instancefields;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
import org.powermock.api.mockito.PowerMockito;
8+
import org.powermock.reflect.Whitebox;
9+
10+
public class AbstractInstanceFieldsUnitTest {
11+
12+
@Test
13+
public void protectedInstanceField_MockClassCountGt5_testNonAbstractMethod() {
14+
15+
// mock
16+
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
17+
Mockito.doCallRealMethod()
18+
.when(instClass)
19+
.testFunc();
20+
21+
// set counter greater than 5
22+
instClass.count = 7;
23+
24+
// compare the result
25+
assertEquals("Overflow", instClass.testFunc());
26+
}
27+
28+
@Test
29+
public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() {
30+
AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class);
31+
PowerMockito.doCallRealMethod()
32+
.when(instClass)
33+
.testFunc();
34+
Whitebox.setInternalState(instClass, "active", true);
35+
36+
// compare the expected result with actual
37+
assertEquals("Added", instClass.testFunc());
38+
}
39+
40+
}

0 commit comments

Comments
 (0)