Java – generate a large stream for testing
•
Java
We have a web service. We upload files and want to write an integration test to upload a slightly larger file The test process needs to generate files (I don't want to add larger files to source code control)
I want to generate a stream of about 50 MB to upload The data itself is not important I tried this with an object in memory, which was easy, but I ran out of memory
Integration tests are written in groovy, so we can generate data using groovy or Java APIs How do we generate a random stream to upload instead of always saving it in memory?
Solution
This is a simple program that generates a 50 MB text file with random content
import java.io.PrintWriter; import java.util.Random; public class Test004 { public static void main(String[] args) throws Exception { PrintWriter pw = new PrintWriter("c:/test123.txt"); Random rnd = new Random(); for (int i=0; i<50*1024*1024; i++){ pw.write('a' + rnd.nextInt(10)); } pw.flush(); pw.close(); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码