无法模拟Spring Data Jpa的void方法



我在下面有 void 方法,我想得到嘲笑。

public void updateEmployee(EmployeeDto dto) {
Employee d = convertToEntity(dto);
employeeRepository.updateEmployee(d.getEmployeeName(), d.getEmployeeDescription(),
d.getEmployeeOwnerEmployeeId(), d.getEmployeeCode(), d.getStatus());
}

但是我得到了以下错误。

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
at com.xxx.EmployeeServiceTest.test_UpdateEmployee(EmployeeServiceTest.java:120)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

测试方法

@RunWith(PowerMockRunner.class)
@PrepareForTest({StatusEnum.class})
public class EmployeeServiceTest {
@Mock
private Employee employeeMock;
@InjectMocks
private EmployeeServiceImpl employeeServiceImpl;
@Mock
private EmployeeRepository employeeRepositoryMock;
@Mock
private EmployeeDto employeeDtoMock;
@Mock
private StatusEnum statusEnum;
@Mock
private Exception ex;
List<String> employeeNames = new ArrayList<>();
@Before
public void setup() {
// To mock static methods or class
mockStatic(StatusEnum.class);       
}

@Test
public void test_UpdateEmployee() {
doNothing().when(employeeServiceImpl).saveEmployee(any(EmployeeDto.class));
employeeServiceImpl.updateEmployee(employeeDtoMock);
/*doAnswer((i) -> {
System.out.println("Employee setName Argument = " + i.getArgument(0));
assertTrue("Pankaj".equals(i.getArgument(0)));
return null;
}).when(employeeServiceImpl).updateEmployee(employeeDtoMock);*/
}
}

这里的例外对我来说似乎很清楚,employeeServiceImpl似乎不是模拟。 你是如何实例化的?在球场上使用Mockito.mock@Mock

---编辑

为了澄清我的答案,在@Before(或测试类中的等效项)中,使用 mockito 实例化您的服务:

this.employeeService = Mockito.mock(EmployeeService.class);

@Mock
private EmployeeService employeeService;

然后它应该起作用。

---编辑 所以看到你如何注入你的模拟,我想我可能会明白发生了什么。 事实上,@InjectMocks不会让你EmployeeService a mock.它允许 mockito 知道让你的框架创建 bean 并在其中注入你创建的模拟。

在这里,如果您已将存储库声明为这样的模拟

@Mock
private EmployeeRepository employeeRepository;

然后,将在employeeService实例中注入EmployeeRepository类型的模拟,这不是模拟。

然后,如果它实际上是您要模拟的存储库,则应将其放在测试的when中,例如:

doNothing().when(employeeRepository).saveEmployee(any(EmployeeDto.class));
employeeServiceImpl.saveEmployee(employeeDtoMock);

然后调用您的服务将进入您的服务saveEmployee函数,但是当它到达模拟存储库时,它将按预期工作。

如果它实际上是您要模拟的整个服务,则使用@Mock而不是@InjectMocks来实例化它。

相关内容

  • 没有找到相关文章

最新更新