I am trying to mock for Java class.
My test class,
@RunWith(MockitoJunitRunner.class)
public class ATest {
@Test
public void readContent() throws Exception {
Map<String, String> params = new HashMap<>();
params.put("key", "value");
C c = Mockito.mock(C.class);
when(c.methodCall("myString")).thenReturn(params);
String response = A.readParams("anyString");
}
}
public class A{
private A(){
}
public static String readParams(anyString){
C c = new C();
Map<String, String> map = c.methodCall("myString");
return "returnString";
}
}
Here,
Map<String, String> map = c.methodCall("myString");
this line is not mocking. instead of it calls actual method.
I am using the below jars,
How can I investigate this issue?
The problem is that you are mocking C and inside the readParams method you create a complete new C object and your mocked object is never used. I have modified your method a little bit to address that.
public static String readParams(C c, String anyString){
Map<String, String> map = c.methodCall("myString");
return "returnString";
}
and when you initialize response, you can change to the following code, passing the c that you have mocked.
String response = A.readParams(c, "anyString");
The working code I updating here..
import java.util.Map;
import com.openbank.tr.util.ParamStore;
public class Instrument {
ParamStore parameterStoreMock;
public Instrument(){
parameterStoreMock = new ParamStore();
}
public static String readParams(String s, ParamStore parameterStoreMock){
Map<String, String> parameterMap = parameterStoreMock.getParams("let");
System.out.println(parameterMap);
return "string";
}
}
import java.util.HashMap;
import java.util.Map;
public class ParamStore {
public ParamStore(){
System.out.println("constructor");
}
public Map<String, String> getParams(String param){
Map<String, String> map = new HashMap<String, String>();
map.put("a", "A");
map.put("b", "B");
map.put("c", "C");
return map;
}
}
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.openbank.tr.util.ParamStore;
@RunWith(MockitoJUnitRunner.class)
public class InstrumentTest {
@Mock
ParamStore parameterStore;
@Test
public void readParameterStore() throws Exception {
Map<String, String> parameterMap = new HashMap<>();
parameterMap.put("e", "EE");
parameterMap.put("f", "FF");
parameterStore = Mockito.mock(ParamStore.class);
Mockito.when(parameterStore.getParams("let")).thenReturn(parameterMap);
String response = Instrument.readParams("someString", parameterStore);
verify(parameterStore).getParams("let");
if (response != null)
assertTrue(true);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.