I am using JavaFX to write a sample video player. But the tests fails when run together, ( Note: individually test passes).
Error: Unexpected exception thrown: java.lang.IllegalStateException: Application launch must not be called more than once
I understand that calling launch() twice on same Application Instance is causing this issue as per this . But I am not able to understand, that after one test completes, why the app is still running ? Why new instance is not getting created for 2nd test. testUnsupportedVideoFileThrowsError() succeeds but testSupportedVideoFilePlaysSuccessfully() fails.
package media;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
public class PlayVideoSnippet extends Application {
    private static String source;
    private Media media = null;
    private MediaPlayer mediaPlayer = null;
    private MediaView mediaView = null;
    private Scene scene = null;
    public static void playVideo(String source) {
        PlayVideoSnippet.source = source;
        PlayVideoSnippet.launch();
    }
    @Override
    public void start(Stage stage) throws InterruptedException {
        try {
            media = new Media(new File(source).toURI().toString());
            //onError close the app
            media.setOnError(() -> {
                Platform.exit();
            });
            //Create MediaPlayer, with media
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setAutoPlay(true);
            //onEnd of media, close the app
            mediaPlayer.setOnEndOfMedia(() -> {
                Platform.exit();
            });
            //Create media viewer
            mediaView = new MediaView(mediaPlayer);
            //create scene
            scene = new Scene(new Group(), media.getWidth(), media.getHeight());
            stage.setScene(scene);
            stage.setTitle("Video Player");
            //attach the mediaView to the scene root
            ((Group) scene.getRoot()).getChildren().add(mediaView);
            stage.show();
        } finally {
            System.out.println("Inside finally");
            stage.close();
        }
    }
}
package media;
import javafx.scene.media.MediaException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PlayVideoSnippetTest {
    /**
     * Tests for {@link PlayVideoSnippet#playVideo(String)}.
     */
    final String PATH_OF_SUPPORTED_VIDEO_FILE = "src/test/resources/file_example_MP4_480_1_5MG.mp4";
    final String PATH_OF_UNSUPPORTED_VIDEO_FILE = "src/test/resources/file_example_WMV_480_1_2MB.wmv";
    @Test
    void testSupportedVideoFilePlaysSuccessfully() {
        assertDoesNotThrow(() -> PlayVideoSnippet.playVideo(PATH_OF_SUPPORTED_VIDEO_FILE));
    }
    @Test
    void testUnsupportedVideoFileThrowsError() {
        RuntimeException exception = assertThrows(RuntimeException.class, () -> PlayVideoSnippet.playVideo(PATH_OF_UNSUPPORTED_VIDEO_FILE));
        assertTrue(exception.getCause().getClass().equals(MediaException.class));
    }
}
