BAEL-1852 moved the code into junit-5 module (#4915)

* BAEL-1852 moved the code into junit-5 module

* BAEL-1852 removed junit-abstract module from parent-modules
This commit is contained in:
Predrag Maric
2018-08-07 14:42:57 +02:00
committed by GitHub
parent a17637b88a
commit ddc106ccd9
13 changed files with 31 additions and 90 deletions
@@ -0,0 +1,17 @@
/**
*
*/
package com.baeldung.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");
}
}
@@ -0,0 +1,14 @@
package com.baeldung.abstractclass.indepedentmethod;
/**
* Test Independent Method
*
*/
public abstract class AbstractIndependent {
public abstract int abstractFunc();
public String defaultImpl() {
return "DEFAULT-1";
}
}
@@ -0,0 +1,10 @@
package com.baeldung.abstractclass.indepedentmethod;
public class ConcreteImpl extends AbstractIndependent {
@Override
public int abstractFunc() {
return 4;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.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;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.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();
}
}
@@ -0,0 +1,38 @@
package com.baeldung.abstractclass.abstractmethod;
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() {
Mockito
.when(cls.abstractFunc())
.thenReturn("Abstract");
Mockito
.doCallRealMethod()
.when(cls)
.defaultImpl();
// validate result by mock abstractFunc's behaviour
Assertions.assertEquals("Abstract Default", cls.defaultImpl());
// check the value with null response from abstract method
Mockito
.doReturn(null)
.when(cls)
.abstractFunc();
Assertions.assertEquals("Default", cls.defaultImpl());
}
}
@@ -0,0 +1,25 @@
/**
*
*/
package com.baeldung.abstractclass.indepedentmethod;
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();
Assertions.assertEquals("DEFAULT-1", actual);
}
@Test
public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() {
AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS);
Assertions.assertEquals("DEFAULT-1", absCls.defaultImpl());
}
}
@@ -0,0 +1,42 @@
package com.baeldung.abstractclass.instancefields;
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;
public class AbstractInstanceFieldsUnitTest {
@Test
public void givenProtectedInstanceField_whenMockClassCountGt5_thenTestNonAbstractMethod() {
// mock
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
Mockito
.doCallRealMethod()
.when(instClass)
.testFunc();
// set counter greater than 5
instClass.count = 7;
// compare the result
Assertions.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
Assertions.assertEquals("Added", instClass.testFunc());
}
}
@@ -0,0 +1,41 @@
/**
*
*/
package com.baeldung.abstractclass.privatemethod;
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;
import org.powermock.modules.junit4.PowerMockRunner;
import java.time.LocalDateTime;
/**
* 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);
String dateTime = LocalDateTime
.now()
.toString();
PowerMockito
.doCallRealMethod()
.when(mockClass)
.defaultImpl();
PowerMockito
.doReturn(dateTime)
.when(mockClass, "getCurrentDateTime");// .thenReturn(dateTime);
String actual = mockClass.defaultImpl();
Assertions.assertEquals(dateTime + "DEFAULT-1", actual);
}
}