Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ST-JS Interfaces are not compatible with JsMockito #59

npiguet opened this issue Jan 19, 2015 · 0 comments


Copy link

commented Jan 19, 2015

JsMockito works in a similar way to the usual Java Mockito

var mockedObject = mock(Array);
when(mockedObject).get(1).thenReturn("hello world");

In the mock() method, JsMockito copies all the fields it finds in the constructor it receives as parameter to the prototype of the mock object. This approach is actually completely compatible with ST-JS as long as the constructor object is a Class.

If it is an Interface, then the code above would generate an error like TypeError: get is not a function. This is caused by the fact that ST-JS does not generate any code for interface methods. Therefore, JsMockito's mock() method has nothing to copy to the prototype of the mocked object, and the returned mock object therefore does not contain any of the methods declared in the interface.

ST-JS should generate some placeholder empty functions for interface methods (maybe using Interface.method = stjs.emptyFunction instead of Interface.method = function(){} to save a bit of memory on the browser.

Here is a workaround for this problem: Create an empty implementation of the interface, and use that as the mocked class.

public EmptyThing implements Thing {...}

public TestSomethingElse {
    public void testSomething(){
        Thing mocked = mock(EmptyThing.class); // Don't use Thing.class directly here

@acraciun acraciun closed this in f925ef2 Jan 23, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.