Why can't I input anything from console when i run unit test with JUNIT
已回答
I want to input somthing from console , when i run my unit test with Junit. But seems that IDEA dosn't support this feature. Lots of people will really really expect this function......
@Test
public void testScanner() throws Exception{
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
System.out.println(line);
}
this code will never hava result, and the program will hang up all the time.
请先登录再写评论。
What's the use case? Unit tests are supposed to be run in bulk and automatically, like on the CI server. Who will provide this input for the automated tests?
I want to receive the SMS or the result's captcha,after inputing the code the program can go on. so now i must run it in the main method.
Agree with Serge, normally tests are supposed to be automatic and do not expect input from console. Please see https://youtrack.jetbrains.com/issue/IDEA-148698#comment=27-2257633 for a way to workaround this but please also note that there are side effects.
Is there something wrong? It doesn't work!
You need to start IDEA with this VM option, not your test.
Thank you very much! I got it!
Anna Kozlova@... Any idea how to add this parameter? On starting Intellij with this parameter I get an error that says "unable to load file". I also tried putting the parameters in double quotes and that still doesn't help.
Intellij throws an error stating "cannot find file (path of bin folder - exe)\-Deditable.java.test.console=true ".
Was it necessary to enforce disabling the console in this case? It just makes debugging a test harder. It's retarded to have disabled the console by default!
Hi Mugen,
you need to go Help | Edit Custom VM options and add the line `-Deditable.java.test.console=true` there, then restart. Should actually work
Anna
Perfect. That worked for me.
And don't forget to put closing code at the end of the code!
Why id doesn't work in my Intellij?
Please give me some hint plz.
It's my vmoptions file.
But it doesn't work for me.
Just in case, I put it in the Edit configuration and tried Run, but it doesn't work the same way.
Please Help me 😂
---
My Intellij Version : IntelliJ IDEA 2023.1
Hello, I've tried IDEA 2024.1 and 2023.3.6 under Mac/Windows, click the
Help | Find Actionfrom the main menu, typeRegistry..., select it and in the opened list, find and enable theeditable.java.test.consoleoptions there, without restart, it works:I can confirm that this scenario works for Java in IntelliJ 2024.1.
Is there a possibility to add this feature also to Kotlin Multiplatform Jacky Liu ? The following snippet does not work with
editable.java.test.consoleset:Which results in:
I also tried this:
… which does not wait for an Input :(