Skip to content

Instantly share code, notes, and snippets.

View chbatey's full-sized avatar
🐯
WFH

Christopher Batey chbatey

🐯
WFH
View GitHub Profile
@chbatey
chbatey / ClassThatReturnsFutures.scala
Created February 6, 2014 22:34
Scala: Example class that returns a future
class ClassThatReturnsFutures() {
def goDoSomething() : Future[String] = {
future {
println("I best do this asynchronously")
"The result"
}
}
}
@chbatey
chbatey / ClassThatReturnsFuturesTest.scala
Created February 6, 2014 22:36
Scala: Not a good way to test asynchronous code
test("Test a asynchronous method synchronously") {
val underTest = new ClassThatReturnsFutures()
val futureResult = underTest.goDoSomething()
futureResult onComplete {
case Success(value) => value should equal("Something")
case Failure(exp) => fail(exp)
}
}
@chbatey
chbatey / ClassThatReturnsFuturesTest.scala
Created February 6, 2014 22:40
ScalaTest: Using whenReady to test Futures
test("Test a asynchronous method synchronously - whenReady") {
val underTest = new ClassThatReturnsFutures()
val futureResult = underTest.goDoSomething()
whenReady(futureResult) { result =>
result should equal("The result")
}
}
@chbatey
chbatey / ClassThatReturnsFuturesTest.scala
Created February 6, 2014 22:41
ScalaTest: Testing futures with future value
test("Test a asynchronous method synchronously - futureResult") {
val underTest = new ClassThatReturnsFutures()
val futureResult = underTest.goDoSomething()
futureResult.futureValue should equal("The result")
}
@chbatey
chbatey / ChildActor.scala
Created February 11, 2014 02:39
Akka: Example actor that sends a message to its parent
class ChildActor extends Actor {
def receive: Actor.Receive = {
case "Do something" => {
// Do something important
context.parent ! "I was told to do something and I did it"
}
}
}
@chbatey
chbatey / ChildActorTest.scala
Created February 11, 2014 02:40
Akka: Testing a message sent to an actor's parent
class ChildActorTest extends TestKit(ActorSystem("ChildActorTest")) with FunSuiteLike with ShouldMatchers {
test("Should inform parent when told to do something") {
val parent = TestProbe()
val underTest = TestActorRef(Props[ChildActor], parent.ref, "ChildActor")
underTest ! "Do something"
parent.expectMsg("I was told to do something and I did it")
}
}
@chbatey
chbatey / ExampleWebAppController.java
Created February 13, 2014 21:40
Spring boot: Super simple example app
@Controller
public class ExampleWebAppController {
@RequestMapping("/exampleendpoint")
public @ResponseBody Payload exampleEndpoint(@RequestParam(value="input") String input) {
return new Payload("Something really important: " + input);
}
}
@chbatey
chbatey / ExampleWebAppSpecification.groovy
Created February 14, 2014 02:25
Spock: Testing a web application
class ExampleWebAppSpecification extends Specification {
def "Should return 200 & a message with the input appended"() {
setup:
def primerEndpoint = new RESTClient( 'http://localhost:8080/' )
when:
def resp = primerEndpoint.get([ path: 'exampleendpoint', query : [ input : 'Get a hair cut' ]])
then:
with(resp) {
status == 200
contentType == "application/json"
@chbatey
chbatey / build.gradle
Created February 14, 2014 02:34
Gradle: Plugins for spring boot and groovy
apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'groovy'
@chbatey
chbatey / build.gradle
Created February 14, 2014 02:37
Spock: Gradle dependencies
compile("org.codehaus.groovy:groovy-all:2.2.0")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile("org.spockframework:spock-core:0.7-groovy-2.0")
testCompile("org.codehaus.groovy.modules.http-builder:http-builder:0.7+")
testCompile("net.sf.json-lib:json-lib:2.4+")