Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseService {
- public void save() {
- // a lot of code here! I cannot change this code.
- }
- }
- public Childservice extends BaseService {
- public void save(){
- //logic for testing
- super.save();
- //logic for testing
- }
- }
- public class Parent {
- public int save() {
- return 99;
- }
- }
- public class Child extends Parent {
- public int save() {
- int i = super.save();
- return i*2;
- }
- }
- @RunWith(PowerMockRunner.class)
- @PrepareForTest(Parent.class)
- public class ParentTest {
- @Test
- public void testSave() {
- PowerMockito.suppress(PowerMockito.methodsDeclaredIn(Parent.class));
- System.out.println(new Child().save());
- }
- }
- public class Bar {
- public void bar() {
- System.out.println("Bar#bar()");
- }
- public int numBar() {
- System.out.println("Bar#numBar()");
- return 99;
- }
- }
- public class Foo extends Bar {
- @Override
- public void bar() {
- super.bar();
- System.out.println("Foo#bar()");
- }
- @Override
- public int numBar() {
- System.out.println("Foo#numBar() - before-call super.numBar()");
- int result = super.numBar();
- System.out.println("Foo#numBar() - after-call super.numBar();");
- return result;
- }
- }
- import org.junit.Assert;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.Mockito;
- import org.powermock.core.classloader.annotations.PrepareForTest;
- import org.powermock.modules.junit4.PowerMockRunner;
- @RunWith(PowerMockRunner.class)
- @PrepareForTest(Bar.class)
- public class FooTest {
- @Test
- public void testNumBar() throws Exception {
- Foo foo = new Foo();
- Foo fooSpy = Mockito.spy(foo);
- Mockito.doReturn(10).when((Bar) fooSpy).numBar();
- Assert.assertEquals(10, fooSpy.numBar());
- }
- }
Add Comment
Please, Sign In to add comment