简体   繁体   中英

How to convert InputStream to FilePart?

I'm trying to test this simple function, that receive a file from controller and extract emails from it.

    fun extractEmailsFromFile(file: Mono<FilePart>, emailsPerFileLimit: Int): Flux<String> {
        return file
                .flatMap { readFileContent(it) }
                .flatMapMany { textContent -> extractAndValidateEmails(textContent, emailsPerFileLimit) }
    }

To do the unit test, I want to put a file on resource folder and read it. How can i convert this inputStream:

javaClass.getResourceAsStream("/upload/emails.csv")

in a FilePart to pass as parameter in my test?

You can try this line:

getClass().getClassLoader().getResourceAsStream("upload/emails.csv")

Besides, in a unit test using JUnit, you can use TemporaryFolder rule to create temporary folders and files if necessary. It's quite useful. For more info: https://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM