mardi 9 juin 2020

Test if method is called once application started

What is happening : When application starts method is being called and return is saved to a property of an object.

I would like to write a test that checks that onApplicationEvent my method is called and bytes is no longer empty, is there a way to proceed?

public class FaqAttachment implements ApplicationListener<ApplicationStartedEvent> {

private final String fileName = "FAQ.pdf";
private volatile byte[] bytes;

public Attachment asAttachment() {
    return new Attachment(pdfToBytes(), fileName);
}

private synchronized byte[] pdfToBytes() {
    if (bytes == null) {
        try (FileInputStream inputStream = new FileInputStream(new File(ClassLoader.getSystemResource(fileName).getFile()))) {
            this.bytes = ByteStreams.toByteArray(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return bytes;
}

@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
    pdfToBytes();
}


 @Test
public void attachmentBytesShouldNotBeEmptyOnceApplicationStarted ()  {

}

Aucun commentaire:

Enregistrer un commentaire