如何在春季服务中模拟映射映射器输出?
我有使用映射映射器(Custommapstruct -Mapper)的春季服务(MyService):
@Service
public class MyService {
private final ClassA classA;
private final ClassB classB;
/*
other private fields declarations...
*/
private CustomMapstructMapper customMapstructMapper = Mappers.getMapper(CustomMapstructMapper.class);
//MyService constructor
public MyService(final ClassA classA, etc...) {
this.classA = classA;
//etc...
}
public ServiceOutput mainMethod(some parameters) {
//some business logic
MyMapperOutput myMapperOutput = customMapstructMapper.map(MapperParameter parameter);
ServiceOutput serviceOutput = some business logic with myMapperOutput;
return serviceOutput;
}
}
我想单元测试MyService(我使用Junit 5),并模拟我的Custmapsstrumpstructmapper 输出,而无需在测试期间调用真实映射器执行。我已经有另一个测试类,该类特定于
Custompaps -Mapper
中测试所有自定义映射。
因此,我有一个测试课:
@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@InjectMocks
private MyService myService;
@Mock
private CustomMapstructMapper customMapstructMapper;
@Mock
private MyMapperOutput myMapperOutput;
@Test
void testMyService() {
/*
Some mocks creation ..
*/
Mockito.when(myMapperOutput.getSomeField()).thenReturn("Some value");
Mockito.when(customMapstructMapper.map(Mockito.any(MapperParameter.class))).thenReturn(myMapperOutput);
ServiceOutput serviceOutput = myService.mainMethod(some parameters);
/*
Some assertions on serviceOutput
*/
}
}
当我运行测试时,我的Mapper Custommaps -Mapperimpl的实现在MyService中被调用,而不是我的模拟。 由于某些字段没有启动,因此在我的映射器中抛出了NullPoInterException。 我试图通过实现我的映射映射器创建一个模拟:
@Mock private CustomMapstructMapperImpl customMapstructMapper;
但是我得到了相同的结果。
我在做什么错?
I have a spring service (MyService) that uses a mapstruct mapper (CustomMapstructMapper) :
@Service
public class MyService {
private final ClassA classA;
private final ClassB classB;
/*
other private fields declarations...
*/
private CustomMapstructMapper customMapstructMapper = Mappers.getMapper(CustomMapstructMapper.class);
//MyService constructor
public MyService(final ClassA classA, etc...) {
this.classA = classA;
//etc...
}
public ServiceOutput mainMethod(some parameters) {
//some business logic
MyMapperOutput myMapperOutput = customMapstructMapper.map(MapperParameter parameter);
ServiceOutput serviceOutput = some business logic with myMapperOutput;
return serviceOutput;
}
}
I want to unit test MyService (I am using Junit 5) and mock my CustomMapstructMapper
output without calling the real mapper during the test execution. I already have another test class that specificly test all the custom mappings in CustomMapstructMapper
.
So I have this test class :
@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@InjectMocks
private MyService myService;
@Mock
private CustomMapstructMapper customMapstructMapper;
@Mock
private MyMapperOutput myMapperOutput;
@Test
void testMyService() {
/*
Some mocks creation ..
*/
Mockito.when(myMapperOutput.getSomeField()).thenReturn("Some value");
Mockito.when(customMapstructMapper.map(Mockito.any(MapperParameter.class))).thenReturn(myMapperOutput);
ServiceOutput serviceOutput = myService.mainMethod(some parameters);
/*
Some assertions on serviceOutput
*/
}
}
When I run my test, the implementation of my mapper customMapstructMapperImpl is called in MyService, not my mock.
A NullPointerException is thrown in my mapper because some fields are not initiated.
I have tried to create a mock with the implementation of my mapstruct mapper :
@Mock private CustomMapstructMapperImpl customMapstructMapper;
but I get the same result.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有利用春季框架和对其进行映射支持。
在您的服务更改中:
则会
如果您尚未使用映射器,
导致生成的映射器具有
@component
从弹簧框架上注释,并且可以自动使用。这。进行这些更改后,您提供测试模拟的方法应该有效。
You're not taking advantage of the spring framework and mapstruct support for it.
in your service change:
into
If you don't have it yet at your mapper use
This will cause the generated mapper to have the
@Component
annotation from the spring framework, and it becomes possible to auto-wire this.After making these changes your method of supplying a mock for testing should work.
下面的代码正常工作。所以也许还有其他事情发生了。您的代码示例还不够完整。
结果:
The code below works correctly. So maybe something else happened. Your code example is not quite complete.
result:
在这里简单的方法:
只需在
MyService
中添加package-Provate
setter方法,然后在测试代码中注入模拟的
custommapsstructmapper
A simpler way here:
Just add a
package-private
setter method into yourMyService
and inject your mocked
CustomMapstructMapper
in test code