IntelliJ IDEA控制台junit不能用Scanner输入---版本:IntelliJ IDEA 2019.3.3 x64

IntelliJ IDEA控制台junit不能用Scanner输入

PS:IDE版本IntelliJ IDEA 2019.3.3 x64

测试代码:

import org.junit.Test;

import java.util.Scanner;

public class TestScan {
    @Test
    public void testScan() {
        Scanner scan = new Scanner(System.in);
        System.out.print("用户名:");
        String userName = scan.nextLine();
        System.out.print("密  码:");
        String password = scan.nextLine();

        System.out.println("用户名:" + userName);
        System.out.println("密  码:" + password);
    }
}

错误运行:

说明:

  1. 这段代码在eclipse是可以运行的,在eclipse的Junit单元测试中是可以输入内容的
  2. 使用Junit运行单元测试时,为什么不能从控制台输入任何内容?由于Junit是自动测试单元,不支持手动输入!
  3. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000556544-Why-can-t-I-input-anything-from-console-when-i-run-unit-test-with-JUNIT

解决方法:

方法一:使用main方法

测试代码:

import java.util.Scanner;

public class TestScan {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("用户名:");
        String userName = scan.nextLine();
        System.out.print("密  码:");
        String password = scan.nextLine();

        System.out.println("用户名:" + userName);
        System.out.println("密  码:" + password);
    }
    
}

运行结果:

 

方法二:更改vmoptions配置文件

步骤:

  1. 找到IntelliJ IDEA安装目录
  2. 进入bin文件夹
  3. 如果你的系统是32位的,找到【idea.exe.vmoptions】,使用记事本打开;如果你的系统是64位的,找到【idea64.exe.vmoptions】,使用记事本打开
  4. 在文本最后添加  -Deditable.java.test.console=true  然后保存
  5. 重启IntelliJ IDEA

测试代码:

import org.junit.Test;

import java.util.Scanner;

public class TestScan {
    
    @Test
    public void testScan() {
        Scanner scan = new Scanner(System.in);
        System.out.print("用户名:");
        String userName = scan.nextLine();
        System.out.print("密  码:");
        String password = scan.nextLine();

        System.out.println("用户名:" + userName);
        System.out.println("密  码:" + password);
    }
}

运行结果:

方法三:更改Edit Custom VM Options

说明:

此方法和方法二类似,但是此方法修改的是IDEA加载时的vmoptions配置文件(【C:\Users\XiaoGangpao\.IntelliJIdea2019.3\config】),而我安装的目录是(【D:\IntelliJ IDEA 2019.3.3\bin】)

IDEA加载时的vmoptions配置文件和安装目录bin中的vmoptions配置文件可能不一样

C:\Users\XiaoGangpao\.IntelliJIdea2019.3\config

C:C盘
\UsersUser目录
\XiaoGangpao自己的用户目录
\.IntelliJIdea2019.3IDEA目录(不是安装目录)
\configconfig目录

 

 

 

 

 

步骤:

  1. 在导航找到help ->Edit Custom VM Options
  2. 弹出的对话框文件中追加  -Deditable.java.test.console=true

测试代码:

import org.junit.Test;

import java.util.Scanner;

public class TestScan {
    
    @Test
    public void testScan() {
        Scanner scan = new Scanner(System.in);
        System.out.print("用户名:");
        String userName = scan.nextLine();
        System.out.print("密  码:");
        String password = scan.nextLine();

        System.out.println("用户名:" + userName);
        System.out.println("密  码:" + password);
    }
}

运行结果:

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值