Junit Mock是接口中的默认方法



我有一个名为class的类,它使用另一个类Mapping,该类使用默认类实现接口IMap。

public interface IMap<K,V> {
K fromSource(V source);
default List<K> fromSourceList(List<V> sourceList) {
List<K> result= new ArrayList<>();
for (V source: sourceList) {
result.add(this.fromSource(source));
}
return result;
}
}

我试着用Mockito对直接从SourceList调用的类进行单元测试,但我无法存根默认方法。

我尝试使用Mapping.java作为间谍或模拟

  • Mockito.doReturn(objStubbed(.when(mapping(.fromSourceList((
  • Mockito.when(mapping.fromSourceList(anyList((((.thenReturn(objStubbed(

你能帮我吗?


另一个输入:我需要测试使用默认接口的类,如下所示:

public class Mapping implements IMap<String,String> {

@Override
public String fromSource(String source) {
return source.append(".");
}

}
public class Class {

private final Mapping mapping = new Mapping();

public String doSomething(String string) {
List<String> listStr = new ArrayList<>();
listStr.add(string);
listStr.add("This is a test");

List<String> manipulatedString = mapping.fromSourceList(listStr);
return manipulatedString.toString();
}
}
@RunWith(MockitoJUnitRunner.class)
public class ClassTest {
@Mock
IMap<String, String> map;

@InjectMock
ClassTest class;
@Test
public void testMapMock(){
List<String> target = new ArrayList<>();
target.add("test.");
target.add("This is a test.");
when(map.fromSourceList(any())).thenReturn(target);

String result = class.doSomething("test");
Assert.assertEquals("test.This is a test.", result);
}
}

mock示例:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class TestMap {
@Mock
IMap<String, String> map;
@Test
public void testMapMock(){
List<String> source = Stream.of("A").collect(Collectors.toList());
List<String> target = Stream.of("B").collect(Collectors.toList());
when(map.fromSourceList(any())).thenReturn(target);
Assert.assertEquals(map.fromSourceList(source), target);
}
}

您需要通过构造函数将mock注入到类中
以下一种方式对我进行测试:

public class Mapping implements IMap<String,String> {
@Override
public String fromSource(String source) {
return source + ".";
}
}
public class Class {
private IMap<String, String> mapping;
public Class(IMap<String, String> mapping) {
this.mapping = mapping;
}
public String doSomething(String string) {
List<String> listStr = new ArrayList<>();
listStr.add(string);
listStr.add("This is a test");
List<String> manipulatedString = mapping.fromSourceList(listStr);
return manipulatedString.toString();
}
}
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ClassTest {
@Mock
IMap<String, String> map;
@InjectMocks
Class ob;
@Test
public void testMapMock(){
List<String> target = new ArrayList<>();
target.add("test.");
target.add("This is a test.");
when(map.fromSourceList(any())).thenReturn(target);
String result = ob.doSomething("test");
Assert.assertEquals("[test., This is a test.]", result);
}
}

相关内容

  • 没有找到相关文章

最新更新