Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NetBeans

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use NetBeans", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use NetBeans" this article.

one。 Create a project

Next, open NetBeans, then click the menu "File"-> "New Project", and open the "New Project" dialog box:

Please select General in Category, select Java Application from Project, and then click next to enter the New Java Application dialog box:

In this dialog box, you need to set the name of the project and the directory where the project is located. I named my project JUnitTest, and the "project location" is G:\ YPJCCK\ JUnit\ NetBeans. In addition, please remove the check mark before "create main class". After the project has been created, JUnit 3.8.1 has been brought with it.

two。 Write JavaBean for testing

The JavaBean for testing is simple, named Book, with only two attributes, id and name, which will be used in the two use cases. Let's start writing the JavaBean.

Click "File"-> "New File" to open the "New File" dialog box:

Make sure JUnitTest is selected for Project, then select the Java class in Category, select the Java class in Files of Type, and click next to proceed to the next window:

Set the class name to Book and the package to net.zheng.junit.test. Click "finish" when the configuration is completed, and the modified code is as follows:

Package net.zheng.junit.test;public class Book {private String id = null;private String name = null;public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;}}

At this point, the JavaBean for the test is written.

three。 Write test cases

There is only one class for testing, named BookTest, which inherits from the junit.framework.TestCase class. The BookTest class contains two use cases, corresponding to the testId and testName methods of the class, that is, each method implements a test case. Note that in JUnit, every method used to implement a test case in a subclass of junit.framework.TestCase must be named in the format of testXXX, and these methods are executed at run time. In addition, BookTest also includes two methods, setUp and tearDown, the former is executed before each test method starts, and is mostly used for initialization, while the latter is executed after each test method is completed, which is mostly used to clean up resources. Let's start writing BookTest.

Click "File"-> "New File" to open the "New File" dialog box:

Make sure JUnitTest is selected for Project, then select the JUnit class in Category, select the test for the existing class in Files of Type, click next to proceed to the next window:

Select the class to test, click "finish", and modify the code as follows:

Package net.zheng.junit.test;import junit.framework.TestCase;import junit.framework.Assert;public class BookTest extends TestCase {Book book = null;// suggests creating a construct public BookTest (String name) {super (name);} protected void setUp () throws Exception {/ / calling the setUp () of the superclass for the test class to ensure that the test environment is initialized super.setUp (); System.out.println ("Test begins!") ; book = new Book (); System.out.println ("book object initialized!") ;} protected void tearDown () throws Exception {System.out.println ("book objects will be cleaned!") ; book = null;System.out.println ("Test is over!") ; / / call tearDown () of the superclass to ensure that the test environment is cleaned up super.tearDown ();} public void testId () {book.setId ("001"); / / set the value of the id property to 001pm / use Assert to check whether the value of the id property is 001Assert.assertEquals ("001", book.getId ()); System.out.println ("the id property is tested!") ;} public void testName () {book.setName ("ASP"); / / set the value of the name property to ASP//. Use Assert to check whether the value of the name property is JSP. This is an error-bound test Assert.assertEquals ("JSP", book.getName ()); System.out.println ("the name property is tested!"). ;}}

There is nothing to say about the setUp and tearDown methods here, but the initialization and cleanup of the book object is performed, but testId and testName need to be explained. The former is testing the id property of book, first assigning it to "001", and then using the assertEquals method of Assert to check whether the value stored in the id property is the expected value. Since my expected value is also "001", this use case should be successful after execution. The latter tests the name property of book, which is first assigned to "ASP", and then uses the assertEquals method of Assert to see if the value is expected. Because I deliberately set the expected value to "JSP", there will be an error after the use case is executed. But please note that because I deliberately want to make the test wrong, I set the expected value to an impossible value, if you are a tester, please do not do this, otherwise if something else leads to an error, it's easy to cause unnecessary trouble to yourself.

Here is a brief introduction to the static class junit.framework.Assert used above. This class mainly contains eight methods:

The 1.assertEquals () method, which is used to check whether the value stored in the object is the expected value, similar to the equals () method used in string comparison

The 2.assertFalse () and assertTrue () methods are used to check whether the variable is false or true. If the value of the variable viewed by assertFalse () is false, the test succeeds, and if it is true, it fails. AssertTrue () is the opposite.

The 3.assertSame () and assertNotSame () methods are used to compare whether the references of two objects are equal and unequal, similar to comparing two objects through "=" and "! ="

The 4.assertNull () and assertNotNull () methods are used to check whether the object is empty and not empty

The 5.fail () method, which means failure, is used to throw an error. I personally think there are two uses: the first is in test-driven development, because the test cases are written before the class being tested, and it is not clear whether they are correct or not, you can use the fail method to throw errors for simulation; the second is to throw unexpected errors, such as whether the data to be read from the database is correct, and the cause of the error is database connection failure.

four。 Run BookTest

Once you've written the BookTest, you're ready to run. Please select BookTest in the "Project" column, right-click, and select "run File". The test information will be output from the "output" window:

five。 Test suite

When there are multiple test classes that need to be tested at once, you can use test suites to do this.

In NetBeans, click File-> New File to open the New File dialog box:

Make sure JUnitTest is selected for Project, then select the JUnit class in Category, select the test suite in Files of Type, and click next to proceed to the next window:

Change the class name to AllTests, click finish, and then modify the code as follows:

Package net.zheng.junit.test;import junit.framework.*;public class AllTests extends TestCase {public AllTests (String testName) {super (testName);} public static Test suite () {TestSuite ts= new TestSuite ("AllTests"); ts.addTestSuite (BookTest.class); return ts;}}

TestSuite is used to organize test classes, and the test classes are added to the ts object through its addTestSuite () method. All test classes added to ts will be executed when the file is run. In addition, you can use its constructor to add test classes to ts objects when defining ts objects, such as:

TestSuite ts = new TestSuite (BookTest.class)

This does not affect the subsequent use of the addTestSuite () method. The test suite runs in the same way as before.

In addition, JUnit itself provides an environment for running tests, but some changes need to be made in NetBeans, so I won't go into detail, just the code here:

Package net.zheng.junit.test;import junit.framework.*;public class Test {public static void main (String [] args) {/ / assemble test classes using TestSuite TestSuite ts = new TestSuite (); ts.addTestSuite (TestBook.class); / / textui, command line mode junit.textui.TestRunner.run (ts); / / swingui,Swing mode / / junit.swingui.TestRunner.run (ts.getClass ()); / / awtui,AWT mode / / junit.awtui.TestRunner.run (ts.getClass ());}}

The above is all the contents of this article "how to use NetBeans". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report