Git Product home page Git Product logo

Comments (2)

ekuefler avatar ekuefler commented on May 5, 2024

Yeah, mocking static methods (especially native ones) is always going to be difficult. I don't think this is something that can be done directly by Mockito or GwtMockito - if you want to make such code testable, you'll probably have to restructure your class slightly to take advantage of dependency injection, either manually or through Gin. For example, you could define a class like this:

class MyClass {
  private final Provider<JsDate> dateProvider;
  @UiField Label time;

  @Inject
  MyClass(Provider<JsDate> dateProvider) {
    this.dateProvider = dateProvider;
  }

  void updateTime() {
    time.setText("The time is " + dateProvider.get().getTime());
  }

With a provider method in a Gin module defined like this:

@Provide
JsDate provideJsDate() {
  return JsDate.create();
}

Then you could test your class like this:

@RunWith(GwtMockitoTestRunner.class)
public class MyClassTest {

  @Mock JsDate date;

  @Test
  public void testUpdateTime() {
    MyClass myClass = new MyClass(Providers.of(date));
    when(date.getTime()).thenReturn(1, 2);

    myClass.updateTime();
    myClass.updateTime();

    verify(myClass.time.setText("The time is 1"));
    verify(myClass.time.setText("The time is 2"));
  }
}

Would that work for you?

from gwtmockito.

schnatterer avatar schnatterer commented on May 5, 2024

I experienced a similar issue with static methods in a smart GWT client, where I just couldn't see how to wrap the static method. So I build an (ugly?) extension for StubGenerator that helped me work around the issue.

Do you see an alternative to this approach, or would you extend GwtMockito with this functionality?

The problem is as follows

My workaround looks like this

public class StubGenerator {

    // ...

    /**
     * Allows to stub methods, even static ones. Use only if really necessary!
     * Try to use {@link Mockito#mock(Class)} instead!
     *
     * @param clazz the class to stub
     * @param method the method of <code>clazz</code> to stub
     * @param returnValue the value to be returned by <code>clazz.method</code>
     */
    public static void addStubbedMethod(Class<?> clazz, String method, final Object returnValue) {
        STUB_METHODS.put(new ClassAndMethod(clazz, method), new StubMethod() {
            @Override public Object invoke() {
                return returnValue;
            }
        });
    }

    // ...
}

In the test method calling

StubGenerator.addStubbedMethod(SC.class, "generateID", "a" + String.valueOf(System.currentTimeMillis()));

avoids the exception mentioned above.

from gwtmockito.

Related Issues (20)

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.