Edited at

SidenでHello Worldとテスト

More than 3 years have passed since last update.


Siden(紫電かな?)

これ凄い。

http://qiita.com/taichi@github/items/680e7beb6e2cc97e0895

ってことでやってみた。


mainメソッド


Application.java


import ninja.siden.App;

public class Application {
public static void main(String[] args) {

App app = new App();

new HelloResource(app).defineRoute();

app.listen();
}
}



Resouceクラス


HelloResource.java

import ninja.siden.App;

public class HelloResource {

private App app;

public HelloResource(App app){
this.app = app;
}

public void defineRoute(){
app.get("/hello", (req,res)->"Hello World");
}
}



テスト


HelloResourceSpec.groovy

import groovyx.net.http.RESTClient

import ninja.siden.App
import ninja.siden.App.Stoppable
import spock.lang.Shared;
import spock.lang.Specification

class HelloResourceSpec extends Specification {

@Shared stop

def setupSpec(){
def app = new App()
new HelloResource(app).defineRoute()
stop = app.listen()
}

def "簡単な起動のテスト"(){
setup:
def endpoint = new RESTClient( 'http://localhost:8080/' )
when:
def resp = endpoint.get([ path: 'hello'])
then:
with(resp) {
status == 200
data.text == "Hello World"
}
}

def cleanupSpec(){
stop.stop()
}
}



ビルドスクリプト


build.gradle

apply plugin: 'java'

apply plugin: 'groovy'

sourceCompatibility = 1.8
version = '1.0'

repositories {
mavenCentral()
jcenter()
}

dependencies {
compile 'ninja.siden:siden-core:0.2.0'
testCompile 'org.codehaus.groovy:groovy-all:2.3.8'
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7+'
}


次は、RestサービスとDBアクセス書く。