【发布时间】:2021-08-02 14:00:46
【问题描述】:
您好,我的 Spring Boot 项目有这个简单的代码:
@Component
public class UserRowMapper implements RowMapper<User> {
@Value("${bug.value}")
private String id;
@Value("${wrong.value}")
private String userName;
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
return User.builder()
.id(rs.getInt(id))
.userName(rs.getString(userName)).build();
}
}
我想要创建一个简单的 Mockito 测试来检查 @Value 字符串,如下所示:
@ExtendWith(MockitoExtension.class)
class UserRowMapperTest {
@Mock
Environment environment;
@Mock
ResultSet resultSet;
@InjectMocks
UserRowMapper userRowMapper;
@Test
void testMapRow() {
when(environment.getProperty("user.id")).thenReturn("id");
when(environment.getProperty("user.userName")).thenReturn("userName");
try {
final User user = userRowMapper.mapRow(resultSet, anyInt());
// check if its ok
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
但我找不到简单的方法来检查我注入的值是否符合我的预期。
有什么想法吗?
【问题讨论】:
标签: java spring spring-boot unit-testing mockito