Keep the bar green to keep the code Clean--junit detailed (ii)

Source: Internet
Author: User

Test Cases & Test suites

Give me a chestnut:

  1. Write the Mystack class simulation stack, and test case writing test;

  2. Write the file deletion method and delete the test. No more demos, poke this get code

Mystack class:

  1. Public class MYSTATCK {
  2. Private String[] elements;
  3. Private int Nextindex;
  4. Public Mystatck () {
  5. elements = new string[100];
  6. Nextindex = 0;
  7. }
  8. Public void push (String Element) throws Exception {
  9. if (Nextindex >= 100) {
  10. Throw New Exception (" array out of bounds exception! ");
  11. }
  12. elements[nextindex++] = element;
  13. }
  14. Public String pop () throws Exception {
  15. if (Nextindex <= 0) {
  16. Throw New Exception (" array out of bounds exception! ");
  17. }
  18. return Elements[--nextindex];
  19. }
  20. Public String Top () throws Exception {
  21. if (Nextindex <= 0) {
  22. Throw New Exception (" array out of bounds exception! ");
  23. }
  24. return elements[nextindex-1];
  25. }
  26. }

Write tests on the push method:

  1. Public void Testpush () {
  2. Mystatck mystatck = new mystatck ();
  3. The exception that the method throws is Try-catch treated in the//test case.
  4. Try {
  5. Mystatck.push ("Hello world!");
  6. } catch (Exception e) {
  7. Assert.fail ("Push method exception, test failed. ");
  8. }
  9. String result = null;
  10. Try {
  11. result = Mystatck.pop ();
  12. } catch (Exception e) {
  13. E.printstacktrace ();
  14. }
  15. //Verify the assertion that the pressed string is "Hello world!".
  16. Assert.assertequals ("Hello world!", result);
  17. }

Although the Pop method is called in the Testpush test case, a new test case is still created for the pop method:

You can use other methods in the test, or you won't be able to test it.

  1. Public void Testpop () {
  2. Mystatck mystatck = new mystatck ();
  3. Try {
  4. Mystatck.push ("Hello world!");
  5. } catch (Exception e) {
  6. E.printstacktrace ();
  7. }
  8. String result = null;
  9. Try {
  10. result = Mystatck.pop ();
  11. } catch (Exception e) {
  12. Assert.fail ("Pop method exception, test failed. ");
  13. }
  14. //Verify that the assertion popup string is "Hello world!".
  15. Assert.assertequals ("Hello world!", result);
  16. }

Two test methods are roughly the same, but with a different focus. Focus on the assertion judgment of the test method.

Each test case only does one thing and only tests an aspect.

With the development of projects, there are more and more classes and more tests. The mechanical action of a single test also slows down the speed. Then you need a simpler way to test all of your test cases with just a few points-this is using a test suite.

Test Suite (TestSuite): Multiple tests can be combined to perform multiple tests simultaneously

To create a test suite contract:

    1. Create a test class within the test source directory;
    2. Create the public static Test suite () {} method.

Paste Code

  1. Public class Testall extends TestCase {
  2. Public Static Test Suite () {
  3. TestSuite Suite = new TestSuite ();
  4. Suite.addtestsuite (calctest. Class);
  5. Suite.addtestsuite (deletealltest. Class);
  6. Suite.addtestsuite (mystatcktest. Class);
  7. return Suite;
  8. }
  9. }

Run results

Keep the bar green to keep the code Clean--junit detailed (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.