alibaba / testable-mock

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

被测试类的方法没有被mock #323

Open xiaoweibahaha opened 1 year ago

xiaoweibahaha commented 1 year ago

package cn.sunline.risk.management;

import com.alibaba.testable.core.annotation.MockMethod; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.stereotype.Component;

@SpringBootTest(classes = Application.class) public class DemoTest { @Autowired private DemoService1 demoService1;

public static class Mock{

    @MockMethod(targetClass = DemoService1.class)
    public String test2(String x){
        return "123";
    }

    @MockMethod(targetClass = DemoService1.class)
    public String test3(String x){
        return "456";
    }

}

@Test
void testDemo2(){

    System.out.println(demoService1.test1("a"));

}
@Test
public void Demo2(){

/* String a = "zhao"; Mockito.when(demoService1.test2(a)).thenReturn("zhaoxiaowei_v"); Mockito.when(demoService1.test3(a)).thenReturn("zhaoxiaowei_v");

    String s = demoService.test1(a);
    System.out.println(s);
    //Assert.assertEquals(demoService.test1(a),"zhaoxiaowei_v");

*/

}

}

@Component class DemoService{ @Autowired DemoService1 demoService1;

} @Component class DemoService1{

public String  test1(String x){
    System.out.println("*****"+x);
    return test2(x);
}
public String test2(String x){
    System.out.println("---1"+x);
    return x ;
}

public String test3(String x){
    System.out.println("---2"+x);
    return x ;
}

}

xiaoweibahaha commented 1 year ago

输出的结果 *****a ---1a a

日志: [INFO] Start at Tue Apr 25 21:41:11 CST 2023 [INFO] TestableMock start at /Users/didi/Desktop/zhaoxw/workspace/risk-management-service/risk-management-service-20230403_page [INFO] Found test class cn/sunline/risk/management/DemoTest [INFO] Found 2 test cases [INFO] Completed at Tue Apr 25 21:42:38 CST 2023