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.

@VisibleForTesting

Posted at

@VisibleForTestingアノテーション

Javaのコードでテスト目的で使用される非公開メンバー(フィールドやメソッド)を指定するためのアノテーション

非公開メンバーはテストコード内からはアクセスできないようにする
→外部からの誤使用を防げる

テストコード内からはアクセスする必要がある場合がある
@VisibleForTestingアノテーションを使用して非公開メンバーをテストコードに対して可視化できる

@VisibleForTestingアノテーション使用例

public class MyClass {
    private String myPrivateField;

    @VisibleForTesting
    void myPrivateMethod() {
        // テストコードからアクセス可能なメソッドの実装
    }
    
    // 公開されたメソッドや他の非公開メソッドなど
    
}
  • myPrivateFieldフィールドとmyPrivateMethodメソッドが非公開メンバー

myPrivateMethodメソッドに@VisibleForTestingアノテーションが付けられているため、テストコード内からアクセスできる
→テストコード内でmyPrivateMethodメソッドを呼び出してテストできる

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?