Code Monkey home page Code Monkey logo

jmockit's Introduction

Mocking a super method invocation by jMockit on Eclipse

In order to do the unit test for my child class, I tried to mock the super's method which is called inside the child's method.
But it seems impossible with Mockito.
I searched somewhere and it it said that jMockit can do what I want.
https://stackoverflow.com/questions/14125774/powermock-mocking-a-super-method-invocation
But some functions of JMockit were deprecated. So, I rewrote it.
This post maybe useful to who wanna use jMockit to mock super's method.

import org.junit.*;
import mockit.*;

abstract class BaseService { // jMockit can mock the super class no matter it is abstract or not
    public int save() {
    	System.out.println("base service save executing...");
        return 2;
    }
    public abstract void blabla();
}

class ChildService extends BaseService {
    public int save() {
        System.out.println("child service save executing...");
        int y = super.save();
        return 1 + y;
    }
	@Override
	public void blabla() {
		// TODO 
	}
}

// Below is the test part
class MockBase extends MockUp<BaseService>{
    @Mock
    public int save() {
        System.out.println("mocked base");
        return 9;
    }
}

public class TestSuperCall {
    @Test
    public void testSave() throws Exception {
        MockBase mockBase = new MockBase();
        ChildService childService = new ChildService();
        Assert.assertEquals(9 + 1, childService.save());
        new Verifications() {{ mockBase.save(); }};
    }
}

Below is how to import jMockit to Eclipse and mocking the super method.

Download jmockit jar file

https://javadoc.io/doc/org.jmockit/jmockit/latest/index.html
For example, in this sample i downloaded jmockit-1.49.jar and put here.
0

Add jmockit library to your project

1
2

Config javaagent for your test where you want to use jmockit

3
4

Run test

Right click on the file containning your jMockit test code > Run As > JUnit Test

jmockit's People

Contributors

liennguyen2912 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.