I'm trying to write a Test that will compare strings' equality.
Here is a code snippet of the class that should be tested
package ge.jibo.util;
public class TextManager {
private String concatenateTwoString(String t1, String t2) {
return t1 + t2;
}
public String concatenate(String t1, String t2) {
return concatenateTwoString(t1, t2);
}
}
and here is a test class
package ge.jibo.util;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.assertj.core.api.Assertions.assertThat;
public class TextManagerTest {
@Test
@PrepareForTest(TextManager.class)
void checkIfConcatenationWorks() throws Exception {
TextManager textmanager = PowerMockito.spy(new TextManager());
PowerMockito.doReturn("someText").when(textmanager,"concatenateTwoString", Mockito.anyString(), Mockito.anyString());
String text = textmanager.concatenate("ji","bo");
assertThat(text).isEqualTo("jibo");
}
}
as you see, I want to test the public method concatenate, which calls the private method concatenateTwoString in the same class.
The idea is that I want to make a mock object for the private method, and whenever it will be called from the public method it should return the constant value "someText"
but it returns the null instead of "someText" from the private method concatenateTwoString.
org.opentest4j.AssertionFailedError:
Expecting:
<null>
to be equal to:
<"jibo">
but was not.
Expected :jibo
Actual :null
Does anyone know how to fix it?
Dependency Versions:
- junit-jupiter - 5.7.2
- junit-platform-launcher - 1.8.1
- mockito-core - 3.12.4
- mockito-junit-jupiter - 3.12.4
- powermock-core - 2.0.9
- powermock-api-mockito2 - 2.0.9