简体   繁体   English

为什么用JUnit进行单元测试的类没有主类?

[英]Why can classes being unit tested with JUnit not have a main?

My lecturer mentioned this before, but I don't really understand why this is the case. 我的讲师以前曾提到过这一点,但是我真的不明白为什么会这样。 Would anyone be able to explain ? 有人可以解释吗?

We are writing a program to compute an array list of prime numbers, and we have to use JUnit to ensure all members of this arraylist are prime. 我们正在编写一个程序来计算素数数组列表,并且必须使用JUnit来确保此数组列表的所有成员都是素数。 Why can I not use a main in testing this class ? 为什么不能在测试此类时使用main?

Thank you very much :) 非常感谢你 :)

Ok these answers are for the most part too complex. 好的,这些答案大部分都太复杂了。 I think your question is more fundamental. 我认为您的问题更为根本。 ANd its a very good one 它是一个非常好的

The answer is when you become a java developer and start writing large amount of code that get updated/fixed over time then it helps to have a separate test plug-in that will automatically run tests on your code from outside the code to check if it's still working in the way you would expect. 答案是当您成为Java开发人员并开始编写随时间推移而更新/修复的大量代码时,那么使用一个单独的测试插件会有所帮助,该插件将自动从代码外部对您的代码运行测试以检查是否仍以您期望的方式工作。 This means you can fix/debug different aspect of the code for whatever reason and afterwards your boss walks over and asks does the code still do what the client wanted it to do since your fix? 这意味着您可以出于任何原因修复/调试代码的不同方面,然后您的老板走过去,并询问代码自修复以来仍然按照客户的意愿进行操作吗? Without complication You can answer him without complex in-main error statements, which are mixed up with the normal program output (and slow down the code in non test conditions), but with a pretty green junit bar that says it all still works. 无需复杂,您可以在没有复杂的内部错误语句的情况下回答他,该错误语句与正常的程序输出混合在一起(并在非测试条件下降低了代码的速度),但是带有一个漂亮的绿色junit栏,表示所有语句仍然有效。 You won't see the value of this until you develop large projects and you have hundreds of tests to do. 在开发大型项目并且要做数百个测试之前,您将看不到它的价值。 In addition junit has a number of other tricks up its sleeves... 此外,junit还提供了许多其他技巧。

Because JUnit is providing a main that calls the functions that you provide in your classes. 因为JUnit提供了一个调用您在类中提供的函数的main You can still have your own main functions; 您仍然可以拥有自己的main功能。 they just won't get used when you run JUnit. 当您运行JUnit时,它们将不会被使用。 You can use main functions to test your own classes individually, but using JUnit has some advantages as described in org.life.java's answer. 您可以使用main功能分别测试您自己的类,但是使用JUnit具有org.life.java的答案中所述的一些优点。

You can, it just wouldn't be recommended. 您可以,只是不推荐使用。 If you write a unit test for testing it, then you can use the junit test runner to run the test and to produce a report indicating whether it passed or failed. 如果编写用于测试的单元测试,则可以使用junit测试运行程序来运行该测试并生成报告,以表明该测试是通过还是失败。 If you don't do this then you'll need to code your own report mechanism. 如果您不这样做,则需要编写自己的报告机制。

Unit tests have the following structure normally: 单元测试通常具有以下结构:

  1. Create test infrastructure 创建测试基础架构
  2. Execute test 执行测试
  3. Validate passed 验证通过

Your situation has something similar and is thus a good candidate for using junit. 您的情况类似,因此很适合使用junit。

The unit testing API's available provide you with useful utilities that you would ordinarily have to code yourself. 可用的单元测试API为您提供了通常必须自己编写的有用实用程序。

Why don't you try both approaches and see for yourself. 您为什么不尝试两种方法并自己看看。

In unit testing you are not testing anything as a whole. 在单元测试中,您并未测试任何整体。 A unit test must test a UNIT normally a method. 单元测试通常必须测试UNIT的一种方法。 So you should write the method that computes your array, and use Junit to just test the method. 因此,您应该编写用于计算数组的方法,并使用Junit来测试该方法。

The main method is just an entrypoint and it "defines" the flow of the procedure. 主要方法只是一个入口点,它“定义”过程的流程。 In unit testing we don't worry on flow. 在单元测试中,我们不必担心流程。 We just forcus on the unit. 我们只是在单元上作罢。 The program flow is verified using the System/Component test, not by the unit tests. 使用系统/组件测试而不是通过单元测试来验证程序流。

Because JUnit tests are run by a framework not as a standard console application. 因为JUnit测试是由框架而不是作为标准控制台应用程序运行的。

The JUnit test runner finds the tests by reflection. JUnit测试运行器通过反射查找测试。

See the documentation here . 请参阅此处的文档。

参见: org.junit.runner.JUnitCore.main(String...) ,诸如此类的基础。

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

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