1
    Class  A{

    B objB = new B();
    objB.someBMethod();

    }

    Class B{

    public void someBMethof(){

    C objC = new C();

    }
    }

    class C{
    int a=1;
    public C(){}
    public C(int v){
    a=v;
    }
    }

@RunWith( PoswerMockRunner.class )
@PrepareForTest({ A.class, B.class, C.class})
Class TestApp{

    @Mock
    C mockC;

    PowerMockito.whenNew( C.class ).withNoArguments().thenReturn(mockC);

}

The above code captures what im trying to do. But the whenNew() does not seem to be working and when i try debuggin the C object created is not the mock. Dont know whats happening. Some pointers would be much appreciated. thanks

broun
  • 2,483
  • 5
  • 40
  • 55

1 Answers1

5

You provides some code, so thanks. But the next time, consider to post an sscce (Correct (Compilable) example).

I tried (and fix your code) and it works. This is my version of your code :

public class A {

    public int someAMethod() {
        B objB = new B();
        return objB.someBMethod();
    }
}

public class B {

    public int someBMethod() {
        C objC = new C();
        return objC.getA();
    }
}

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class, C.class })
public class TestApp {

    @Mock
    C mockC;

    @Test
    public void shoudlReturnTheCValue() throws Exception {
        when(mockC.getA()).thenReturn(666);
        PowerMockito.whenNew(C.class).withNoArguments().thenReturn(mockC);
        assertEquals(666, new A().someAMethod());
    }
}

I have configured a maven project with the following dependencies :

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>
gontard
  • 28,720
  • 11
  • 94
  • 117
  • Okay, I will look further where im doing different. thanks for the response and sure will post compilable code next time. And one question is i see some times the when() / whenNew() similar things failing to do the stubbing if the arguments passed in are done through matchers and not the exact values that get passed during the actual runtime. Can you tell me in what situatin actual values are expected and when mactchers can be used? i could get the pattern when this happens ? – broun Mar 14 '13 at 02:00
  • You found the problem ? Sorry, i don't understand the others questions. May be you should create a new question to get the help of more persons. – gontard Mar 14 '13 at 15:57