2016-12-01 33 views
7

içinde çalışmıyor. Test yöntemime @WithMockUser ile ek açıklamasına rağmen yine de Erişim Reddedildi alıyorum. Neden bu entegrasyon testinde çalışmıyor? Her şey @WebAppConfiguration ve MockMvc ile test ile iyidir.@WithMockUser, Entegrasyon Testi - İlkbahar önyükleme

Testi Sınıfı:

@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)); 
    } 
} 

Kontrolör sınıfı:

@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(); 
    } 

} 
+0

Bunu çözdünüz mü? Ben de çalışmak için bu sorunları almak için sorunları var – BigDong

+0

Ne yazık ki;/ – Milso

+0

bahar güvenliği oauth2 kullanıyor musunuz? – BigDong

cevap

İlgili konular