Java Mockito无法在SpringBoot中模拟接口类

Java Mockito无法在SpringBoot中模拟接口类,java,spring-boot,junit,mockito,Java,Spring Boot,Junit,Mockito,我正在尝试使用带有service接口和interfaceimpl类的控制器类测试一个测试用例。但是它总是失败并返回null指针异常或找不到自定义异常甚至模拟方法以返回模拟值 控制器类 @RestController 公共类重启控制器{ @自动连线 重启服务重启服务; @后期映射(path=“list/{id}”) 公共响应属性findByID(@PathVariable(value=“id”)字符串id) 抛出一个例外{ List List=restAPIService.findByID(id)

我正在尝试使用带有
service
接口和
interface
impl类的控制器类测试一个测试用例。但是它总是失败并返回
null
指针异常或找不到自定义
异常
甚至模拟方法以返回模拟值

控制器类
@RestController
公共类重启控制器{
@自动连线
重启服务重启服务;
@后期映射(path=“list/{id}”)
公共响应属性findByID(@PathVariable(value=“id”)字符串id)
抛出一个例外{
List List=restAPIService.findByID(id);
if(list==null | | list.isEmpty()){
抛出新的IDNotFoundException(id);
}
返回新的响应属性(列表,HttpStatus.OK);
}
服务接口:
@服务
公共接口重启服务{
公共列表findByID(@Param(“id”)String id)抛出RestAPIException;
}
服务impl类:
@服务
公共类restapiserviceinpl实现RestAPIService{
@凌驾
公共列表findByID(字符串id)引发RestAPIException{
返回serviceUtils.transformationObj(repository.findByID(id));
}
}
测试班
@Mock
RestAPIService restAPIServiceImpl;
@注射模拟
重启控制器重启控制器;
@试验
public void testListByIDController()引发RestAPIException{
Mockito.when(restapiservice.findByID(“1”))。然后返回(baseDTOData());
ResponseEntity expectedList=restapicController.findByID(“1”);
assertEquals(1,expectedList.getBody().size());
}
基于私有列表的OData(){
列表=新的ArrayList();
员工e=新员工();
e、 setId(“2”);
列表.添加(e);
退货清单;
}
xml(使用JUnit5)

org.springframework.boot
弹簧起动试验
测试
朱尼特
朱尼特
org.junit.jupiter
JUnitJupiter api
测试
org.junit.jupiter
朱尼特木星发动机
测试
org.junit.platform
junit平台发射器
测试
org.mockito
莫基托朱庇特酒店
测试
上述代码失败并进入IDNotFoundException,但模拟返回不起作用


任何人都可以在这方面提供帮助。

删除控制器的
@Spy
。删除
@Spy
后,也会得到相同的结果,即不返回模拟值并引发id not found异常。感谢@M.DeinumbaseDTOData()可能会返回null或空列表。请发布一个完整的最小示例来重现此问题。否,baseDTOData()具有带值的列表。即使在测试用例中,最后两行在运行测试时也不会执行。感谢@JB NizetIf,因为它返回
null
。要么您显示的代码不是您实际使用的代码,要么您一定在做非常奇怪的事情。模拟的默认行为是,如果谓词不匹配,则返回空列表。可以吗添加实际的测试用例(而不仅仅是方法)。删除控制器的
@Spy
。删除
@Spy
后,也会得到与不返回模拟值和抛出id not found exception相同的结果。感谢@M.DeinumbaseDTOData()可能会返回null或空列表。请发布一个完整的最小示例来重现此问题。否,baseDTOData()具有带值的列表。即使在测试用例中,最后两行在运行测试时也不会执行。感谢@JB NizetIf,因为它返回
null
。要么您显示的代码不是您实际使用的代码,要么您一定在做非常奇怪的事情。模拟的默认行为是,如果谓词不匹配,则返回空列表。可以吗添加实际的测试用例(而不仅仅是方法)。
@RestController
    public class RestAPIController {

        @Autowired
        RestAPIService restAPIService;

        @PostMapping(path = "list/{id}")
        public ResponseEntity<List<Employee>> findByID(@PathVariable(value = "id") String id)
                throws RestAPIException {


            List<Employee> list = restAPIService.findByID(id);

            if (list == null || list.isEmpty()) {

               throw new IDNotFoundException(id);
            }

            return new ResponseEntity<>(list, HttpStatus.OK);
        }
@Service
public interface RestAPIService {

    public List<Employee> findByID(@Param("id") String id) throws RestAPIException;
}
@Service
public class RestAPIServiceImpl implements RestAPIService {


    @Override
    public List<Employee> findByID(String id) throws RestAPIException {

        return serviceUtils.transformationObj(repository.findByID(id));
    }
}
@Mock
RestAPIService restAPIServiceImpl;


@InjectMocks
RestAPIController restAPIController;

@Test
    public void testListByIDController() throws RestAPIException {

        Mockito.when(restAPIServiceImpl.findByID("1")).thenReturn(baseDTOData());

        ResponseEntity<List<Employee>> expectedList = restAPIController.findByID("1");

        assertEquals(1, expectedList.getBody().size());

    }

private List<Employee> baseDTOData() {
        List<Employee> list = new ArrayList<>();

        Employee e = new Employee();

        e.setId("2");

        list.add(e);

        return list;
    }
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <!-- exclude junit 4 -->

            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Mockito extention -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>