clover test cases failing because of cast error

We have uograded the clover to the current version and many of the test cases are failing becasue of the cast failing.

For example :


java.lang.reflect.Field field;

String fieldValue = (String) field.get(segment);

my test case failed with this error

java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String

I have tried //CLOVER:OFF for that function but that didn't help, any idea how to get around this problem?

Thanks in advance

3 answers

0 vote
Could you paste a little bit larger code sample? I'd like to see at least a class signature, a method signature and a code context in which the problematic statement is located.

I have created a test case scenario that replicate this error.

Junit test

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import org.junit.BeforeClass;
import org.junit.Test;
import  my.util.FillerClover;

public class CloverCastTest {
 
 static Foo foo = null;
 
 @BeforeClass
 public static void setupBefoerClass(){
  foo = new Foo();
  foo.setS("foo");
  
  
 }
  
 @Test
 public void testReflection() throws Exception {
  String fieldValue = "";
     for (Field field : foo.getClass().getDeclaredFields()) {
    if (field.getType() == String.class) {   
       fieldValue = (String) field.get(foo);
   }   }  
  
  FillerClover.fillSpacesAndZeros(foo);  
  assertTrue("String value ?", fieldValue.equals("foo"));  
 }
 
}


class Foo {
 
 public String s; 
 
 public String getS() {
  return s;
 }
 public void setS(String s) {
  this.s = s;
 }  
}

FillerClover source

import my.annotation.Character;
import my.annotation.Number;

import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;

public class FillerClover {
 
  
 public static Object fillSpacesAndZeros(Object object) {

  
  for (Field field : object.getClass().getDeclaredFields()) {

   fillSpacesOrZeros(object, field);

  }

  return object;
 }
 
 
 
 private static void fillSpacesOrZeros(Object object, Field field) {
  field.setAccessible(true);

  String fieldValue = StringUtils.EMPTY;

  try {
            if (field.getName().equals("lineItemList")) {
             return ;
            }
   fieldValue = (String) field.get(object);
   if (fieldValue == null ) fieldValue = StringUtils.EMPTY;
   
   Character character = field.getAnnotation(Character.class);
   Number number = field.getAnnotation(Number.class);
   
   if (character != null) {
    
    fieldValue = StringUtils.left(fieldValue, character.value());
    field.set(object,
      StringUtils.rightPad(fieldValue, character.value()));
   }
   else if  (number != null) {
    fieldValue = StringUtils.right(fieldValue, number.value());
    field.set(object, StringUtils.leftPad(fieldValue,
      number.value(), '0'));
   }
   
   
  } catch (IllegalArgumentException e) {
   
   e.printStackTrace();
   StringBuilder sb = new StringBuilder();
   sb.append("error while parsing ").append(field).append(" with ").append(field.getName());
   System.out.println(sb.toString());

  } catch (IllegalAccessException e) {
   e.printStackTrace();
   StringBuilder sb = new StringBuilder();
   sb.append("error while parsing ").append(field).append(" with ").append(field.getName());
   System.out.println(sb.toString());

  }
 }
  
 
}

The error

-------------------------------------------------------------------------------
Test set: my.se.CloverCastTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.124 sec <<< FAILURE!
testReflection(gov.dhs.cbp.se.CloverCastTest)  Time elapsed: 0.015 sec  <<< ERROR!
java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String
 at my.util.FillerClover.fillSpacesOrZeros(FillerClover.java:36)
 at my.util.FillerClover.fillSpacesAndZeros(FillerClover.java:18)
 at my.se.CloverCastTest.__CLR4_0_0syumm48ng(CloverCastTest.java:39)
 at my.se.CloverCastTest.testReflection(CloverCastTest.java:25)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 ...

One interestiing point is that the cast error did not happend in the main line code

if (field.getType() == String.class) {   
       fieldValue = (String) field.get(foo);   // <---- no error
 }

Error occur in the FillerClover Static class???

java.lang.ClassCastException: com_atlassian_clover.TestNameSniffer$1 cannot be cast to java.lang.String
at my.util.FillerClover.fillSpacesOrZeros(FillerClover.java:36)

fieldValue = (String) field.get(object);  //<--- line 36

0 vote
As far as I see your test case is incorrect. You assume that all fields declared in a class are of type String, which is not true because Clover adds a static field of type TestNameSniffer. In one case you're filtering-out the Clover's field by "if (field.getType() == String.class)". In a second case you take all fields and cast them without checking the type. This is why a ClassCastException is thrown.

Thanks.But why ///CLOVER:OFF will not disable the Clover static field TestNameSniffer.

Where did you put the CLOVER:OFF? Did you surround the entire class?

Just this mehtod "fillSpacesOrZeros"

OK. So this is an explanation, as I wrote before.

You have to exclude the entire class from being instrumented.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Confluence

Three common content challenges + how to manage them

An efficient enterprise content management system, or ECM, is a must-have for companies that create work online (cough   cough, all companies). If content calendars, marketing plans, and bu...

61 views 0 4
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