Search code examples
javatestngtestng-annotation-test

How to pass resource bytes as parameters


I have a Test method foo. I have test input large.zst in test/resources. Now I would like to pass the bytes of large.zst as byte[] into foo.

Is there any way of passing the file content as a byte array via Annotations?


Solution

  • Adapting the answer from @tquadrat to testng would look like this IMO:

        @DataProvider(name = "largeCompressed")
        private Object[] createLargeCompressed() {
            // Had no luck using `new Path`.
            URL url = this.getClass().getResource("/large.zst");
            Path path;
            try {
                path = Path.of(url.toURI());
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
            try {
                return new Object[]{Files.readAllBytes(path)};
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        @Test(dataProvider = "largeCompressed")
        public void foo(byte[] largeCompressed) {
            // do something with the byte array …
        }