1

我找到了这个有用的库PODAM,但我很难获得自动字节数组。我使用了实现的 AttributeStrategy

public class LogoStrategy implements AttributeStrategy<byte[]>{

private static final int MAX_SIZE_FILE = 512*1024;

  @Override
  public byte[] getValue() throws PodamMockeryException {
    byte[] b = new byte[20];
    new Random().nextBytes(b);
    return b;
  }

}

但是当我使用我得到这个错误:

2014-10-21 20:13:04 PodamFactoryImpl [ERROR] The type of the Podam Attribute 
Strategy is not java.lang.String but [B. An exception will be thrown.

Exception in thread "main" uk.co.jemos.podam.exceptions.PodamMockeryException: An illegal argument was passed
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:1569)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:129)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:119)
at cl.molavec.jpa.entities.singleton.QuotationPropertiesSingleton.getNewInstance(QuotationPropertiesSingleton.java:26)
at cl.molavec.main.InsertDummyData.main(InsertDummyData.java:66)

Caused by: java.lang.IllegalArgumentException: The type of the Podam Attribute Strategy is not java.lang.String but [B. An exception will be thrown.
at uk.co.jemos.podam.api.PodamFactoryImpl.returnAttributeDataStrategyValue(PodamFactoryImpl.java:2888)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:1493)
... 4 more

我实现了 AttributeStrategy 但使用 char[] 属性并且一切都很好。我不明白为什么需要一个字符串的错误。

有什么建议么?

提前致谢。

4

1 回答 1

1

我猜您将注释附加@PodamStrategyValue到字符串字段。

@PodamStrategyValue(LogoStrategy.class)
String myAttribute;

由于LogoStrategy返回byte[]它不能分配给字符串,因此异常。

将字段类型更改为byte[]

@PodamStrategyValue(LogoStrategy.class)
byte[] myAttribute;

或使LogoStrategy返回字符串

public class LogoStrategy implements AttributeStrategy<String>{

    private static final Random rnd = new Random();

    @Override
    public String getValue() throws PodamMockeryException {
        byte[] bytes = new byte[20];
        rnd.nextBytes(bytes);
        return DataTypeConverter.printHexBinary(bytes);
    }
}
于 2015-05-03T16:39:15.880 回答