Question
There is the following code snippet:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(random());
}
}
public static int random() {
return new Random().nextInt(100);
}
}
and we'd like to make the random() method always return the same value, not a random one.
There is Force Return action available in the debugger tool window:
but it needs to be performed manually every time we need to return a custom value. How to automate this process?
Answer
Right now there is no straightforward solution to do it right from the debugger / breakpoints view. Here is a related feature request: https://youtrack.jetbrains.com/issue/IDEA-189838
Workaround: Hardcode a needed return value and Use the HotSwap feature to control it during a program execution
-
add a needed return value at the beginning of a method:
public static int random() { if (true) return 1; return new Random().nextInt(100); } -
run the program in debug mode and observe that a new value is returned
-
once it's not needed, remove the
if (true) return 1;without stopping the application and performBuild | Recompile ClassNamefrom the IntelliJ IDEA main menu
Comments
0 comments
Please sign in to leave a comment.