I have such Application
class:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:applicationContext.xml")
@EnableJpaRepositories("ibd.jpa")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I also have this UserService
class (it is discovered by @EnableJpaRepositories("ibd.jpa")
):
@RestController
@RequestMapping("/user")
public class UserService {
@Autowired
private UserRepository userRepository;
@RequestMapping(method = RequestMethod.POST)
public User createUser(@RequestParam String login, @RequestParam String password){
return userRepository.save(new User(login,password));
}
And I try to test in this UserService
:
@ContextConfiguration
class UserServiceTest extends Specification {
@Autowired
def UserService userService
def "if User not exists 404 status in response sent and corresponding message shown"() {
when: 'rest account url is hit'
MockMvc mockMvc = standaloneSetup(userService).build()
def response = mockMvc.perform(get('/user?login=wrongusername&password=wrongPassword')).andReturn().response
then:
response.status == NOT_FOUND.value()
response.errorMessage == "Login or password is not correct"
}
But the issue is:
UserService in test is null
- it doesn't get successfully @Autowired
; that shows that the Spring Context didn't load. How can I configure for successful Autowiring?
Was solved by:
and usage of RestTemplate
as in this question