Comments (2)
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.
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
- In Smart GWT, each component is assigned an ID via
SC.generateID()
- GwtMockito's
StubGenerator.invoke()
will make this method return an empty string. - This causes an exception in smart GWT's
IDManager.validateID()
, because empty String does not match the RegEx.
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)
- getting GwtMockitoTestRunner$FailedCastException in DateBox HOT 1
- ClassCastException with JsArrayInteger HOT 1
- Since Mockito 2.1.0 TextBox.setValue(null) does not work anymore HOT 4
- ToggleButton ClassCastException
- What does it mean "Illegal class modifiers" ? HOT 1
- JaCoCo support not working HOT 7
- Better GWT unit testing http://google.github.io/gwtmockito hyperLink HOT 2
- Provide an example in the samples or documentation for doing code coverage HOT 1
- #68 broke the build HOT 1
- Java 9 support HOT 2
- gwtmockito and GXT HOT 1
- Java 10 upgrade: java.lang.IllegalStateException HOT 3
- javassist 3.22.0-GA breaks gwtmockito ("class is frozen") HOT 3
- Mockito failure with Java11 HOT 1
- gwtmockito v1.1.9 is not available in public repos HOT 3
- IllegalAccessError when using GwtMockitoTestRunner
- Release 1.1.9 is incomplete in maven repository HOT 1
- ClassCastException When Calling MenuItem.setTitle HOT 2
- Junit5 support HOT 1
- Problem with Java 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gwtmockito.