From a394adbb44309cb74efdd611f3b6e31c267e21c0 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 9 Mar 2017 18:52:23 +0100 Subject: [PATCH] Work around JaCoCo injecting fields/methods into classes when the tests are run from gradle --- .../net/corda/carpenter/ClassCarpenterTest.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/experimental/src/test/kotlin/net/corda/carpenter/ClassCarpenterTest.kt b/experimental/src/test/kotlin/net/corda/carpenter/ClassCarpenterTest.kt index fdc780bc48..5ffe32e51d 100644 --- a/experimental/src/test/kotlin/net/corda/carpenter/ClassCarpenterTest.kt +++ b/experimental/src/test/kotlin/net/corda/carpenter/ClassCarpenterTest.kt @@ -1,6 +1,8 @@ package net.corda.carpenter import org.junit.Test +import java.lang.reflect.Field +import java.lang.reflect.Method import kotlin.test.assertEquals @@ -12,11 +14,16 @@ class ClassCarpenterTest { val cc = ClassCarpenter() + // We have to ignore synthetic fields even though ClassCarpenter doesn't create any because the JaCoCo + // coverage framework auto-magically injects one method and one field into every class loaded into the JVM. + val Class<*>.nonSyntheticFields: List get() = declaredFields.filterNot { it.isSynthetic } + val Class<*>.nonSyntheticMethods: List get() = declaredMethods.filterNot { it.isSynthetic } + @Test fun empty() { val clazz = cc.build(ClassCarpenter.Schema("gen.EmptyClass", emptyMap(), null)) - assertEquals(0, clazz.declaredFields.size) - assertEquals(2, clazz.declaredMethods.size) // get, toString + assertEquals(0, clazz.nonSyntheticFields.size) + assertEquals(2, clazz.nonSyntheticMethods.size) // get, toString assertEquals(0, clazz.declaredConstructors[0].parameterCount) clazz.newInstance() // just test there's no exception. } @@ -33,9 +40,9 @@ class ClassCarpenterTest { "byteMe" to Byte::class.javaPrimitiveType!!, "booleanField" to Boolean::class.javaPrimitiveType!! ))) - assertEquals(8, clazz.declaredFields.size) + assertEquals(8, clazz.nonSyntheticFields.size) + assertEquals(10, clazz.nonSyntheticMethods.size) assertEquals(8, clazz.declaredConstructors[0].parameterCount) - assertEquals(10, clazz.declaredMethods.size) val i = clazz.constructors[0].newInstance(1, 2L, 'c', 4.toShort(), 1.23, 4.56F, 127.toByte(), true) assertEquals(1, clazz.getMethod("getAnIntField").invoke(i)) assertEquals(2L, clazz.getMethod("getALongField").invoke(i))