Spring Boot はすごく簡単に始められそう。Spock もいけそうかなぁ?おとなしくJavaにしておくか悩ましい。

Spring Tool Suite

年も明けたしSpring Bootやるかーって思って。まずはSTSから始めてみることにした。STS 3.7.2。

ダウンロードして起動。久々のSTSだなー。使いやすくなってるんだろうなぁwktk

f:id:bufferings:20160103100405p:plain

Spring Starter Project + Groovy

よし。File > New > Spring Starter Projectね。

f:id:bufferings:20160103100424p:plain

お。便利そう。・・・JavaとGroovyが選べる。ゴクリ・・・。Groovy選んでみようかな。mainはJavaで書いて、testにSpock使えたら嬉しいから。

f:id:bufferings:20160103100443p:plain

で、Webを選んで作成!あぁ・・・pomにエラー出た。もしかして、あれか。これは、しまった感じか・・・。Groovyチカラのない僕には・・・。

f:id:bufferings:20160103100455p:plain

Multiple annotations found at this line:
- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (execution:
default-testCompile, phase: test-compile)
- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (execution: default-
compile, phase: compile)

Plugin execution not covered by lifecycle configuration: org.codehaus.groovy:groovy-eclipse-compiler:2.9.1-01:add-groovy-build-paths
(execution: default-add-groovy-build-paths, phase: initialize)

Spring Starter Project

おとなしくGroovy使わないようにすればいいんだけど、でも悔しいので原因を解決することにしました。

ということで、まずはGroovyじゃなくて普通にJavaで作ってみることにした。やっぱり。pomのエラー出ないや。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo2Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo2Application.class, args);
	}
}

動くよねぇ・・・。

f:id:bufferings:20160103100542p:plain

コントローラたしてみよっと。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Demo2Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo2Application.class, args);
	}
}

@RestController
class ExampleController {

	@RequestMapping("/")
	public String hello() {
		return "Hello World!";
	}

}

動くよねぇ・・・。

f:id:bufferings:20160103100558p:plain

一瞬だなー。Spring Boot便利だなー。

テストも、こんな風にしてあっさりうごくねぇ。

package com.example;

import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Demo2Application.class)
@WebIntegrationTest
public class Demo2ApplicationTests {

	private RestTemplate template = new TestRestTemplate();

	@Test
	public void rootAccess_WillReturnHelloWorld() {
		String actual = template.getForEntity("http://localhost:8080", String.class).getBody();
		Assert.assertThat(actual, Is.is("Hello World!"));
	}

}

Groovy Eclipse Plugin

てことで、もういっかいGroovyに挑戦。しばらくぐぐった後、Groovy Eclipseプラグインを入れなきゃなんじゃないの?ってなって。入れてみることにした。

Home · groovy/groovy-eclipse Wiki · GitHub

STS のベースの Eclipse が 4.5.1 なので↓の更新サイトからインストール

http://dist.springsource.org/snapshot/GRECLIPSE/e4.5/

必要そうなのをチェック

f:id:bufferings:20160103100651p:plain

インストールして、STS再起動して、プロジェクトのMaven Updateしたら、pomのエラー消えたー。

で、動いたー。(∩´∀`)∩ワーイ

package com.example

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
class DemoApplication {

	static void main(String[] args) {
		SpringApplication.run DemoApplication, args
	}
}

@RestController
class ExampleController {

	@RequestMapping("/")
	def hello() {
		"Hello Groovy World!"
	}
}

f:id:bufferings:20160103100741p:plain

と思ったら

プロジェクトにエラー出とった。(´;ω;`)ブワッ

f:id:bufferings:20160103102603p:plain

Groovy: compiler mismatch Project level is: 2.4 Workspace level is 2.3

これはあれか。あれやな。Groovy-Eclipseの更新サイトから、2.4を落として、

f:id:bufferings:20160103102847p:plain

んで、それを有効にする。

f:id:bufferings:20160103103138p:plain

と。エラー消えた。(∩´∀`)∩ワーイ

Groovy-Eclipseの今後が気になる

Spring Tool Suite 3.7.2 - New and Noteworthy

のGGTSのところに、こんな風に書いてて、あぁそういえばそうだったなーって思いつつ。

GGTS

Since the end of March Pivotal is no longer sponsoring the Groovy and Grails projects (as described here). Therefore we don't ship new versions of the GGTS bundle anymore. Groovy-Eclipse remains on the dashboard and was fully updated for Eclipse 4.5. It was *not* updated for Eclipse 4.5.1 so beware that if you install Groovy-Eclipse you will be downgrading your Java compiler and missing all the JDT related fixes that went into 4.5.1.

  • GGTSの新しいバージョンはもう出さないよ。
  • Groovy-EclipseはEclipse4.5まではサポートしてるけど、4.5.1用の更新はしないよ。

ってこと?なのかなー?

Reference:
Groovy 2.4 And Grails 3.0 To Be Last Major Releases Under Pivotal Sponsorship | Pivotal P.O.V.

Spock

ま、それはそうと、Groovy使いたいのってSpock使いたいからで、やってみよう。・・・ん?

f:id:bufferings:20160103103918p:plain

Duplicating managed version 1.0-groovy-2.4 for spock-core

あー。bootに定義してあるってことかな。じゃ、バージョン外したらいっか。

<dependency>
  <groupId>org.spockframework</groupId>
  <artifactId>spock-core</artifactId>
  <scope>test</scope>
</dependency>

これでいけそう。ちなみに、bootの方見てみたらこんな風に定義してあったや。

<dependency>
  <groupId>org.spockframework</groupId>
  <artifactId>spock-core</artifactId>
  <version>${spock.version}</version>
  <exclusions>
    <exclusion>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.spockframework</groupId>
  <artifactId>spock-spring</artifactId>
  <version>${spock.version}</version>
</dependency>

spock-springか。これも入れたら良さそうな名前やな。入れとこ。

40. Testing にも、こんな風に書いてるからね。

If you wish to use Spock to test a Spring Boot application you should add a dependency on Spock’s spock-spring module to your application’s build. spock-spring integrates Spring’s test framework into Spock.

@WebIntegrationTest使うのに、必要なのかな。

SpockのSpec

書いてみる。

package com.example

import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.TestRestTemplate
import org.springframework.boot.test.WebIntegrationTest
import org.springframework.web.client.RestTemplate

import spock.lang.Specification

@SpringApplicationConfiguration(classes = DemoApplication)
@WebIntegrationTest
class DemoApplicationTests extends Specification {

	RestTemplate template = new TestRestTemplate();
	
	def "Root access will return HelloGroovyWorld!"() {
		when:
		def body = template.getForEntity("http://localhost:8080", String).body

		then:
		body == "Hello Groovy World!"
	}
}

動いたー。

Groovyはテストだけに使いたいので

パッケージをごにょって。ぎっはぶにあげといた。

bufferings/SpringBootWebWithSpock · GitHub

おしまい

Groovyを選んでなかったら10分くらいで全部終わってたのに、結局2,3時間かかったや・・・。(ヽ´ω`)

ま、面白かった。終わり。