Skip to content

Instantly share code, notes, and snippets.

@groundwater
Created April 20, 2012 09:32
Show Gist options
  • Save groundwater/2427340 to your computer and use it in GitHub Desktop.
Save groundwater/2427340 to your computer and use it in GitHub Desktop.
HBase Coprocessor Test with Mocked HTableInterface and CoprocessorEnvironment
import org.specs2.mutable._
import org.specs2.mock._
import ca.underflow.MyApp
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.CoprocessorEnvironment
import org.apache.hadoop.hbase.client.{ HTableInterface, Put }
class HelloWorldSpec extends Specification with Mockito {
val row = Bytes.toBytes("myrow")
"My App" should {
"put row" in {
val env = mock[CoprocessorEnvironment]
val htb = mock[HTableInterface]
// This will be set by the callback on our table
var put_called = false
env.getTable(tbl) returns htb
// Provide an anonymous function to answer
// HTableInterface.put method calls
htb.put(any[Put]) answers { _p ⇒ // Type java.lang.Object
val put = _p.asInstanceOf[Put]
// Assert the correct row is being put
put.getRow() must beEqualTo(row)
put_called = true
}
// We need some way to inject our mock environment
val app = MyApp(env)
// Assume our app will call HTableInterface
// to insert a new row at "myrow"
app.insertRow("myrow")
// the above callback is executed synchronously
// so this value should be true if the HTableInterface
// provided has its put method called
put_called must beTrue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment