Set negative expectations in EasyMock - easymock

I would like to understand EasyMock better, and working with it I came up with this question.
What I would like to do is setting up a negative expectation over an object, to check if a certain method is not called during the test (when verifying those initial expectations).
I know that the default behaviour of verify is checking both cases: your expectations were met, and no other calls were performed; but there is no record in the test that a certain method is not called, in other words, if you set an expectation over this method and it doesn't get called, your test will fail (confirming that your implementation is behaving properly!).
Is there a way using EasyMock to set this up? I couldn't find anything in the documentation.
Thank you for your attention, and in advance for your help!

The way EasyMock works is like this:
create a Mock Object for the interface you would like to simulate,
record the expected behavior, and
switch the Mock Object to replay state.
Like in following if you don't set any expectation:
mock = createMock(YourInterface.class); // 1
// 2 (we do not expect anything)
replay(mock); // 3
then it means that if ClassUnderTest call any of the interface's methods, the Mock Object will throw an AssertionError like this:
java.lang.AssertionError:
Unexpected method call yourMethodWhichYouDidNotExpectToBeCalled():
This itself is Negative Expectation checking.

Related

CakePHP4: Convert or cast Cake\ORM\Entity to actual entity for type hinting

Let's say I have a method that is expecting an Invoice entity as a parameter. So I add the type hint Invoice like so:
public function doThatThing (Invoice $invoiceEntity)
{
// ... operate on $invoiceEntity
}
Unfortunately, when I pass the results of InvoiceTable->get(123), I get the error "TypeError: Argument 1 passed to doThatThing must be an instance of App\Model\Entity\Invoice, instance of Cake\ORM\Entity given..." in my unit tests.
Is there a good way to cast or convert the generic ORM results of ->get() to the specific Entity type that I know it must be?
#greg-schmidt got me looking in the right direction. I'm afraid my question only mentioned the key clue in passing when I said "in my unit tests."
As it turns out, my unit test has a fake invoice table that extends InvoiceTable. My fake does call setTable and setAlias so that it looks like the real table.
But when I call get() on the fake, the call chain bypasses InvoiceTable, lands in ORM/Table, and eventually calls into ORM/Query. When ORM/Table calls the constructor on ORM/Query, it passes $this, which is an instance of my fake, and not of the real, underlying InvoiceTable (setTable and setAlias have no effect at this point). And as #greg-schmidt guessed,
InvoiceTable is almost certainly not your invoice table implementation
My solution will be to have my fake invoice implement its own get() method which will return the response from a real InvoiceTable.

What is the purpose of next('r') in the context of an RxJS Subject

I'm still fairly new to the RxJS world (please pardon my semantics), but I've seen a few examples of code that creates a Subject to do some work, and then calls next(0), or next('r') on the subscription. It appears to re-run the stream, or rather fetch the next value from the stream.
However, when I tried using this to call an API for some data, it completely skips over the work it's supposed to do as defined in the stream (assuming it would "run" the stream again and get new data from the server), and instead my subscriber gets the 'r' or zero value back when I try to call next like that.
I get that making the subscription "starts execution of the stream", so to speak, but if I want to "re-run" it, I have to unsubscribe, and resubscribe each time.
Is it a convention of some kind to call next with a seemingly redundant value? Am I just using it in the wrong way, or is there a good use-case for calling next like that? I'm sure there's something fundamental that I'm missing, or my understanding of how this works is very wrong.
It's a good question, I definitely recommend you to read about hot and cold Observables.
cold Observables execute each time someone subscribes to it.
const a$ = of(5).pipe(tap(console.log))
a$.subscribe(); // the 'tap' will be executed here
a$.subscribe(); // and here, again.
hot Observables do not care about subscriptions in terms of execution:
const a$ = of(5).pipe(
tap(console.log),
shareReplay(1)
);
a$.subscribe(); // the 'tap' will be executed here
a$.subscribe(); // but not here! console.logs only once
In your example you are using Subject that represents cold Observable.
You can try to use BehaviorSubject or ReplaySubject - both of them are hot but be aware that they behave differently.
IN you example you can modify your Subject like the following:
const mySubject = new Subject();
const myStream$ = mySubject.pipe(
shareReplay(1)
);
myStream$.subscribe(x => console.log(x))
mySubject.next(1);
mySubject.next(2);
mySubject.next(3);

How to assert an actual value with two different expected values

I'm writing a protractor test which asserts a URL against two different browser URLs.
I tried using toMatch with a regular expression but I get an error.
Is it possible to assert an actual string value against 2 or more expected string values and see if it is equal to any of them?
expect(url1).toMatch(/this.url|www.google.com/);
Sounds like you want to use toContain() which works for finding an item in an array. So switch your assertion around because you want to pass it an array of URLs [this.url, 'www.google.com'] and assert that it will contain url1.
expect([this.url, 'www.google.com']).toContain(url1);
Note: While this should work, personally I don't like to have my values on the receiving end of the assertion i.e. toContain(url1), I would rather that be passed first expect(url1).... However, in the end it's still asserting actual vs expected values so not a huge deal in my opinion.
https://jasmine.github.io/2.0/introduction.html

In which cases does App Engine search raise a TransientError?

I've seen the Index.search method raise a search.TransientError, and I know other methods may raise such errors as well, but in the documentation, I'm seeing code like this:
# Index the document.
try:
index.put(doc)
except search.PutError, e:
result = e.results[0]
if result.code == search.OperationResult.TRANSIENT_ERROR:
# possibly retry indexing result.object_id
except search.Error, e:
# possibly log the failure
When are checks like this necessary, and in which cases can I rely on search.TransientErrorbeing raised?
Looking through the source code, it looks like some (if not all) methods of the Index class use the _MakeSyncSearchServiceCall function, which, if anything goes wrong, calls _ToSearchError passing in an exception. This error returns an element from the _ERROR_MAP dictionary, based on the application_error property of the error. This dictionary contains theTransientError`, so it is clear that it is raised in certain cases.
So, when is it necessary to check result.code, and what other errors could be raised in cases that are out of my control?

What does this error actually mean?

So I have seen this error show up, just sometimes, but it is not helpful in describing the actual error which has occured. Nor does it give any clues as to what might cause it to display.
Cannot use modParams with indexes that do not exist.
Can anyone explain more verbosly what this error means, what it relates to (such as a behaviour, component, controller, etc), the most common causes and how to fix it?
To kickstart the investigation, you can find the error here.
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/ObjectCollection.php#L128
Layman's Terms
CakePHP is being told to apply an array of parameters to a collection of objects, such that each particular object can modify the parameters sent on to the next object. There is an error in how CakePHP is being told to do this.
In Depth
Generically, this rises from the CakePHP event publication mechanism. Somewhere in your code is an instance of ObjectCollection, which is being triggered with certain parameters. That is, a method is being called on every object in that collection.
Each callback method is given parameters. Originally the parameters are passed into trigger(). In normal cases (where modParams is false), every callback gets the same parameters. But when modParams is not strictly false, the result of each callback overwrites the parameter indicated by modParams.
So if there are two objects in the collection, modParams is 1, and the params[1] is 'a' initially, then the callback is given the first object with params[1] == a. That callback returns 'b', so when the next callback is called, the second object gets params[1] == b.
The exception raises when the modParams value given does not exist in the originally given params. Eg, if modParams is 2 and params is array (0 => 'a', 1 => 'b'), you'll get this exception.
In your case
Specifically, debugging this has to be done at a low-level because it's a method on a generic class. The backtrace from the Exception should get you bubbled up to a trigger() call on a particular concrete class. That call is being given non-false modParams and a params that doesn't have the given modParams. It could be a code bug in a concrete class extending ObjectCollection, or it could simply be a generic message arising from a method not being given expected arguments.
Have you tried reading the documentation?
/*
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
* Setting modParams to an integer value will allow you to modify the parameter with that index.
* Any non-null value will modify the parameter index indicated.
* Defaults to false.
*/
You did not paste any code, so I guess your 3rd arg of the method contains something wrong.

Resources