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:
+38
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -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());
|
||||
}
|
||||
}
|
||||
+42
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user