0
私はこのメソッドをユニットテストする必要がありますが、これはPattern.compile("(.*)(" + regEx + ")(.*)");
のため実行できません。誰でも私を助けてくれる?BufferedReaderとPattern.compileをモックする方法は?
public final String getRegEx(String regEx) throws InterruptedException {
StringBuffer sb = new StringBuffer();
String getURL = FrameworkConstants.DIAGNOSTIC_EVENTS;
int timeoutSec = 100;
BufferedReader sr = openURL(getURL, timeoutSec, true);
if (null != sr) {
String inputLine;
try {
Pattern pattern = Pattern.compile("(.*)(" + regEx + ")(.*)");
Matcher matcher;
while (sr.ready()) {
inputLine = sr.readLine();
matcher = pattern.matcher(inputLine);
if (matcher.matches()) {
System.out.println("MATCHED LINE: " + inputLine);
sb.append(matcher.group(2));
sb.append("\n");
}
}
} catch (IOException e) {
exception(e);
} finally {
try {
sr.close();
} catch (IOException e) {
exception(e);
}
}
if (sb.toString().trim().isEmpty()) {
System.out.println("No matches have been found!");
}
} else {
System.out.println(getURL + " - No access to the requested URL!");
}
return sb.toString().trim();
}
これは、試験方法である:
public final void testGetDiagnoseEvent() throws InterruptedException, IOException {
@Mock
private SubTestScript cut_subTestScript = null;
@Mock
private static BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
/** Mock SubTestScript class to access the method */
cut_subTestScript = Mockito.mock(SubTestScript.class, new CallsRealMethods());
/** Mock method method openURL() to return the buffer stored elements . */
bufferedReader =new BufferedReader(new StringReader("#0129: 2017-07-30 02:05:56 00000128 0207 power supply ok\n"
+ "#0105: 2017-07-30 02:05:42 00000169 0232 JRU version correct (SW=0500012B, CFG=105)\n"
+ "#0131: 2017-07-30 02:05:56 00000076 0131 SDMU fully functional\n"
+ "#0132: 2017-07-30 02:05:56 00000076 0132 SDMU fully functional\n"
+ "#0134: 2017-07-30 02:05:56 00000086 0140 Odometry fully functional\n"
+ "#0135: 2017-07-30 02:06:10 00000168 0232 JRU version mismatch (JRU=0000007B,105, EVCMP=0500012B,105)\n"
+ "#0136: 2017-07-30 02:06:14 00000169 0232 JRU version correct (SW=0500012B, CFG=105)\n"
+ "#0137: 2017-07-30 02:06:18 00000168 0232 JRU version mismatch (JRU=0500012B,123, EVC=0500012B,105)\n"
+ "#0138: 2017-07-30 02:06:22 00000169 0232 JRU version correct (SW=0500012B, CFG=105)\n"));
MemberModifier.stub(MemberMatcher.method(TestScript.class, "openURL", String.class, int.class, boolean.class)).toReturn(bufferedReader);
assertEquals("JRU version correct", cut_subTestScript.getRegEx("JRU version correct"));
}
可能重複。 com/questions/21105403/mocking-static-methods-with-mockito) – talex