-
Notifications
You must be signed in to change notification settings - Fork 38
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
[Mock] Let's discuss mocking #24
Comments
I've been thinking about how to support mocking in Jnario for quite a while know, without coming to a final decision. First thing is that I am not sure yet whether we need an explicit syntax for creating mocks or whether providing an xtend specific wrapper is sufficient. For example, it should be possible to provide something like the Spock mocking syntax based on Xtend. For example: val myMockedList = mock(typeof(List)) => [ val myStub = stub(typeof(Stack)) => [ I think I will start with a library for Xtend and see how far I will get. |
Hello Sebastian, I work currently on a such library: https://github.com/borisbrodski/xtend-jmockit :-) You will find the sand-box version of all extension methods below. I already get most of the features to work. The only problem is the parameter matching. For example:
In the Java those calls can be perfectly differentiated, but with the Xtend not. For this case I provided new "with" extension method, so it looks like this:
If you don't use "any_", you don't need the "with_" methods. The finest thing is, that you can use block (closures) to test parameters (Xtend): mock [ Cool, ne? If you interested, you can help me on this small project. (I think, it would be a little bit better, to offer this kind of functionality also without Jnario.) Regards, PS PPS protected void stub(final Procedure1< Expectations > proc) { { protected void mock(final Procedure1< Expectations > proc) { { protected void returns(Expectations expectations, Object result) throws Exception { protected void setResult(Expectations expectations, Object result) throws Exception { protected void setResult(Expectations expectations, Object firstObject, Object... remainingValues) throws Exception { protected < T > T onInstance(Expectations expectations, T mockedInstance) throws Exception { protected void setTimes(Expectations expectations, int times) throws Exception { protected void setMaxTimes(Expectations expectations, int maxTimes) throws Exception { protected void setMinTimes(Expectations expectations, int minTimes) throws Exception { protected < T > T any(Expectations expectations) throws Exception { protected < T > T any(Expectations expectations, Class< T > t) throws Exception { protected String anyString(Expectations expectations) throws Exception { protected Long anyLong(Expectations expectations) throws Exception { protected Integer anyInt(Expectations expectations) throws Exception { protected Short anyShort(Expectations expectations) throws Exception { protected Byte anyByte(Expectations expectations) throws Exception { protected Boolean anyBoolean(Expectations expectations) throws Exception { protected Character anyChar(Expectations expectations) throws Exception { protected Double anyDouble(Expectations expectations) throws Exception { protected Float anyFloat(Expectations expectations) throws Exception { protected < T > T match(Expectations expectations, Matcher< T > matcher) throws Exception { protected < T > T match(Expectations expectations, Class< T > t, Matcher< T > matcher) throws Exception { protected < T > T match(Expectations expectations, final Function1< T, Boolean > matcherFnc) throws Exception { @OverRide protected < T > T match(Expectations expectations, Class< T > t, final Function1< T, Boolean > matcherFnc) protected int matchInt(Expectations expectations, Matcher< Integer > matcher) throws Exception { protected int matchInt(Expectations expectations, final Function1< Integer, Boolean > matcherFnc) throws Exception { protected long matchLong(Expectations expectations, Matcher< Long > matcher) throws Exception { protected long matchLong(Expectations expectations, final Function1< Long, Boolean > matcherFnc) throws Exception { protected short matchShort(Expectations expectations, Matcher< Short > matcher) throws Exception { protected short matchShort(Expectations expectations, final Function1< Short, Boolean > matcherFnc) protected byte matchByte(Expectations expectations, Matcher< Byte > matcher) throws Exception { protected byte matchByte(Expectations expectations, final Function1< Byte, Boolean > matcherFnc) throws Exception { protected float matchFloat(Expectations expectations, Matcher< Float > matcher) throws Exception { protected float matchFloat(Expectations expectations, final Function1< Float, Boolean > matcherFnc) protected double matchDouble(Expectations expectations, Matcher< Double > matcher) throws Exception { protected double matchDouble(Expectations expectations, final Function1< Double, Boolean > matcherFnc) protected boolean matchBoolean(Expectations expectations, Matcher< Boolean > matcher) throws Exception { protected boolean matchBoolean(Expectations expectations, final Function1< Boolean, Boolean > matcherFnc) protected char matchCharacter(Expectations expectations, Matcher< Character > matcher) throws Exception { protected char matchCharacter(Expectations expectations, final Function1< Character, Boolean > matcherFnc) protected < T > T with(Expectations expectations, T t) throws Exception { protected int with(Expectations expectations, int i) throws Exception { protected long with(Expectations expectations, long i) throws Exception { protected short with(Expectations expectations, short i) throws Exception { protected byte with(Expectations expectations, byte i) throws Exception { protected float with(Expectations expectations, float i) throws Exception { protected double with(Expectations expectations, double i) throws Exception { protected boolean with(Expectations expectations, boolean i) throws Exception { protected char with(Expectations expectations, char i) throws Exception { private < T > void addExpectationMatcher(Expectations expectations, Object matcher) throws Exception { private RecordPhase getCurrectExpectationsPhase(Expectations expectations) throws Exception { |
That looks nice, I really like the syntax how you create mocks. The only thing I don't like too much is the way how you declare returned values. I find this quite unintuitive (although I know that this is how JMockit handles it). I would prefer something like: mock[ But this should be possible by providing a subclass of Expectations, which overrides the '=>' operator, shouldn't it? |
Cool idea. I will try it out. PS
|
In case of mocking and assertion I would say it is no problem, as the semantic are basically the same. You express what the result of this expression is. The redundancy with the "with" semantics is unfortunate, the problem was that this was introduced after I create the Jnario assertions. On Friday, 28. September 2012 at 14:21, Boris Brodski wrote:
|
I think, this not doing to function. The problem is, that the context mock [ is not a context extending Expectations but receiving the instance of the Expectations implementation as an "it" parameter. This means, that the "assert" meaning of the operator => get linked. Do you have an idea, how to overcome this? |
Shouldn't something like public static void mock(final Procedure1< ExpectationsWithArrow > proc) {
new ExpectationsWithArrow() {
{
proc.apply(this);
}
};
}
public class ExpectationsWithArrow extends Expectations{
public <T> void operator_doubleArrow(T methodCall, T result){
...
}
}
```java
do the trick? |
Unfortunately not. The Xtend mock [ translates by the Xtend to a something like Procedure function1 = new Procedure() {
} This means, that the "service.call" line is not in the context of the ExpectationsWithArrow class. By the way, this is the reason, way the xtend-jmockit extension library is needed in the first place. If we could force Xtend to extend this anonymous block from the Expectations or any other class (and not an interface), the JMockit library would work with Xtend nearly out of the box. Xtend [ ] block works with any interface with a single method to implement (like Comparator) but not with any abstract class with a single method to implement :-( |
Hello Sebastian, please, take a look of the dynamic results with JMockit: https://github.com/borisbrodski/jmockit-xtend#DynamicReturnValue I think, it looks good. Only constraint: max. 6 parameters. |
Looks really good. The constraint is not a problem in my opinion. You should use your jnario specs to generate the documentation ;-). Have a look at: for an example. |
Hello Sebastian, I wrote a patch for the jmockit, so the JUnit rules get supported as well. Using it and some other workarounds I managed to marry JMockit + Jnario + JUnit rules. You can take a look of this discussion (about the JMockit patch) https://groups.google.com/forum/?fromgroups=#!topic/jmockit-users/dD2YsZkzRoI As mentioned there, I had to write a small workaround in order to get the instance of the test class, created by the Jnario. In Jnario in ExampleRunner you override Another question is, would you like to declare JMockit the official mocking library for Jnario in the future? |
Hi Boris, could you please create a pull request for the change. Thanks. Sebastian |
I'm not sure, I understand the purpose of
But object instantiation should be done using
Is it save to just call
|
I could try it out and see, if the tests stay green, in case you don't have time for this. |
I am delegating the call so that you can use a different implementation for creating spec instances, e.g. Guice. |
Do you plan to support one mocking library officially? Could it be JMockit? If so, could you please take a look at https://groups.google.com/forum/?fromgroups=#!topic/jmockit-users/dD2YsZkzRoI The plans to make JMockit official Jnario mocking library could help adding JUnit rules support to the JMockit. Are you interested in all this? PS |
Hi On Friday, 19. April 2013 at 10:53, Boris Brodski wrote:
I don't know yet. There are many mocking libraries out there each with different advantages and disadvantages. This is why I have a hard time deciding on an official library. Also, I would like to avoid adding a specific mocking library to jnario's dependencies.
Hm. I don't really see the need for specifying expectations in rules either. For me they are an essential part of a test's "documentation". I need to be able to read the expectations in order to understand the behavior that is tested. |
I (partially) agree on expectations, but the mocks are very important. For example, I use UnitTestRule, where I stub out all the hibernate and EJB stuff, than I have PersistentTestRule that connects to the real database but mocks out EJB stuff (all test run locally testing HQL queries against real DB). The point is: specifying a rule, you put your test in some special environment. This environment can be created with mocks and stubs. You don't always need to see exactly, how mocks and stubs are configured, as long the behave consistently emulating some test environment. |
I think it is already possible to integrate any mocking framework using a combination of SpecCreator, extension methods and Junit rules. So I don't really see a need to add special support to Jnario. There are two problems with an "official" mocking library:
|
I agree with you. I think Jnario could provide integration for multiple mocking libraries (in form of separate projects, like https://github.com/borisbrodski/jmockit-xtend). A list of such projects with URLs and invitation to add support for more libraries could be enough. |
When doubleclicking a word inside a richstring the whole richstring was selected by the editor. After binding the correct ITokenTypeToPartioningTypeMapper and an appropriate DoubleClickStrategy Richstring-Tokens are handled by finding selecting the word under the courser, instead of selecting the whole token.
…kePartitioning fix sebastianbenz#24 TokenTypePartitioning for richstrings
Hello Sebastian,
I read about your wish to discuss mocking support within Jnario/Xteand. This is exactly one of my tasks at the moment :-)
I evaluated all available mock libraries. At the mean time the Jmockit is in my option the best one. The core features:
How do you plan to integrate the mock API? I would suggest to override some of the Xtend Nonterminals, like BlockExpression (or something like this) to introduce some new keywords ('mock', 'stub', ...).
What do you think?
The text was updated successfully, but these errors were encountered: