Ich habe einen ROD-Endpunkt für das Hochladen von Dateien in Dropwizard implementiert. Ich bin neu und versuche nur zu lernen.Dropwizard-Datei-Upload-Test fehlgeschlagen, da ResourceConfig keinen Argument-Konstruktor erwartet
@Path("/files")
@Produces(MediaType.APPLICATION_JSON)
public class FileUploadResource {
private final MyAppWebConfiguration configuration;
private static final Logger logger = LoggerFactory.getLogger(FileUploadResource.class);
public FileUploadResource(MyAppWebConfiguration configuration) {
this.configuration = configuration;
}
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {
logger.info("Request to upload the file ", fileDetail.getFileName());
final String uploadedFileLocation = configuration.getCsvUploadPath();
final String fileName = fileDetail.getFileName();
writeToFile(uploadedInputStream, uploadedFileLocation, fileName);
return Response.ok("File " + fileName + " is uploaded to the location " + uploadedFileLocation).build();
}
// save uploaded file to new location
protected void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation, String fileName) throws IOException {
logger.info("Writing {} to {}", fileName, uploadedFileLocation);
final java.nio.file.Path outputPath = FileSystems.getDefault().getPath(uploadedFileLocation, fileName);
Files.copy(uploadedInputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
logger.info("Uploaded {} to the location {}", fileName, uploadedFileLocation);
}
Der Code funktioniert gut und kann die Datei hochladen. Ich versuche es mit folgendem Code auf https://gist.github.com/psamsotha/218c6bbeb6164bac7cbc Basis zu testen:
public class FileUploadResourceTest extends JerseyTest {
private final static MyAppWebConfiguration mockConfiguration = mock(MyAppWebConfiguration.class);
@Override
public ResourceConfig configure() {
return new ResourceConfig(FileUploadResource.class)
.register(MultiPartFeature.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
@Override
public void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class);
}
@Test
public void test() {
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("/Users/rocky/Downloads/test.csv"));
filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("/Users/rocky/Downloads/test.csv").build());
MultiPart multiPart = new FormDataMultiPart()
.bodyPart(filePart);
Response response = target("/files").request()
.post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
response.close();
}
Dieser Test mit dem folgenden Fehler fehl:
WARNING: The following warnings have been detected: WARNING: HK2 service reification failed for [com.my.app.resources.FileUploadResource] with an exception:
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in com.my.app.resources.FileUploadResource class.
at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192)
at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:178)
at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:128)
at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:179)
Ich habe kein kein Argument Konstruktor in FileUploadResource
habe aber new ResourceConfig(FileUploadResource.class)
erwartet eine kein Argumentkonstruktor. Wie gebe ich hier die Info über den Konstruktor des einen Arguments weiter?
Jede Hilfe hier würde sehr geschätzt werden. Gerne können Sie auch andere Best Practices für den Code und die Tests vorschlagen, damit ich sie verbessern kann.
Vielen Dank im Voraus.
Versucht den @peeskillet-Vorschlag. Habe dieses jetzt: 'FileDataBodyPart filePart = new FileDataBodyPart (" Datei ", neue Datei ("/Users/rocky/Downloads/test.csv ")); filePart.setContentDisposition (FormDataContentDisposition.name ("Datei"). FileName ("/ Benutzer/rocky/Downloads/test.csv"). Build()); MultiPart multiPart = neu FormDataMultiPart() .bodyPart (filePart); '. Aber bekommen 'FEHLER [2016-04-08 10: 41: 29,089] io.dropwizard.jersey.errors.LoggingExceptionMapper: Fehler bei der Bearbeitung einer Anfrage: bdb68f36df42328e ! java.nio.file.NoSuchFileException: null/abc/Benutzer/rocky/Downloads/test.csv'. – Nikki
Die Datei test.csv wurde in dasselbe Verzeichnis und in geänderte Pfade verschoben. Jetzt wird 'org.glassfish.jersey.test.inmemory.InMemoryConnector: Fehler beim Schreiben der Entität in den Ausgabestream. ! java.io.FileNotFoundException: test.csv (Keine solche Datei oder Verzeichnis) ..javax.ws.rs.ProcessingException: Fehler beim Schreiben der Entity in den Ausgabestream. bei FileUploadResourceTest.test (FileUploadResourceTest.java:51) Verursacht durch: java.io.FileNotFoundException: test.csv (Keine solche Datei oder das Verzeichnis) bei FileUploadResourceTest.test (FileUploadResourceTest.java:51) '. Die Zeile 51 lautet '.post (Entity.entity (multiPart, multiPart.getMediaType()));' – Nikki
http: //stackoverflow.com/q/19871955/2587435 \ –