Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

clover test cases failing because of cast error

wilson wong July 30, 2014

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 votes
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 31, 2014
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.
wilson wong July 31, 2014

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

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 1, 2014
Where did you put the CLOVER:OFF? Did you surround the entire class?
wilson wong August 3, 2014

Just this mehtod "fillSpacesOrZeros"

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 3, 2014

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

You have to exclude the entire class from being instrumented.

0 votes
wilson wong July 31, 2014

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 votes
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 31, 2014
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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events