I'm having the following flow:
manager.getObject.doSomthing();
Where doSomething() calls a static function from a Utils class, that in turn, calls a private static function e.g:
public class obj {
public void doSomething(){
Utils.do();
}
}
public class Utils {
public static void do(){
int test = doPrivate();
...
~do unrelated computation~
...
}
private static int doPrivate(){
return someComplexMethod();
}
}
And I would like to mock the doPrivate, so I would still be able to test the do() method
Any way to achieve it with powermockito?
Using powermock-api-mockito you can achieve this.
You can mock specific static method of a class. Below is the syntax:
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
stub(method(Utils.class, "doPrivate")).toReturn(response);
Related
I was trying to access the non static method from class A to a static method in class B
#Component
public class B{
private static A aA1;
#Autowired
private A tA;
#PostConstruct
public void init(){
//<<<Make the enclosed method Static or remove this set>>>
B.aA1=tA;
}
public static void random method(){
aA1.doStuff();
}
}
Is it okay to make the init method static, as the sonar suggest
Or is there another way i could solve this?
Am trying to mock one static void method, I tried PowerMock but always getting NullPointer exception.
We are trying to mock the below call -
public Class XYZ{
public void method1(){
....
SampleClass.methodTypeStatic1().methodTypeStatic2("xyz", "mno", classVeriable);
}
And the main class is -
public class SampleClass implements SampleClassParent{
private static SampleClass var1;
public static SampleClass methodTypeStatic1(){
if (var1 == null) {
//Do something on var1
}
return var1;
}
public void methodTypeStatic2(String localVar1, String localVar2, DifferentStaticClass localVar3) {
//Do something
}}
Am trying to mock the call like this way in my test class -
#RunWith(PowerMockRunner.class)
#PrepareForTest({SampleClass.class})
public class XYZTest{
#InjectMocks
XYZ xyzTestService;
#Test
public void testMethod1(){
...
PowerMockito.mockStatic(SampleClass.class);
PowerMockito.doNothing().when(SampleClass.methodTypeStatic1());
xyzTestService.method1();
}
Also to be mentioned SampleClass.class is not a part of our code, we have imported this external class into our code base to perform some task.
If you want to mock a non-void method, you need to define the behaviour for this method on the mock. You get the doNothing for free (as this is what mocking does),
however you need to define what the method is supposed to return.
Your test could look like this:
#Test
public void testMethod1() {
SampleClass sample = new SampleClass();
PowerMockito.mockStatic(SampleClass.class);
PowerMockito.when(SampleClass.methodTypeStatic1()).thenReturn(sample);
XYZ xyzTestService = new XYZ();
xyzTestService.method1();
}
You didn't not mention any #Mock annotations, so using #InjectMocks does nothing for you besides creating the XYZ object.
I have below interface and its implementation class.
Demo.java
public interface Demo{
void showDemo();
}
DemoImpl.java
#Service
public class DemoImpl implements Demo{
public void showDemo(){
//To Do
}
}
Now i have one class with static method which will internally call showDemo() as below.
DemoStatic.java
#Component
public class DemoStatic{
#Autowired
private Demo demo;
public static void callShowDemo(){
demo.showDemo(); //calling non static method from static method
}
}
Here i am calling non static method from static method. Is my design correct? Or do i need to change my design? Please suggest me.
Thanks!
You can do it this way
#Component
public class DemoStatic {
private static Demo demo;
#Autowired
public void setDemo(Demo d) {
demo = d;
}
public static void callShowDemo(){
demo.showDemo(); //calling static method from static method
}
}
No your design is not correct.
private Demo demo;
has to be
private static Demo demo;
You just cant use NON static members in a static method
The above code will not compile at all. You are trying to refer non-static reference from static method, which is not allowed in Java.
Refer this stackoverflow link for better understanding.
Make the following change:
public static Demo demo;
when you call DemoStatic.callShowDemo(),the demo may not been instand
I'm currently trying to mock a private final static object within a class. It doesnt seem like my object is being mocked properly.
Example:
Code: In main class
public class Main {
private final static serviceA obj = new serviceA();
public somemethod { return true; }
}
Mocked: In my test class I have
Public class TestMain {
private Main mainObj;
private static serviceA obj;
#Before
public void setupBeforeTest() {
obj = Mockito.mock(serviceA.class);
PowerMockito.whenNew(serviceA.class).withNoArguments().thenReturn(obj);
mainObj= Mockito.spy(new Main());
}
}
But obj doesnt return the values I specify when doing
Mockito.when(obj.returnsFalseMethod()).thenReturn(false);
and will actually run the code for obj.returnsFalseMethod().
Any advice would be helpful, also i cannot change any code in the Main class, thanks.
I didn't realize you have to prepare the class creating the object. I was preparing every class except the class instantiating the object.
Suppose we have the following code:
class Test {
private Test() {
System.out.println("test");
}
}
public class One extends Test {
One() {
System.out.println("One");
}
public static void main(String args[]) {
new One();
}
}
When we create an object One, that was originally called the parent class constructor Test(). but as Test() was private - we get an error.
How much is a good example and a way out of this situation?
There is no way out. You have to create an available (protected, public or default) super constructor to be able to extend test.
This kind of notation is usually used in utility classes or singletons, where you don't want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.
When you have a class with only private constructors, you can also change the class to final because it can't be extended at all.
Another solution would be having a method in test which create instances of test and delegate every method call from One to a test instance. This way you don't have to extend test.
class Test {
private Test() {
System.out.println("test");
}
public static Test getInstance(){
return new Test();
}
public void methodA(){
//Some kind of implementation
}
}
public class One {
private final Test test;
One() {
System.out.println("One");
test = Test.getInstance();
}
public void methodA(){
test.methodA();
}
public static void main(String args[]) {
new One();
}
}
Make the constructor of test non-private or move One into test.
BTW, your sample code contains a few issues:
classes should be named title case (Test instead of test)
I'd suggest to make the One's constructor private unless it is called from a different class in the same package
Actually, I found there is a way out. Like this:
class Base {
private Base() {
}
public void fn() {
System.out.println("Base");
}
public static class Child extends Base {
public void fn() {
System.out.println("Child");
}
}
public static Base getChild() {
return new Child();
}
}
Now, you can use getChild() to get instance of the extended class.