在运行测试用例时,映射无法在服务类中工作

发布于 2025-02-13 11:09:52 字数 1946 浏览 1 评论 0原文

我正在创建测试用例,在我的服务类方法之一中,我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2025-02-20 11:09:52

您已经通过@mock注释向您的usermapper注释,而无需为此模拟编写Mockito配置。然后期望空白阵列。
删除@mock注释,或指定模拟应返回的内容。
例如 :

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = Application.class)

public class ApplicationTest {
    
    @Mock
    private UserRepository userRepo;
    
    @Spy
    private UserMapper userMapper = Mappers.getMapper(UserMapper.class);
    
    
    @InjectMocks
    private UserServiceImpl userServiceImpl;

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 :

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = Application.class)

public class ApplicationTest {
    
    @Mock
    private UserRepository userRepo;
    
    @Spy
    private UserMapper userMapper = Mappers.getMapper(UserMapper.class);
    
    
    @InjectMocks
    private UserServiceImpl userServiceImpl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文