Mockito to return different results on different calls

profile.count(any) returns Future.Nil thenReturn Future.Nil

It's that simple

Mockito to verify variable arguments

Let's say you have a method's signature that looks like this:

def select[A](query: String, params: Any*)(f: ResultSet => A): Future[Seq[A]]

When you would like to verify it with a mix of any and values, you will need to do this:

import org.mockito.Matchers.{eq => meq, _}

there was one(db).select(any[String], meq(Seq(1, 2, 3))(any[ResultSet => Category])

That would be corresponding to the invocation of db.select("Something", 1, 2, 3)(someFunc)

RR stub any instance

Use this:

stub.instance_of(Test).use? { true }

SQL to query ancestors (unknown height but know max height)

Let's say we store a tree in a MySQL table with the columns: id, name, and parent_id

We use left join to get all ancestors of it. For example, we know that the max height is 4, so we join 4 times. Here is the SQL statement:

SELECT
 l4.id AS l4_id, l4.name AS l4_name, l4.parent_id AS l4_parent_id,
 l3.id AS l3_id, l3.name AS l3_name, l3.parent_id AS l3_parent_id,
 l2.id AS l2_id, l2.name AS l2_name, l2.parent_id AS l2_parent_id,
 l1.id AS l1_id, l1.name AS l1_name, l1.parent_id AS l1_parent_id
 FROM `table` AS l4
 LEFT JOIN `table` AS l3 ON l4.parent_id = l3.id
 LEFT JOIN `table` AS l2 ON l3.parent_id = l2.id
 LEFT JOIN `table` AS l1 ON l2.parent_id = l1.id
 WHERE l4.id IN (<some_node_we_want_ancestors>);

'2 >&1' must follow '>test.log'

I was stuck at IO redirection. It didn't seem to work.

At the end, I have found that '2 >&1' must follow '>test.log'...

Variable arguments and Mockito

Let's say you have a method

def test(t: String, a: Any*) { }

Then you invoke it with:

val args = Seq(1, "b")
test("aaa", args:_*)

When you want to verify with Mockito, you have to do this:

there was one(obj).text(eq("aaa"), ArrayBuffer(1, "b"))

Apache Pig CSV loader

It seems we cannot use CSVLoader, but we need to use CSVExcelStorage.

Here is an example:

data = LOAD 'something.csv' 
       USING org.apache.pig.piggybank.storage.CSVExcelStorage()
       AS (user_id: int, country: chararray);

Oh, man, the documentation is so confusing. CSVLoader doesn't have an example; CSVExcelStorage has an example, but it is only for storing.

InvalidUseOfMatchersException

I got InvalidUseOfMatchersException, even I already use a matcher in every argument.

It turns out that the method has 3 arguments; the last one has a default value.

Next time, when you want to stub a method, you need to use a matcher on every argument.

RR mock and stub

stub() is for replacing the method implementation

mock().a_method.with_any_args is for replacing the method implementation AND expecting the method to be called once; You may use mock().twice in order to expect it to be called twice.

update_attributes() and attr_accessible

In ActiveRecord, you cannot invoke update_attributes on an attribute if you don't declare it with attr_accessible.

I was stuck with this problem for about an hour. It was discovered because I printed every possible thing around it....