gpt4 book ai didi

java - 无法模拟Spring Data Jpa的void方法

转载 作者:行者123 更新时间:2023-11-29 04:08:02 25 4
gpt4 key购买 nike

我有下面的 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;

然后它应该可以工作了。

--- 编辑所以看到你如何注入(inject)你的模拟我想我可能会发生什么。事实上,@InjectMocks 不会使您的 EmployeeService 成为模拟。它允许 mockito 知道让您的框架创建 bean 并注入(inject)您在其中创建的模拟。

如果你已经将你的存储库声明为像这样的模拟

@Mock
private EmployeeRepository employeeRepository;

然后 EmployeeRepository 类型的模拟将被注入(inject)到 employeeService 实例中,这不是模拟。

然后如果它实际上是你想要模拟的存储库,你应该把它放在你的测试中的 when 中,比如:

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

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

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

关于java - 无法模拟Spring Data Jpa的void方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57039565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com