IllegalAccessException when running clover test

I am getting the below error when running the clover test. Same test runs fine during the unit test.

This is happening when i am trying to copy an object using reflection.

Do we have any work around for this issue. 

Clover Version:4.0.2

Error:

Caused by: java.lang.IllegalAccessException: Can not set static final com_atlassian_clover.TestNameSniffer field <<XXX Class>>.__CLR4_0_2_TEST_NAME_SNIFFER to com_atlassian_clover.TestNameSniffer$1
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)
at sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(UnsafeQualifiedStaticObjectFieldAccessorImpl.java:77)
at java.lang.reflect.Field.set(Field.java:764)

 

Code that copies the object:

Class<? extends Object> copy1 = entity.getClass();

 Class<? extends Object> copy2 = entity2.getClass();

Field[] fromFields = copy1.getDeclaredFields();
Field[] toFields = copy2.getDeclaredFields();
Object value = null;
for (Field field : fromFields) {
Field field1 = copy2.getDeclaredField(field.getName());
value = field.get(entity);
  field1.set(entity2, value);

}

 

2 answers

This widget could not be displayed.

As you can see in the exception message, you're trying to write to a static final field.

Change your copying method to skip fields of type TestNameSniffer.

This widget could not be displayed.

Thanks Marek. This worked

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published yesterday in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

101 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you