如何在构造函数中注入lambda方法?

时间:2018-06-15 11:31:03

标签: java list dictionary lambda mockito

测试类

将列表流式传输到地图中,我从此列表的元素中获取属性

def common_pods

   pod 'TTTAttributedLabel', '~> 1.7.0'
   pod 'iRate', '~> 1.7.5'
   pod 'MBProgressHUD', '~> 0.6'
   pod 'FlurrySDK', '~> 4.2.3'
   pod 'ACSimpleKeychain', '~> 0.0.1'
   pod 'WEPopover', '~> 0.0.1'
   pod 'AFNetworking', '~> 1.3.1'
   pod 'Nimbus', '~> 1.0.0'
   pod 'QuincyKit', '~> 2.1.9'

end

project = Xcodeproj::Project.open “./<projectNameHere>.xcodeproj"

project.targets.each do |t|

target t.name do

    common_pods

end

测试

public class MyClass {

    private final Map<String, IMyInterface> map;

    public MyClass(final List<IMyInterface> list) {
        this.map = list.stream().collect(Collectors.toMap(IMyInterface::getUniqueName, i -> i));
    }
}

或测试

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @InjectMock
    private MyClass instance;
    @Spy
    private ArrayList<IMyInterface> list;
    @Mock
    private A a;
    @Mock
    private B b;

    @Before void setUp() throws Exception {
        list.add(a);
        list.add(b);
    }
}

如何在执行构造函数后获取注入Map?我必须测试这个类并使用map对象

编辑:

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @Spy
    private ArrayList<IMyInterface> list;
    @Mock
    private A a;
    @Mock
    private B b;

    private class MockedClass extends MyClass {
        MockedClass(List<IMyInterface> list) {
            super(list);
        }
    }

    @Before void setUp() throws Exception {
        list.add(a);
        list.add(b);
    }
}

是界面中的方法 对象A和B实现IMyInterface

我想将注入的列表收集到地图中 当我将元素添加到列表中时,我在测试类中以调试模式获得它,但是当调试模式打开时

IMyInterface::getUniqueName()

停止

1 个答案:

答案 0 :(得分:0)

The correct answer is that should I mock A and B in my @Before

private A a;
private B b;


@Before
public void setUp() throws Exception {
  a = new A(mock.Service.class); // cause it have an argument
  b = new B(mock.Service.class); // cause it have an argument
  list.add(a);
  list.add(b);
}