Mockito什么时候(不)工作

时间:2017-08-25 08:20:23

标签: java gwt

我正在尝试为GWTP演示者编写测试,但出于某种原因,mockito when()不会触发。我使用stub方法返回一个mock,但它返回null,因为正如我所说的方法何时()不工作(或者我假设)。有什么想法吗?

我的单元测试:

@RunWith(JukitoRunner.class)
public class PresenterATest {

    @Inject
    PresenterA presenterA;

    PresenterA presenterASpy;

    @Mock
    ColumnProperties columnProperties;

    @Before
    public void setUp() throws ActionException {
        MockitoAnnotations.initMocks(this);
        presenterASpy = spy(presenterA);
    }

    @Test
    public void presenterTest() {
        setUpColumnPropertiesMock();

        when(presenterASpy.getSelectedColumn()).thenReturn(columnProperties);

        // adjust colors intervals with slider
        presenterA.saveColumnIntervals(Arrays.asList(45.55, 59.9, 70.05, 100.15),
                Arrays.asList("#e9a8ff", "#d3ffa6", "#ffffa6", "#a8d4ff", "#ffbca6"));      
    }

    private void setUpColumnPropertiesMock() {
        when(columnProperties.getName()).thenReturn("col1");
    }
}

主讲人:

public class PresenterA {

    @Inject
    Settings settings;

    public ColumnProperties getSelectedColumn() {
        return settings.getSelectedColumn();
    }

    public void saveColumnIntervals(List<Double> intervals, List<String> colors) {
        ColumnProperties columnProperties = getSelectedColumn();

        columnProperties.setIntervalList(intervals); //Nullpointer here, because getSelectedColumn() returns null
        columnProperties.setColorList(colors);
    }
}

0 个答案:

没有答案