0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SpringBoot】lazy-initializationが有効な環境で全てのBeanが正常に初期化されるかテストする

Posted at

やること

Beanを遅延初期化することで起動を高速化するlazy-initializationオプションですが、SpringBootTestの高速化にも有効です。
一方、ただlazy-initializationを有効にするだけではテストで初期化されないBeanが出てしまい、最悪デプロイしてみて初めて初期化エラーに気付くことになります。

そこで、全てのBeanが正常に初期化されるかだけを確認するテストを作ります。

やり方

  • @SpringBootTest(properties = ["spring.main.lazy-initialization=false"])を特定のテストクラスに付ければできる
  • Spring Initializrで追加される...ApplicationTestsに付与するのが良さげに思える
サンプルコード(KotlinですがJavaでもそう変わりません)
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest(properties = ["spring.main.lazy-initialization=false"])
class FooApplicationTests {

    @Test
    fun contextLoads() {
    }
}

解説

やっていることは別記事でまとめています。
また、この記事ではSpringBootTestの引数を設定する以外の方法についても触れています。

付与先を...ApplicationTestsにしているのは、単に1アプリケーションに1つ存在するテストだからで、特に深い理由は有りません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?