提问者:小点点

JUnit模拟接口中的默认方法


我有一个名为Class的类,它使用另一个类Maps,它实现了一个带有默认类的接口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

  • 模拟i到。做返回(obj存根)。当(映射)。来自源列表();
  • 模拟i到。当(映射。从源列表(任何列表()))。然后返回(obj存根);

你能帮我吗?

另一个输入:我需要测试使用默认界面的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);
    }
}

共1个答案

匿名用户

模拟示例:

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