开发者

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

开发者 https://www.devze.com 2023-01-28 09:02 出处:网络
My lecturer mentioned this before, but I don\'t really understand why this is the case. Would anyone be able to explain ?

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. Why can I not use a main in testing this class ?

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. 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. 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...


Because JUnit is providing a main that calls the functions that you provide in your classes. You can still have your own main functions; they just won't get used when you run 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.


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. 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.

The unit testing API's available provide you with useful utilities that you would ordinarily have to code yourself.

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. So you should write the method that computes your array, and use Junit to just test the method.

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.

The JUnit test runner finds the tests by reflection.

See the documentation here.


See: org.junit.runner.JUnitCore.main(String...), something like that is underlying.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号