I want to create an instance of class with only one internal
constructor for unit-tests purpose. I am running Guidewire version 10.
Getting error on string with .newInstance
call:
var s3ServiceCtor = InternalBucketS3Service.getClass().getDeclaredConstructors().first()
s3ServiceCtor.setAccessible(true)
var newInstanceResult = s3ServiceCtor.newInstance({_testClaim}.toArray())
return newInstanceResult as S3Service
Here is a class I want to create:
class InternalBucketS3Service extends AbstractS3Service {
private var _claim : Claim
internal construct(claim : Claim) {
super(Configuration.S3InternalBucket)
_claim = claim
}
// no more constructors, only methods ...
And I am getting this error:
junit.framework.AssertionFailedError: Exception in constructor: testGeneralS3ServicesCompare (java.lang.IllegalArgumentException
at jdk.internal.reflect.GeneratedConstructorAccessor141.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at ru.tinkoff.util.s3.S3TestServiceImplComparisonTest.getRealS3(S3TestServiceImplComparisonTest.gs:59)
at ru.tinkoff.util.s3.S3TestServiceImplComparisonTest.<init>(S3TestServiceImplComparisonTest.gs:25)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
How to fix it?
There is another way how to create instance:
But seems like it works only for
private
constructors. In this case i cant use it, needinternal
.Am just gonna make constructor
public
and add@Deprecated
to it to mark "for tests only". Please comment if it is actually possible to createinternal
object in Gosu via reflection.