【问题标题】:Mokito Annotation @Mock, @InjectMocks not workingMokito 注释 @Mock,@InjectMocks 不起作用
【发布时间】:2020-11-03 01:35:29
【问题描述】:

您好,我是 Mokito 的新手,目前正在学习在线教程,我有一个非常基本的 mokito 测试设置,像这样没有使用注释:

class SomeBusinessMockTest {
    @Test
    public void testfindGreatestFromAllData() {
        DataService dataServiceMock = mock(DataService.class);
        when(dataServiceMock.retreiveAllData()).thenReturn(new int[] {24,1,2});
        SomeBusinessImpl businessImpl = new SomeBusinessImpl(dataServiceMock);
        int result = businessImpl.findGreatestFromAllData();
        assertEquals(24, result);
    }

}

这可以工作并给出一个绿色条,但是当我尝试使用注释时:

@RunWith(MockitoJUnitRunner.class)
class SomeBusinessMockTest {
    
    @Mock
    DataService dataServiceMock; 
    
    @InjectMocks
    SomeBusinessImpl businessImpl;
    
    @Test
    public void testfindGreatestFromAllData() {
        when(dataServiceMock.retreiveAllData()).thenReturn(new int[] {24,1,2});
        int result = businessImpl.findGreatestFromAllData();
        assertEquals(24, result);
    }

}

这一直在 when(dataServiceMock.retreiveAllData()).thenReturn(new int[] {24,1,2}); 行给我 NullPointer Exception 不知道发生了什么,似乎我唯一改变的是模拟对象的初始化方式。 为了提供完整但可选的上下文,这里是 SomeBusinessImpl.java 和 DataService 接口:

public class SomeBusinessImpl {
    // someBusinessImpl depends on interface DataService to work 
    // so would need to check 
    
    private DataService dataService;

    public SomeBusinessImpl(DataService dataService) {
        super();
        System.out.println("dsadsadsadasdas");
        this.dataService = dataService;
    }

    int findGreatestFromAllData() {
        int[] data = dataService.retreiveAllData();
        int greatest = Integer.MIN_VALUE;
        dataService.retreiveAllData();
        for (int value : data) {
            System.out.println(value);
            if (value > greatest) {
                greatest = value;
            }
        }
        return greatest;
    }

}

interface DataService{
    int[] retreiveAllData();
}

这是否与 Junit 和 Mockito 版本问题有关,还是我遗漏了一些非常明显的东西。 谢谢

【问题讨论】:

  • JUnit4 还是 5?你能在你的测试类中展示你的导入吗?

标签: java junit mockito


【解决方案1】:

我很困惑,因为当我添加@RunWith 标签时,IDE 要求我将 JUnit4 添加到构建路径中,但是从 spring starter.io 中,我选择的 spring 版本是使用 Junit5 的 2.3.5,所以在为了在 Junit5 中执行此操作:

package com.example.mokito.mokitodemo;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class SomeBusinessTest {
    
    @Mock
    DataService dataService;
    
    @InjectMocks
    SomeBusinessImple someBusinessImple;

    @Test
    void testfindTheGreatestFromAllData() {

         when(dataService.retrieveAllData()).thenReturn(new int[] {24,1,2});
         //SomeBusinessImple someBusinessImple = new SomeBusinessImple();
         //someBusinessImple.setDataService(dataService);
         int result = someBusinessImple.findTheGreatestFromAllData();
         assertEquals(24,result);
         
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多