在 Spock 上调用服务得到空对象
Calling a service on Spock gets null object
我正在尝试在具有以下结构的 spock 框架下执行测试
class UserServiceSpec extends Specification{
ModelMapper modelMapper = Stub(ModelMapper.class)
UserRepository userRepository=Mock(UserRepository.class)
UserService userService
def setup(){
userService= new UserServiceImpl(userRepository,modelMapper)
}
def "find user by email"(){
given: "Creates a constant with an existing email"
def constant ="juan@rodriguez.com"
when: "Calling the method findUserByEmail"
def response= userService.findUserByEmail(constant)
then:"Comparing the response result with the constant"
response.getEmail()==constant
}
}
然而,当我尝试获取 response.getEmail() 的值时,结果是一个空对象,它应该获取与“常量”相同的参数,错误是:
Cannot invoke method getEmail() on null object
UserService 的代码结构和实现是:
public interface UserService {
UserModel findUserByEmail(String email);
}
UserService 实现是:
@Service
@Qualifier("userDetailsService")
public class UserServiceImpl implements UserService, UserDetailsService {
private final UserRepository userRepository;
private final ModelMapper modelMapper;
public UserServiceImpl(UserRepository userRepository,ModelMapper modelMapper){
this.userRepository= userRepository;
this.modelMapper= modelMapper;
}
}
但是,如果我使用相同的方法从控制器(spring 项目)调用服务,我会得到一个带有它的参数的对象。如何修复此 spock 测试?
- 我尝试为服务使用“@Autowired”而不是初始化为 UserServiceImpl,但它不起作用。
- 我尝试不模拟映射器和存储库,但仍然得到相同的空对象。
您没有指定与模拟 UserRepository
的任何交互,因此 Spock returns null
来自您对其进行的任何方法调用。假设您的存储库是普通存储库,请这样说:
then:
userRepository.findByEmail(constant) >> { c -> new UserEntity(email: c, ...) }
constant == response.email // expected value comes first, and you can use Groovy property syntax
我正在尝试在具有以下结构的 spock 框架下执行测试
class UserServiceSpec extends Specification{
ModelMapper modelMapper = Stub(ModelMapper.class)
UserRepository userRepository=Mock(UserRepository.class)
UserService userService
def setup(){
userService= new UserServiceImpl(userRepository,modelMapper)
}
def "find user by email"(){
given: "Creates a constant with an existing email"
def constant ="juan@rodriguez.com"
when: "Calling the method findUserByEmail"
def response= userService.findUserByEmail(constant)
then:"Comparing the response result with the constant"
response.getEmail()==constant
}
}
然而,当我尝试获取 response.getEmail() 的值时,结果是一个空对象,它应该获取与“常量”相同的参数,错误是:
Cannot invoke method getEmail() on null object
UserService 的代码结构和实现是:
public interface UserService {
UserModel findUserByEmail(String email);
}
UserService 实现是:
@Service
@Qualifier("userDetailsService")
public class UserServiceImpl implements UserService, UserDetailsService {
private final UserRepository userRepository;
private final ModelMapper modelMapper;
public UserServiceImpl(UserRepository userRepository,ModelMapper modelMapper){
this.userRepository= userRepository;
this.modelMapper= modelMapper;
}
}
但是,如果我使用相同的方法从控制器(spring 项目)调用服务,我会得到一个带有它的参数的对象。如何修复此 spock 测试?
- 我尝试为服务使用“@Autowired”而不是初始化为 UserServiceImpl,但它不起作用。
- 我尝试不模拟映射器和存储库,但仍然得到相同的空对象。
您没有指定与模拟 UserRepository
的任何交互,因此 Spock returns null
来自您对其进行的任何方法调用。假设您的存储库是普通存储库,请这样说:
then:
userRepository.findByEmail(constant) >> { c -> new UserEntity(email: c, ...) }
constant == response.email // expected value comes first, and you can use Groovy property syntax