1

I changed my test to TestFx but problem is persist.

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) {

When it comes to the setTitle method it gives

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);
    }

}

I changed setTitle to public Label getTitle(){return title;} but I get same error with Ljavafx/scene/control/Label. I don't understand reason of exception.

2 Answers2

0

You would need to go for Powermock if you cannot do any refactoring - compatibility table and wiki.

Then you would need to set up you test, so that when a new Label() is created, an actual mock is used instead. Label is not final so you can mock it easily:

@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
        ....
    }
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

Exception is about BorderedTitlePane class. I changed it and my test passed. I want to share changed BorderedTitlePane class:

BorderedTitlePane class writed by @jewelsea. I changed it for set the title when title have to change. But I must use 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);
    }

}