我将测试更改为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相同的错误。我不理解例外的原因。
答案 0 :(得分: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
....
}
答案 1 :(得分:0)
异常是关于BorderedTitlePane类。我改变了它,我的测试通过了。我想分享更改的BorderedTitlePane类:
由@jewelsea撰写的BorderedTitlePane课程。当标题必须改变时,我改变它以设置标题。但我必须使用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);
}
}