在运行测试用例时,映射无法在服务类中工作
我正在创建测试用例,在我的服务类方法之一中,我正在使用mapsstruct
将实体映射到DTO类中。
这是我下面的映射类课程
@Mapper(componentModel = "spring")
public interface UserMapper {
List<UserDto> toUserDto(List<UserEntity> users);
}
我在服务类中注入的方式
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService{
private final UserMapper userMapper;
方式
List<UserDto> userDto = userMapper.toUserDto(lst);
是我的使用
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest {
@Mock
private UserRepository userRepo;
@Mock
private UserMapper userMapper;
@InjectMocks
private UserServiceImpl userServiceImpl;
@Test
public void contextLoads() {
then(controller).isNotNull();
then(userServiceImpl).isNotNull();
}
@Test
public void getAllUser() {
List<UserEntity> lst = new ArrayList<UserEntity>();
UserEntity userOne = new UserEntity();
userOne.setEmpFullname("Test Test1");
userOne.setUserId("marina");
userOne.setFlag("Y");
UserEntity usertwo = new UserEntity();
usertwo.setEmpFullname("Test Test2");
usertwo.setUserId("test");
usertwo.setFlag("Y");
lst.add(userOne);
lst.add(usertwo);
when(userRepo.findByStatus("W")).thenReturn(lst);
try {
List<UserDto> pendingUsersList = userServiceImpl.getPendingUser();
assertEquals(2, pendingUsersList.size());
} catch (GeneralException e) {
e.printStackTrace();
}
}
}
, 类,但是当该行执行
list&lt; userdto&gt; userdto = usermapper.touserdto(lst);
它给了我空白的数组。
注意 - 在我的实体类中,我有很多字段,但是从测试类我只传递3个参数。
I am creating test cases, in one of my service class method I am using mapStruct
to map entity into dto class.
This is my mapper class
@Mapper(componentModel = "spring")
public interface UserMapper {
List<UserDto> toUserDto(List<UserEntity> users);
}
below is how I am injecting in my service class
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService{
private final UserMapper userMapper;
This is how I am using it
List<UserDto> userDto = userMapper.toUserDto(lst);
this is how I am doing it in my Test class
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest {
@Mock
private UserRepository userRepo;
@Mock
private UserMapper userMapper;
@InjectMocks
private UserServiceImpl userServiceImpl;
@Test
public void contextLoads() {
then(controller).isNotNull();
then(userServiceImpl).isNotNull();
}
@Test
public void getAllUser() {
List<UserEntity> lst = new ArrayList<UserEntity>();
UserEntity userOne = new UserEntity();
userOne.setEmpFullname("Test Test1");
userOne.setUserId("marina");
userOne.setFlag("Y");
UserEntity usertwo = new UserEntity();
usertwo.setEmpFullname("Test Test2");
usertwo.setUserId("test");
usertwo.setFlag("Y");
lst.add(userOne);
lst.add(usertwo);
when(userRepo.findByStatus("W")).thenReturn(lst);
try {
List<UserDto> pendingUsersList = userServiceImpl.getPendingUser();
assertEquals(2, pendingUsersList.size());
} catch (GeneralException e) {
e.printStackTrace();
}
}
}
when I am running my test cases I am able to see these 2 records in entity class but when this line executes
List<UserDto> userDto = userMapper.toUserDto(lst);
it gives me blank array.
Note - In my entity Class I have many fields but from test class I am passing only 3 parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经通过@mock注释向您的usermapper注释,而无需为此模拟编写Mockito配置。然后期望空白阵列。
删除@mock注释,或指定模拟应返回的内容。
例如 :
You have annotated your UserMapper with a @Mock annotation, without writing the mockito configuration for this mock. Then the blank array is expected.
Remove the @Mock annotation, or specify what should be returned by the mock.
For example :