Junit Mock接口中的默认方法

Junit Mock a default method in an interface

我有一个名为 Class 的 class,它使用另一个 class 映射,它实现了一个默认为 class 的接口 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 进行单元测试,我的 Class 直接从 SourceList 调用但是,我无法存根默认方法。

我尝试使用 Mapping.java 作为 Spy 或 Mock

你能帮帮我吗?


另一个输入: 我需要像这样测试使用默认接口的 Class:

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);
    }
}

模拟示例:

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 注入 class。
以下一种方式测试对我的工作:

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);
    }
}