jeudi 31 mars 2016

Mockito Mock run Autowired class Object

I have below class scenario. While testing MyTestableClass, I wish to process Autowired class. I would like to mock only variable in AutoWired class.

sample class is as below-

public class MyTestableClass {

    @Autowired
    private MyServiceClass service;

    public void handleError(){
    ...
    service.doSomething();

    }
}

public class MyServiceClass {

    @Autowired
    private JMSChannel channel;

    public void doSomething(){
        System.out.println("Inside Service class");
        .....
        channel.isAvailable();
        .....   
    }
}

@RunWith(MockitoJUnitRunner.class)
public class  MyTestableClassTest {
    private MyTestableClass  testClass= new MyTestableClass();

    private JMSChannel channel;

    @Before
    public void init(){
     channel= mock(JMSChannel.class);
     when(channel.isAvailable()).thenReturn(Boolean.TRUE);  
    }

    @Test
    public void test(){
        testClass.handleError();
    }
}

For example, Console should give me "Inside Service class" before returning true.

Thanks in Advance !

Aucun commentaire:

Enregistrer un commentaire