Tuesday, July 15, 2014

puzzling difference between zsh and bash

Accidentally found a little difference between zsh and bash, which seems a bit annoying.

Suppose there is a simple script.

for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done

and the original output of `hadoop classpath`  looks like this:

zsh %> hadoop classpath  

If I run the above in Bourne shell, the results will be like this (which will give the unique list of all jars contained in the specified classpath):

bash-4.1$  for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done

In zsh, however, I still get:

zsh %> for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done        

This was quite unexpected to find out. Now I am wondering how to make it work in zsh.
PS I was curious enough to ask in SO about this.

Saturday, July 12, 2014

How to mock a non-declared checked exception with Mockito

Little useful mocking hack, which costed me quite some time to find:

to throw checked exception from Scala method (which usually don't declare exceptions, which disappoints frameworks like Mockito), use answer:

mockedObject.doSomething(any[Whatever]).answers{ _ => throw new CheckedException("ouch!") } 

where CheckedException is not an instance of RuntimeException. Not because RuntimeException cannot be thrown that way (it can), but because there is a simpler method for those:

mockedObject.doSomething(any[Whatever]) throws new RuntimeException("ouch!")

So if you need to throw e.g. SQLException, method 1 will work, whereas method 2 will compile but complain during the test that such type of exception does not correspond to the signature. For something like ArithmeticException, both methods will work.