Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableWebSecurity
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
- public final static String ROLE_ADMIN = "ADMIN";
- public final static String ROLE_USER = "USER";
- /**
- * Determines the resource access for different account types
- */
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .antMatchers("/user/create").permitAll()
- .antMatchers("/admin/**").hasRole(ROLE_ADMIN)
- .anyRequest().authenticated()
- .and()
- .csrf().disable()
- .httpBasic();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(inMemoryUserDetailsManager());
- }
- /**
- * Initially fills Spring Security with default accounts
- */
- @Bean
- public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
- final Properties users = new Properties();
- users.put("user","pass,ROLE_USER,enabled"); //login = user, password = pass
- users.put("admin","pass,ROLE_ADMIN,enabled"); //login = admin, password = pass
- return new InMemoryUserDetailsManager(users);
- }
- }
- @RestController
- public class MovieController {
- @Autowired @Qualifier("MovieService")//not relevant
- private MovieService ms;
- @Autowired @Qualifier("CastService")//not relevant
- private CastService cs;
- @RequestMapping(value = "admin/movies", method = GET)
- public List<Movie> selectAllMovies(){
- return ms.selectAll();
- }
- //the rest of the code..
- }
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @Transactional
- public class MovieControllerTest {
- @Mock
- private MovieService movieService;
- @Mock
- private CastService castService;
- @Mock
- private ActorService actorService;
- @Mock
- private UserService userService;
- @InjectMocks
- private MovieController movieController;
- private MockMvc mvc;
- @Before
- public void setUp(){
- MockitoAnnotations.initMocks(this);
- mvc = MockMvcBuilders.standaloneSetup(movieController).build();
- }
- @Test
- @WithMockUser(roles = "ADMIN", username = "admin", password = "pass")
- @WithUserDetails("admin")
- public void testGetAllMovies() throws Exception {
- List<Movie> movieList = Arrays.asList(
- new Movie("title1", "desc1", MovieType.newest, 10f, true),
- new Movie("title2", "desc2", MovieType.newest, 10f, true),
- new Movie("title3", "desc3", MovieType.newest, 10f, true));
- when(movieService.selectAll()).thenReturn(movieList);
- String uri = "admin/movies";
- MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri)
- .accept(MediaType.APPLICATION_JSON)).andReturn();
- String content = result.getResponse().getContentAsString();
- int status = result.getResponse().getStatus();
- verify(movieService, times(1)).selectAll();
- Assert.assertEquals("failure - expected HTTP status 200", 200, status);
- Assert.assertTrue("failure - expected HTTP response body to have a value", content.trim().length() > 0);
- }
- //the rest of the code..
- }
- Wanted but not invoked:
- movieService.selectAll();
- -> at com.myproject.Controller.MovieControllerTest.testGetAllMovies(MovieControllerTest.java:87)
- Actually, there were zero interactions with this mock.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement