PowerMock(PowerMockito)とは

mockitoではstaticメソッドやprivateメソッドはmock化することができませんが、powermockを使うことにより、staticメソッド、privateメソッドをmockにすることができます。

使い方(maven)

  • pom.xmlに以下を追加
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>1.5.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>1.5.1</version>
                <scope>test</scope>
            </dependency>
  • テストクラスにアノテーションを追加
    • @RunWith(PowerMockRunner.class)
    • @PrepareForTest(PasswordUtil.class)(モックにするクラスを記載する。複数可)
    • @PowerMockIgnore({"javax.crypto.*"}(※cipherがおかしくなるので、cipherクラスを使う場合はこのアノテーションも追加)
  • staticメソッドをmockにする
PowerMockito.mockStatic(PasswordUtil.class);

このページへのコメント

ubBJi1 Great, thanks for sharing this blog.Really looking forward to read more. Awesome.

0
Posted by awesome things! 2014年01月20日(月) 14:28:33 返信

cujEuH I cannot thank you enough for the blog article.Really thank you! Much obliged.

0
Posted by awesome things! 2014年01月01日(水) 01:23:02 返信

W2uUSN Awesome article post.Much thanks again. Keep writing.

0
Posted by watch for this 2013年12月19日(木) 10:31:25 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます