7
테스트 메소드에 @WithMockUser 주석이 있지만 액세스가 거부되었습니다. 이것이 통합 테스트에서 작동하지 않는 이유는 무엇입니까? @WebAppConfiguration 및 MockMvc를 사용하여 모든 것을 테스트 할 수 있습니다.@WithMockUser가 통합 테스트에서 작동하지 않습니다 - 스프링 부트
테스트 클래스 :
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FileUploadIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private FileStorageService storageService;
@Test
public void classPathResourceTest() throws Exception {
ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
assertThat(resource.exists(), is(true));
}
@Test
@WithMockUser(username="tester",roles={"USER"})
public void shouldUploadFile() throws Exception {
ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("file", resource);
ResponseEntity<String> response = this.restTemplate.postForEntity("/files", map, String.class);
// assertThat(response.getStatusCode(), is(HttpStatus.OK));
then(storageService).should().addFile((any(String.class)), any(MultipartFile.class));
}
}
컨트롤러 클래스 :
@RestController
@RequestMapping("/files")
@PreAuthorize(value = "hasRole('ROLE_USER')")
public class FileUploadController {
private FileStorageService fileStorageService;
private AuthenticationFacade authenticationFacade;
@Autowired
public FileUploadController(FileStorageService fileUploadService, AuthenticationFacade authenticationFacade) {
this.fileStorageService = fileUploadService;
this.authenticationFacade = authenticationFacade;
}
@ResponseBody
@PostMapping
public ResponseEntity<UUID> uploadFile(@RequestParam("file") MultipartFile file) {
UUID uuid = this.fileStorageService.addFile(authenticationFacade.getAuthentication().getName(), file);
if (uuid != null) return ResponseEntity.ok(uuid);
else return (ResponseEntity<UUID>) ResponseEntity.badRequest();
}
}
해결 했습니까? 이 문제를 해결하려면 – BigDong
불행히도/ – Milso
을 사용합니까 oauth2 봄 보안을 사용합니까? – BigDong