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.

3

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?

-1
Avatar
Permanently deleted user

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. 

0

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.

2
Avatar
Permanently deleted user

Is there something wrong? It doesn't work!

-1

You need to start IDEA with this VM option, not your test.

1
Avatar
Permanently deleted user

Thank you very much! I got it!

1
Avatar
Permanently deleted user

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!

0

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

 

3

Perfect.  That worked for me.

0

And don't forget to put closing code at the end of the code!

@Test
public void testScanner() throws Exception{
       Scanner scanner = new Scanner(System.in);
       String line = scanner.nextLine();
       System.out.println(line);
scanner.close(); // ←←← this
}
0

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

0

Hello, I've tried IDEA 2024.1 and 2023.3.6 under Mac/Windows,  click the Help | Find Action from the main menu, type Registry..., select it and in the opened list, find and enable the editable.java.test.console options there, without restart, it works:

0

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.console set:

 @Test
 fun test() {
      println("Your Input: ")
      val scanner = Scanner(System.`in`)
      val line = scanner.nextLine()
      println(line)
      scanner.close() // ←←← this
    }

Which results in:

No line found
java.util.NoSuchElementException: No line found
	at java.base/java.util.Scanner.nextLine(Scanner.java:1651)

I also tried this:

@Test
fun test() {
    println("Your Input: ")
    val line = readLine()
    println(line)
}

… which does not wait for an Input :(

 

0
can you create a feature request at https://youtrack.jetbrains.com/newIssue?project=IDEA, then paste the link here, I'll help assign it to the right team, thanks!
0

请先登录再写评论。