简体   繁体   中英

How to verify what parameters are passed to a static method in java

As a part of my Junit tests, I want to verify if I am calling a static method of an external class with the right parameters.

eg:- Suppose I have the following as the class under tests.

class A {
    public static void someMethod(String param){
          some.thirdpartyClass.someStaticMethod(param);
    }      
}

Now I want to test as a part of the test for someMethod, that I called the someStaticMethod with the parameter param

Whats the easiest way to do this? I tried going through power mock but couldn't find a way.

It's not difficult with PowerMock, but here is an easier solution using JMockit:

public class ATest
{
    @Test
    public void testSomeMethodInIsolation(@Mocked ThirdPartyClass tpc)
    {
        final String param = "testing";

        new A().someMethod(param);

        new Verifications() {{ ThirdPartyClass.someStaticMethod(param); }};
    }
}

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