Java Unit Test

Source: Internet
Author: User
Tags assert

1, first create a new Java engineering--testjunit. Open the Properties page of the project Testjunit, select the Java Build Path sub-option, click the "Add Library ..." button, select JUnit in the pop-up Add Library dialog box, and select version 4 on the next page. Click "Finish" button after 1.

2, add a new directory under the project Testjunit root testsrc, and put it into the project source code directory, to store the test code, so that it is separated from the source code.

3. Write Unit Test Cases:

Package com.meritit;

Import static Org.junit.Assert.AssertNull;

Import static org.junit.Assert.AssertEquals;

import org.junit.Test;

Public class Testworddealutil {

Test the WORDFORMAT4DB general handling situation

@Test public void Wordformat4dbnormal () {

String target = "EmployeeInfo";

String result = Worddealutil. wordformat4db (target);

assertequals ("Employee_info", result);

}

Handling when testing for NULL

@Test public void Wordformat4dbnull () {

String target = null;

String result = Worddealutil. wordformat4db (target);

Assertnull (result);

}

Processing when testing an empty string

@Test public void Wordformat4dbempty () {

String target = "";

String result = Worddealutil. wordformat4db (target);

assertequals ("", result);

}

To test the case when the first letter is capitalized

@Test public void Wordformat4dbegin () {

String target = "EmployeeInfo";

String result = Worddealutil. wordformat4db (target);

assertequals ("Employee_info", result);

}

To test the case when the tail letter is capitalized

@Test public void Wordformat4dbend () {

String target = "Employeeinfoa";

String result = Worddealutil. wordformat4db (target);

assertequals ("employee_info_a", result);

}

When you test multiple connected letters in uppercase

@Test public void Wordformat4dbtogether () {

String target = "Employeeainfo";

String result = Worddealutil. wordformat4db (target);

assertequals ("Employee_a_info", result);

}

}

4, according to the requirements of the code written as follows:

Package com.meritit;

import Java.util.regex.Matcher;

import Java.util.regex.Pattern;

/**

* Format the contents of a string format such as name, address, etc.

* @author lvxg

*/

Public class Worddealutil {

/**

* Java object names (capitalize the first letter of each word) according to the

* Database naming habits are formatted

* Formatted data is lowercase and uses an underscore to split a named word

*

* Example: EmployeeInfo becomes employee_info after formatting

*

* @param name Java object names

*/

Public Static String wordformat4db (string name) {

Pattern p = pattern. Compile ("[A-z]");

Matcher m = p.matcher (name);

StringBuffer sb = new stringbuffer ();

while (M.find ()) {

M.appendreplacement (SB, "_" +m.group ());

}

return m.appendtail (SB). ToString (). toLowerCase ();

}

}

5, run the test, the result:

The JUnit runtime interface has two test cases not passed.





Java Unit Test

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.