【发布时间】:2020-10-28 09:00:50
【问题描述】:
我正在为学生创建一个简单的 jshell 来技术 java,我想使用 JUNIT 的断言,但是当抛出异常时,sn-ps 不会被拒绝,我看到它可能需要有一个 custom execution engine 但它也是很多。
这是我的代码,它将在 IDE 上运行 REPL,但不会拒绝 throw new Exception("Exception message");
package simple.repl;
import java.io.*;
import java.util.List;
import jdk.jshell.*;
import jdk.jshell.Snippet.Status;
public class Main {
public static void main(String[] args) throws IOException {
try (JShell js = JShell.builder().in(System.in).out(System.out).err(System.out).build()) {
for (final String str : System.getProperty("java.class.path").split(File.pathSeparator)) {
js.addToClasspath(str);
}
do {
//input System.out.println("hello");
//int as = 1;
//throw new Exception("Exception message");
System.out.print("Enter some Java code: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input= reader.readLine();
if (input == null) {
break;
}
List<SnippetEvent> events = js.eval(input);
for (SnippetEvent e : events) {
StringBuilder sb = new StringBuilder();
if (e.causeSnippet() == null) {
// We have a snippet creation event
switch (e.status()) {
case VALID:
sb.append("Successful ");
break;
case RECOVERABLE_DEFINED:
sb.append("With unresolved references ");
break;
case RECOVERABLE_NOT_DEFINED:
sb.append("Possibly reparable, failed ");
break;
case REJECTED:
sb.append("Failed ");
break;
}
if (e.previousStatus() == Status.NONEXISTENT) {
sb.append("addition");
} else {
sb.append("modification");
}
sb.append(" of ");
sb.append(e.snippet().source());
System.out.println(sb);
if (e.value() != null) {
System.out.printf("Value is: %s\n", e.value());
}
System.out.flush();
}
}
} while (true);
}
System.out.println("\nGoodbye");
}
}
【问题讨论】:
标签: java exception junit assertion jshell