首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运行两个测试用例时,“应用程序启动不能被多次调用”JavaFX错误

运行两个测试用例时,“应用程序启动不能被多次调用”JavaFX错误
EN

Stack Overflow用户
提问于 2022-10-27 10:44:25
回答 1查看 41关注 0票数 0

我正在使用JavaFX编写一个示例视频播放器。但是,当一起运行时,测试会失败(注意:单独测试通过)。

错误:Unexpected exception thrown: java.lang.IllegalStateException: Application launch must not be called more than once

据我所知,在同一个应用程序实例上调用两次launch()会导致这个问题。但是我无法理解,在一个测试完成之后,为什么这个应用程序还在运行呢?为什么没有为第二次测试创建新实例。testUnsupportedVideoFileThrowsError()成功了,但testSupportedVideoFilePlaysSuccessfully()失败了。

代码语言:javascript
运行
复制
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();
        }

    }
}
代码语言:javascript
运行
复制
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));
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-10-30 06:04:11

我能够通过以下输入来解决这个问题:

@jewelsea.

  • Inputs从@James_D

提出的用于测试目的的

ApplicationTest.launch()方法内部负责设置初级阶段,并在每次测试之前进行清理。

解决方案:

代码语言:javascript
运行
复制
import org.junit.jupiter.api.Test;
import org.testfx.framework.junit5.ApplicationTest;
class VideoTest extends ApplicationTest {
  
  final String pathOfSupportedFile = "video.mp4";
  final String pathOfUnsupportedFile = "video.wmv";


  @Test
  void testSupportedVideoFilePlaysSuccessfully() throws Exception {
    assertDoesNotThrow(() -> PlayVideoSnippetTest.launch(PlayVideoSnippet.class,
            new String[]{pathOfSupportedFile}));
  }

  @Test
  void testUnsupportedVideoFileThrowsError() throws Exception {
    RuntimeException exception = assertThrows(RuntimeException.class,
        () -> PlayVideoSnippetTest.launch(PlayVideoSnippet.class,
                    new String[]{pathOfUnsupportedFile}));
    assertTrue(exception.getCause().getCause().getClass().equals(MediaException.class));
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74220813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档