1
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 5 years have passed since last update.

【spock】MessageSourceをmock化_message.propertiesからの外部取得はどう対応する?

Posted at

下記実装はMessageSourceをコンストラクタインジェクションしてる。よくみるやつ
message.propertiesとかでメッセージを一括管理できるから便利

Controller
//省略
messageSource.getMessage("message",null,Locale.JAPAN)
//省略

spock側ではこう書く

ServiceImplTest

    @RunWith(Sputnik.class)
class ServiceImplTest extends Specification {

    SampleController target
    MessageSource messageSource

    def setup() {
        messageSource = Mockito.mock(MessageSource.class)
        target = new SampleController(messageSource)
    }

    @Unroll
    def "MessageSource"() {
        setup:
        Mockito.when(messageSource.getMessage("message",null,Locale.JAPAN)).thenReturn("dummy-message")

        when:
        //省略

        then:
        //省略

        where:
        //省略
    }

おわり

上記のように記載してあげればよしなにしてくれますー
日本語のドキュメントがほとんど存在しなかったから苦労した

1
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
1
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?