mockito UnnecessaryStubbingException:检测到不必要的干扰

mqkwyuun  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(156)

我试着测试这个:

package pp.spring_bootstrap.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pp.spring_bootstrap.dao.RoleDao;
import pp.spring_bootstrap.models.Role;

import java.util.HashSet;
import java.util.Set;

@Service
@Transactional(readOnly = true)
public class RoleServiceImpl implements RoleService {
    private final RoleDao roleDao;

    public RoleServiceImpl(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public Role getRoleByName(String name) {
        return roleDao.findByAuthority(name);
    }

    @Override
    public Set<Role> getAdminRoleSet() {
        return new HashSet<>(roleDao.findByAuthorityOrAuthority("USER", "ADMIN"));
    }
}

这是我的测试:

@ExtendWith(MockitoExtension.class)
class RoleServiceImplTest {
    @InjectMocks
    RoleServiceImpl roleService;
    @Mock
    RoleDao roleDao;
    static final String USER = "USER";
    static final String ADMIN = "ADMIN";

    @BeforeEach
    void setUp() {
        when(roleDao.findByAuthority(USER)).thenReturn(new Role(USER));
        when(roleDao.findByAuthorityOrAuthority(USER, ADMIN))
                .thenReturn(List.of(new Role(USER), new Role(ADMIN)));
    }

    @Test
    void getRoleByName() {
        assertThat(roleService.getRoleByName(USER)).extracting(Role::getAuthority).isEqualTo(USER);
        verify(roleDao, times(1)).findByAuthority(USER);
    }

    @Test
    void getAdminRoleSet() {
        var adminRoles = roleService.getAdminRoleSet().stream()
                .map(Role::getAuthority)
                .toList();
        assertThat(adminRoles.size()).isEqualTo(2);
        assertThat(adminRoles).asList().contains(ADMIN, USER);
        verify(roleDao, times(1)).findByAuthorityOrAuthority(USER, ADMIN);
    }
}

由于此异常,测试未通过:

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at pp.spring_bootstrap.service.RoleServiceImplTest.setUp(RoleServiceImplTest.java:29)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.

第29行是第一个when()调用:

@BeforeEach
    void setUp() {
        when(roleDao.findByAuthority(USER)).thenReturn(new Role(USER)); // this is line 29
        when(roleDao.findByAuthorityOrAuthority(USER, ADMIN))
                .thenReturn(List.of(new Role(USER), new Role(ADMIN)));
    }

我看不出有什么不必要的,因为它是getRoleByName()测试所必需的。正如您在代码片段中看到的,类方法确实调用了它
这种异常行为的原因是什么?我应该做些什么来解决它?

zlwx9yxi

zlwx9yxi1#

测试结果只与两个测试中的一个相关,所以另一个是不必要的,这就是为什么你会得到错误。因此,请将测试移至相关测试或使用@MockitoSettings(strictness = Strictness.LENIENT)

@ExtendWith(MockitoExtension.class)
class RoleServiceImplTest {
    @InjectMocks
    RoleServiceImpl roleService;
    @Mock
    RoleDao roleDao;
    static final String USER = "USER";
    static final String ADMIN = "ADMIN";

    @Test
    void getRoleByName() {
        when(roleDao.findByAuthority(USER)).thenReturn(new Role(USER));
        assertThat(roleService.getRoleByName(USER)).extracting(Role::getAuthority).isEqualTo(USER);
        verify(roleDao, times(1)).findByAuthority(USER);
    }

    @Test
    void getAdminRoleSet() {
        when(roleDao.findByAuthorityOrAuthority(USER, ADMIN))
                .thenReturn(List.of(new Role(USER), new Role(ADMIN)));
        var adminRoles = roleService.getAdminRoleSet().stream()
                .map(Role::getAuthority)
                .toList();
        assertThat(adminRoles.size()).isEqualTo(2);
        assertThat(adminRoles).asList().contains(ADMIN, USER);
        verify(roleDao, times(1)).findByAuthorityOrAuthority(USER, ADMIN);
    }
}

@MockitoSettings(strictness = Strictness.LENIENT)
class RoleServiceImplTest {

相关问题