Regular Expression and junit test, regular expression junit

Source: Internet
Author: User

Regular Expression and junit test, regular expression junit

You need to know some regular expression statements, and then write the regular expression statements following the rules. Then, it is about junit testing.

 

 

After learning about a document you have read, you can take a look at the document, or search for it later.

 

The regular expression is a string:

Starting with ^

End with $.

[] Indicates the value range.

\ D indicates a number.

The following two expressions are equivalent:

^ [0-9] * $

^ \ D * $

Both represent numbers

The following indicates multiple Chinese characters:

^ [\ U4e00-\ u9fa5] {0,} $

The Chinese character code set is from \ u4e00-\ u9fa5

All English letters and numbers:

^ [A-zA-Z0-9] * $

4 to 40 digits and letters:

^ [A-zA-Z0-9] {} $

At least one character should be a letter:

^ [A-zA-Z] + $

\ W is equivalent to ^ [a-zA-Z0-9] * $.

So we can write a simple email Verification:

Regex = "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\\. \ w + ([-.] \ w +) * $ ";

 

Use String test = new string (forTest );

Test. matches (regex) for comparison

 

 

Run the following code: package com. letben. regex; public class Regex {public static void main (String [] args) {String regex = "^ [0-9] * $"; String msg = "34 "; boolean isok = msg. matches (regex); System. out. println (isok); regex = "^ \ d * $"; System. out. println (msg. matches (regex); System. out. println ("***********************"); regex = "^ \ d {0, 3} $"; System. out. println (msg. matches (regex); System. out. println ("***************************"); regex = "^ [\ u4e00-\ u9fa5] {0,} $"; msg = "Chinese character"; System. out. println (msg. matches (regex); System. out. println ("********************************"); // regex = "^ [A-Za-z0-9] + $"; regex = "^ [A-Za-z0-9] {1234} $"; msg = ""; System. out. println (msg. matches (regex); // All are English letters regex = "^ [A-Za-z] + $"; msg = "ABCcddsd"; System. out. println (msg. matches (regex); regex = "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\\. \ w + ([-.] \ w +) * $ "; msg =" liguohua1110@163.com "; System. out. println (msg. matches (regex ));}}

 

 

Test:

 

This lecture is very simple, as mentioned in the school.

Class to be tested:

 

package Calculate;public class Calculate {    public int add(int a,int b){        return a+b;    }    public int minus(int a,int b){        return a-b;    }}

 

Right-click the current class and create a Junit test case. This is the test case. For Development, we can create many corresponding test cases to form a test case set, once the test case set is well written, a variable amount of source code can be used for testing. This test case set is actually cool. This is the legendary white box test.

In short, it is not so in-depth, but it is indeed to be written. Now we have such an impression. You must never lose your notes! In other words, I don't really want to back it up. As the usage increases, I will gradually become familiar with the original architecture. Burst Error: errors is a syntax error. Failure is a logical error.

 

Package Calculate; import static org. junit. assert. *; import org. junit. after; import org. junit. afterClass; import org. junit. before; import org. junit. beforeClass; import org. junit. test; public class TestCalculate {/* This is a set of Calculate c; // the object that is called to obtain this class Before each method is used @ Before public void getMyClass () {c = new Calculate () ;}// release the class object After each method is used @ After public void dismissMyClass () {c = null ;} * /// a full test case is provided here, and then the test can be executed. // Used to check different results static Calculate c; // obtain this object @ BeforeClass public static void getMyClass () {c = new Calculate ();} before the class is created ();} // release the object after the class is created. @ AfterClass public static void dismissMyClass () {c = null;} @ Test public void testAdd () {int result = c. add (4, 5); assertEquals (9, result) ;}@ Test public void testMinus () {int result = c. minus (4, 3); assertEquals (1, result );}}

 

 

 

 

 

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.