質問するログイン新規登録

質問編集履歴

1

投稿ミスの修正

2017/10/02 13:14

投稿

gu3.net
gu3.net

スコア11

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,9 @@
3
3
  privateメソッド内でインスタンスをnewして、メソッド呼び出しをしている箇所をMock化し、
4
4
  自分で生成したオブジェクトをそのまま返すようなテストを実行したいと考えています。
5
5
 
6
- 下記のように実装しましたが、Processbuilder.start()でNullPointerになっています。
6
+ 下記のように実装しましたが、NullPointerになっています。
7
+ CommandクラスをMock化しているので、sampleA内の`Cmd.execute`は実行されず、
8
+ when句で設定したResultが返却されると思っているのですが、どうやら実行されており、`Processbuilder.start`でエラーとなっています。
7
9
 
8
10
 
9
11
  ###発生している問題・エラーメッセージ
@@ -15,6 +17,29 @@
15
17
 
16
18
  ###該当のソースコード
17
19
  ```java
20
+ @RunWith(SpringRunner.class)
21
+ @SpringBootTest
22
+ public class SampleATest {
23
+
24
+ private static final SampleA sampleA = new SampleA();
25
+
26
+ @Test
27
+ public void testExecuteCommand() throws Exception {
28
+ Method method = SampleA.class.getDeclaredMethod("executeCommand", Object.class, String.class);
29
+ method.setAccessible(true);
30
+
31
+ Result result = mock(Result.class);
32
+ Whitebox.setInternalState(result, "exitValue", 0);
33
+
34
+ Command command = spy(Command.class);
35
+ when(command.execute(anyObject(), anyObject())).thenReturn(result);
36
+
37
+ Result result2 = (Result) method.invoke(sampleA, obj, strs);
38
+ assertThat(result2.getExitValue(), is(0));
39
+ }
40
+ }
41
+ ```
42
+ ```java
18
43
  public class SampleA {
19
44
  private void executeCommand(Object Obj, String str) {
20
45
  Command Cmd = new Command();
@@ -37,7 +62,7 @@
37
62
  ProcessBuilder builder = new ProcessBuilder(CmdList);
38
63
  Process process = builder.start();
39
64
  try {
40
- result.exitValue = process.exitValue();
65
+ result.exitValue = process.exitValue();
41
66
  } finally {
42
67
  process.destroy();
43
68
  }
@@ -46,12 +71,10 @@
46
71
  }
47
72
  }
48
73
  ```
49
- ```ここに言語を入力
50
- ここにご自身が実行したソースコードを書いてください
51
- ```
52
74
 
53
- ###試したこと
75
+ ###補足情報(言語/FW/ツール等のバージョンなど)
54
- 課題に対してアプローチしたことを記載してください
76
+ * SpringBoot 1.5.x
77
+ * JUnit 4.x
78
+ * Mockito 2.x
55
79
 
56
- ###補足情報(言語/FW/ツル等バージョンなど)
80
+ ドにマスクをしておりますで、不整合がありましたら申し訳ありません。
57
- より詳細な情報