简体   繁体   中英

test overloaded java methods with spock

In my eclipse project I've got a java class that validates if objects of different types are empty using overloaded methods:

public class EmptyProof {

    public static boolean isEmpty(String field) {
        System.out.println("String " + field);
        return field == null || field.trim().equals("");
    }

    public static boolean isEmpty(BigDecimal d) {
        System.out.println("BI " + d.toString());
        return d == null || d.compareTo(new BigDecimal("0")) == 0;
    }

    public static boolean isEmpty(Object input) {
        System.out.println("obj " + input.toString());
        return input == null || String.valueOf(input).length() == 0;
    }
}

Now I want to write a Unit Test in Spock:

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import spock.lang.Specification;

class EmptyProofSpec extends Specification {

    def 'must evaluate emptiness of a String correctly'() {

        expect: "isEmpty for String returns the right answer"
            System.out.println("test string " + bi.toString());
            EmptyProof.isEmpty(str as String) == result

        where: "using this table"
            str                     || result
            ""                      || true 
            null                    || true
            "a"                     || false
    }

    def 'must evaluate emptiness of a BigInteger correctly'() {

        expect:
            System.out.println("test BigInt " + bi.toString());
            EmptyProof.isEmpty(bi as BigInteger) == result

        where: "using this table"
            bi                                              || result
            BigInteger.ONE                                  || false
            new BigInteger(Integer.MIN_VALUE) as BigInteger || false
//          null as BigInteger                              || true
//          BigInteger.ZERO  as BigInteger                  || true
    }

}

This brings me the following output in the console:

test string 
String 
test string null
test string a
String a
test BigInt 1
obj 1
test BigInt -2147483648
obj -2147483648

As you can see my tests with String objects call isEmpty(String). But my calls with BigInteger do not call isEmpty(BigInteger) but isEmpty(Object). I would like to add my test with BigInteger.ZERO but this would fail as the Object-method does not care about 0's.

I already tried some things like casting and the @CompileStatic annotation. Yet without success.

Can I instruct Spock to use the BigInteger method without a change in my Java class?

The reason it fails over to isEmpty(Object) is because there is no method defined for BigInteger . The one which is present is defined for BigDecimal

public static boolean isEmpty(BigDecimal d) { ... }

Add/edit one for BigInteger .

Also note, null case will fall back to Object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM