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 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 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 - doesn't wired. Means that Context isn't loaded. Please tell me where the problem in ContextConfiguration of test.
Was solved by:
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = Application.class)
@WebAppConfiguration
@IntegrationTest
and usage of RestTemplate