Testfx给出了java.lang.NoSuchMethodError

问题描述 投票:0回答:2

我将测试更改为TestFx,但问题仍然存在。

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/ILAreaManagement/ILArea.fxml"));
        Parent root = fxmlLoader.load();

        MaintenanceController maintenanceController = fxmlLoader.getController();

        Platform.runLater(() ->{
            try {
                maintenanceController.initialize(maintenanceAreaObjectObservableList);
            } catch (Exception e) {

它提供了setTitle方法

java.lang.NoSuchMethodError:gov.tubitak.ys03.sysmaintenance.utilities.BorderedTitledPane.setTitle(Ljava / lang / String;)V

((BorderedTitledPane)ILAreaPane)。setTitle(“IL Area”);

public class BorderedTitledPane extends StackPane {

    Label title = new Label(" ");

    public BorderedTitledPane(Node content) {
        StackPane contentPane = new StackPane();
        content.getStyleClass().add("bordered-titled-content");
        contentPane.getChildren().add(content);

        getStyleClass().add("bordered-titled-border");
        getChildren().addAll(title, contentPane);
    }

    public void setTitle(String title) {
        this.title.setText(title);
        this.title.getStyleClass().add("bordered-titled-title");
        StackPane.setAlignment(this.title, Pos.TOP_CENTER);
    }

}

我将setTitle更改为public Label getTitle(){return title;}但是我得到了与Ljavafx / scene / control / Label相同的错误。我不明白例外的原因。

unit-testing javafx label mockito testfx
2个回答
0
投票

如果你不能进行任何重构,你需要去Powermock - compatibility table and wiki

然后,您需要设置测试,以便在创建新的Label()时,使用实际的模拟。标签不是最终的,因此您可以轻松地模拟它:

@PrepareForTest(Label.class)
@RunWith(PowerMockRunner.class)
public class MyTest{

    @Test
    public void init() throws Exception {
        Label labelMock = Mockito.mock(Label.class);
        PowerMockito.whenNew(Label.class)
            .withNoArguments().thenReturn(labelMock);

        // rest of your test
        ....
    }

0
投票

关于BorderedTitlePane类的例外情况。我改变了它,我的测试通过了。我想分享更改的BorderedTitlePane类:

BorderedTitlePane类由@jewelsea编写。当标题必须改变时,我改变它以设置标题。但我必须使用StringProperty。

public class BorderedTitledPane extends StackPane {

    private StringProperty title = new SimpleStringProperty();

    public BorderedTitledPane(String titleString, Node content) {
        final Label titleLabel = new Label();
        titleLabel.textProperty().bind(Bindings.concat(title, ""));
        titleLabel.getStyleClass().add("bordered-titled-title");
        title.set(titleString);
        StackPane.setAlignment(titleLabel, Pos.TOP_CENTER);

        StackPane contentPane = new StackPane();
        content.getStyleClass().add("bordered-titled-content");
        contentPane.getChildren().add(content);

        getStyleClass().add("bordered-titled-border");
        getChildren().addAll(titleLabel, contentPane);
    }

    public String getTitle(){
        return title.get();
    }

    public StringProperty getTitleStringProperty(){
        return title;
    }

    public void setTitle(String title){
        this.title.set(title);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.