java - 如何更新测试类中intellij idea插件的持久状态?

标签 java testing intellij-idea intellij-plugin

我正在为 Intellij Idea 开发插件并进行一些测试。在我的一项测试中,我需要更新持久状态。但它并没有发生,并且 loadStategetState 都没有被调用。

我编写了实现PersistingStateComponent的类(我认为我做得正确,因为这不是我第一次这样做)。在一项测试中,我调用将数据添加到 State 类的方法,并且已成功添加但未保存。另一个应该获取该数据,但它的状态为空。测试类实现LightPlatformTestCase。

文档说:

Persistent Component Lifecycle The loadState() method is called after the component has been created (only if there is some non-default state persisted for the component), and after the XML file with the persisted state is changed externally (for example, if the project file was updated from the version control system). In the latter case, the component is responsible for updating the UI and other related components according to the changed state.

The getState() method is called every time the settings are saved (for example, on frame deactivation or when closing the IDE). If the state returned from getState() is equal to the default state (obtained by creating the state class with a default constructor), nothing is persisted in the XML. Otherwise, the returned state is serialized in XML and stored.

那么有可能这些情况都不会发生吗?

我可以在测试方法中做一些事情来更新我的持久状态吗?

或者它应该可以工作,我应该在我的代码中查找问题?

更新:当我运行插件时,它工作正常。

我的类看起来像这样:

@State(name = "MyStateName", storages = {@Storage(id="MyStateId", file = "D:/MyStateName.xml")})
public class MyClass
  implements PersistentStateComponent<MyClass.State> {

  public static class State{
    Integer someValue  = 10;
  }

  State myState = new State();

  public State getState() {
    return myState;
  }

  public void loadState(State state) {
    myState = state;
  }

  public SFApexClassWrapp getValue() {
    return myState.value;
  }

  public void addValue(Integer value) {
    myState.value = value;
  }

}

最佳答案

当您使用 PersistentStateComponent 时,作为插件开发人员的唯一责任是返回 State 类的实例。因此,作为插件开发人员,唯一对您进行测试有意义的是您的实例是否正确返回。您不需要测试 XML 序列化,因为它是 IntelliJ IDEA 代码的一部分,这不是您的责任,并且已经经过了相当好的测试。

这就是为什么当 IntelliJ IDEA 为您的代码运行测试时,它不会保存或加载组件的设置。如果需要,您可以手动触发设置保存,但一般情况下您没有必要这样做。

请发布有关存储配置对象的多个实例的单独问题。

关于java - 如何更新测试类中intellij idea插件的持久状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26792189/

相关文章:

java - IntelliJ 插件开发卡住目标 IntelliJ 实例

ruby-on-rails - 分别使用 Cucumber 和 Rspec 如何测试以及测试什么?

reactjs - 如何测试使用 enzyme 改变 useState 状态的点击事件?

ruby-on-rails - 如何使用 fabrication in rails 编写 factory_girl create 命令

intellij-idea - Android Studio(Intellij Idea)-运行/调试配置-记住要在哪个设备上运行

java - AndroidAnnotations 和 Dagger

Java System.out.println 格式

java - 如何通过 Java 代码影响 System.loadLibrary() 的搜索路径?

java - gradle 进程命令 java 已完成,退出值非零 1

Java 一维数组