alibaba / testable-mock

换种思路写Mock,让单元测试更简单
https://alibaba.github.io/testable-mock/
MIT License
1.83k stars 310 forks source link

OmniConstructor工具类,开启了字节码增强,但mock对象时还是会随机偶发报错 #314

Open caofanCPU opened 1 year ago

caofanCPU commented 1 year ago
image

JDK jdk1.8.0_291.jdk

IDEA版本: IntelliJ IDEA 2022.3.1 (Ultimate Edition) Build #IU-223.8214.52, built on December 20, 2022

caofanCPU commented 1 year ago

经过排查,发现testable.properties配置文件不生效 我是使用spock测试框架

配置文件不生效怎么排查呢

caofanCPU commented 1 year ago

再排查,发现可能是环境上哪里有问题,导致testable-agent无法直接识别出test/resource下的testable.properties配置文件,使用pom指定相对于根路径的path,可以成功

image
caofanCPU commented 1 year ago

使用的是spock框架,开启了增强omni.constructor.enhance.enable=true 测试用例里使用spock的Mock方法时,会出现报错:构造方法重复

Duplicate method name "" with signature "(Ljava.lang.Void;)V" in class

请问spock对于spock的搭配,具体实践还是new 实际类麽?

附上场景代码

`class OmniConstructorTest extends Specification {

@SpringBean
private SomeServiceClass contractClient = Mock()

@MockDiagnose(LogLevel.VERBOSE)
static class Mock {

}

@Unroll
def "omniConstructor"() {
    def bigDecimal = OmniConstructor.newInstance(BigDecimal.class)
    def date = OmniConstructor.newInstance(Date.class)
    def longNumber = OmniConstructor.newInstance(Long.class)
    def bigInt = OmniConstructor.newInstance(BigInteger.class)

    expect:
    assert 0 == OmniConstructor.newInstance(int.class)
    assert 0L == OmniConstructor.newInstance(Long.class)
    assert "" == OmniConstructor.newInstance(String.class)
    assert LogLevel.DISABLE == OmniConstructor.newInstance(LogLevel.class)
    assert 0.0 == bigDecimal
    assert date.getTime() > 0
    assert longNumber == 0L
    assert bigInt.intValue() == 0
}

}`