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

How to Mock static method in System Class or Third party Class #1151

Open
1017073490 opened this issue Mar 6, 2023 · 0 comments
Open

How to Mock static method in System Class or Third party Class #1151

1017073490 opened this issue Mar 6, 2023 · 0 comments

Comments

@1017073490
Copy link

Thank you for reading!

I want to Mock static method in System Class such as Math.java.

I have consulted the official MockStaticCases.java and code

public void testTestM10() throws Exception { System.out.println(Math.sin(60) < (-0.4)); mockStatic(Math.class); doReturn(-0.8).when(Math.sin(60)); assertTrue(Math.sin(60) < (-0.4)); verifyStatic(EmployeeService.class); Math.sin(60); }

it show me the following content in the console:

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.zhangxing.mybatis.pojo.EmployeeServiceTest.testTestM10(EmployeeServiceTest.java:112)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:

  1. missing thenReturn()

  2. you are trying to stub a final method, which is not supported
    3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed

    at com.zhangxing.mybatis.pojo.EmployeeServiceTest.testTestM10(EmployeeServiceTest.java:112)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Looking forward to your reply

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