tinmegali
10/28/2019 - 4:17 PM

@PreAuthorize integrational tests using mocks

// reference: https://stackoverflow.com/a/34613097/4871489
@RunWith(SpringRunner.class)
// use the exclusive configuration for the test
@SpringBootTest(classes = {CourseApp.class, InstructorResourceIntTest.InstructorResourceTestConfig.class})
public class InstructorResourceIntTest {
    // create an exclusive configuration for the test, turning on global security for it
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    protected static class InstructorResourceTestConfig {
        @Bean
        @Primary
        public InstructorService instructorService(InstructorRepository repository, InstructorMapper mapper) {
            // mocked classes
            EurekaClients eurekaClients = Mockito.mock(EurekaClients.class);
            RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
            // use the mocks to build the class using @PreAuthorize methods
            return new InstructorServiceImpl( repository, mapper, eurekaClients, restTemplate);
        }
    }
}