Personal projects (JUnit unit tests)

Source: Internet
Author: User

---restore content starts---

I. Introduction of the topic

The unit test I chose to do a basic operation of the program, the program implemented the addition, subtraction, multiplication, except, squared, the inverse of the operation, the program to test the comparison of simple, for the initial contact with JUnit I test is easier to understand.

Second, the source of GitHub links

Original code: Https://github.com/FBean/myself_project/blob/master/Calculator1.java

Test: Https://github.com/FBean/myself_project/blob/master/Calculator1Test.java

Third, the design of the module test cases, test results

Calculator1.java's Design module

Calculator1test.java Test Cases

Test results

The progress bar is red color indicates that the error is found, the specific test results on the progress bar indicates "a total of 4 tests, of which 1 tests were ignored, two Tests failed."

The JUNIT4 framework is used in the test class, and the package is naturally included in the corresponding package. One of the most important package is org.junit.*. After it is included, most of the features are there. There is also a word that is very important "import static org.junit.assert.*;", we use a series of assertequals methods when testing from this package.

Which class you want to test, you'll first create an object of that class.

In order to test the calculator class, we must create a calculator object.

Comment Description

@Test:

Indicates that the method is a test method

@Before:

The method that uses the metadata is executed one time before each test method executes.

@After:

The method that uses the metadata is executed once after each test method executes.

Note: @Before and @after can only be marked by one method. This is equivalent to replacing the setup and Teardown methods in the previous version of JUnit, but you can still call that name, but JUnit will not ask you to do so.

@Ignore:

The test method for the metadata token is ignored in the test. You can also pass a string argument to the label to indicate why this test method is ignored. For example: @lgnore ("The method has not been implemented"), when executed, only reports that the method is not implemented and does not run the test method.

Iv. problems and solutions, experience

1, in the design of the time it is best to right-click the project name select Properties Select Java Compiler set Compiler compliance level to a higher version

2, to use JUNIT4 test must do the following

Right-click the project name to select Properties

Select the Java Build Path option in the Open interface

Then click the button below

Select the JUnit option

Click the Next button to set the JUnit library version to JUNIT4 in the popup interface

Finally click the Finish button

3, for this JUNIT4 unit test I was only in the initial contact stage, the operation of the test case is not very clear, but after this operation feeling to test the program is very necessary, so that the entire program runs at a good knowledge, but also according to the test results of the program to make minor adjustments, This is a real long-time knowledge.

---restore content ends---

Personal projects (JUnit unit tests)

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.