Corda 如何解决单元测试“LinearState”的普通实现时引发的“NotSerializableException”
我有一个LinearState的简单实现,其中唯一的参数是UniqueIdentifier。我有一份简单的合同 然而,当我尝试对它进行单元测试时,我总是得到NotSerializabeException 我正在使用Corda v4 java模板。在IntelliJ上使用Gradle Runner测试设置 我试着用@CordaSerializable注释类,用@ConstructorForDeserialization注释构造函数Corda 如何解决单元测试“LinearState”的普通实现时引发的“NotSerializableException”,corda,Corda,我有一个LinearState的简单实现,其中唯一的参数是UniqueIdentifier。我有一份简单的合同 然而,当我尝试对它进行单元测试时,我总是得到NotSerializabeException 我正在使用Corda v4 java模板。在IntelliJ上使用Gradle Runner测试设置 我试着用@CordaSerializable注释类,用@ConstructorForDeserialization注释构造函数 @CordaSerializable public class I
@CordaSerializable
public class InvestorState implements LinearState {
private final UniqueIdentifier linearId;
@ConstructorForDeserialization
public InvestorState(UniqueIdentifier id) {
this.linearId = id;
}
...
}
我发现构造函数中列出的参数必须与声明的变量相同 我的更正代码:
@BelongsToContract(InvestorContract.class) // note this is required for Corda v4
@CordaSerializable // not strictly necessary as this class implements LinearState
public class InvestorState implements LinearState {
private final UniqueIdentifier linearId;
@ConstructorForDeserialization // only necessary if there are multiple constructors
public InvestorState(UniqueIdentifier linearId) {
this.linearId = linearId;
}
...
}
在我的例子中,将构造函数参数“id”更改为最初声明的“linearId”解决了问题只需我的两分钱供将来参考: 建议Corda中的可序列化对象遵守以下规则,因为它们允许对不可变状态对象进行反序列化: 构造函数中每个属性的JavaBean getter,名称为getX。例如,对于构造函数参数foo,必须有一个名为getFoo的getter。 如果foo是一个布尔值,那么getter可以选择性地称为isFoo这就是为什么必须在编译类时启用参数名的原因 一个构造函数,它接受您希望以序列化形式记录的所有属性。 这是序列化框架重建类实例所必需的 如果提供了多个构造函数,序列化框架需要知道使用哪个构造函数。@ConstructorForDeserialization注释可用于指示哪个注释 对于没有@ConstructorForDeserialization注释的Kotlin类,将选择主构造函数
@BelongsToContract(InvestorContract.class) // note this is required for Corda v4
@CordaSerializable // not strictly necessary as this class implements LinearState
public class InvestorState implements LinearState {
private final UniqueIdentifier linearId;
@ConstructorForDeserialization // only necessary if there are multiple constructors
public InvestorState(UniqueIdentifier linearId) {
this.linearId = linearId;
}
...
}