简体   繁体   English

如何使用 JUnit 5 测试 Java 中的 Public Static Void Main 方法?

[英]How to test the Public Static Void Main method in Java using JUnit 5?

I want to take inputs in the main method , then pass to another that returns a value back to main and print the returned value in main method.我想在 main 方法中获取输入,然后传递给另一个将值返回给 main 并在 main 方法中打印返回值的方法。 I want to do this using JUnit 5.我想使用 JUnit 5 来​​做到这一点。

(I am new to JUnit 5, so, would really appreciate the help.) (我是 JUnit 5 的新手,因此,非常感谢您的帮助。)

It is considered bad to test static methods because complex static methods themselves are bad and anti-OOP.测试静态方法被认为是不好的,因为复杂的静态方法本身就是不好的并且是反 OOP 的。

You can instead write your logic in a separate class, instantiate an instance of it in your main and use its method.您可以改为在单独的类中编写逻辑,在 main 中实例化它的实例并使用其方法。

Then with JUnit 5, test the class' method by instantiating that class and using the method.然后使用 JUnit 5,通过实例化该类并使用该方法来测试该类的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM