Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xiaoweibahaha opened this issue Apr 25, 2023 · 1 comment
Open

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

xiaoweibahaha opened this issue Apr 25, 2023 · 1 comment

Comments

@xiaoweibahaha
Copy link

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
Copy link
Author

输出的结果
*****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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant