Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSCI 202 in-class lab — JUnit CSCI 202 in-class lab — JUnit The goal To learn how to do unit testing with JUnit. Unit testing is a methodology for testing individual components of a software produce. It is considered an important part of both extreme programming and agile development and is used in many companies. Microsoft is very serious about unit testing and has thousands of employees whose jobs are to test programs. Sometimes a pair consisting of a programmer and a tester develop software together. In any case if you want a hope of getting a starting job with a high salary as a software engineer, you need to know something about unit testing. JUnit is a popular unit testing framework which is supported by plugin in Java. It is briefly described in Chapter 8 of the textbook and we’re going to use it this week’s lab. Getting started Because unit testing involves the testing of a single class, we aren’t going to create an application project this week. Instead use the menu choices File ⇒ New Project followed by the choices Java and Java and whatever project name you desire. This will give you a project with no Java classes. Now right-click on Source Packages and make the selections New ⇒ Java Class... which will now display a New Java Class window. Put your new class in the edu.unca.cs.csci202 package with IntNode as your class name. Use the following (which you may recognize from the match calls lab) as the body of your IntNode class. private int item ; private IntNode next ; public IntNode() { item = 0 ; next = null ; } public void setItem(int item) { this.item = item ; } public int getItem() { return item ; } public void setNext(IntNode next) { this.next = next ; } public IntNode getNext() { return next ; } You can trying running your program, but you’ll only get a pop-up window saying that there is no project. However, you should compile your file with Run ⇒ Compile File to check that your IntNode class is syntactically correct. Getting lots of help from NetBeans Let’s generate some tests for your class. Right-click on the name of your program and go with the menu choices Tools ⇒ Create Tests. Just go with the defaults in the next window and then select the latest version of JUnit with given a choice. Hopefully you won’t have to download and install JUnit. If all goes well, you should see a entry for Test Packages in the Projects panel. Expand it. A file named IntNodeTest.java should appear. Open it. You should see methods named testSetItem, testGetItem, testSetNext and testGetNext for writing test cases for your methods. You can test your program with Run ⇒ Test Projects but all test will fail because you haven’t written any test cases. A note about the textbook In Java 5 static imports were added to the language. The original language description for the static import suggests that this feature should be used very sparingly. The textbook does not use static imports for JUnit test cases, but the NetBeans-generated test class does. This means that the textbook will say Assert.assertEquals where assertEquals is used in your project. A minute for a presentation Let’s look at the first dozen pages of a presentation on Testing with JUnit from the University of Victoria. Trying it out Testing the existing cases Clearly this is not a very complicated Java class to test. However, let’s complete a test for the testSetItem method. Now try to complete tests for the other three cases. Adding a couple of methods Add two rather pointless operations to the IntNode class. The first with be called doubleItem and will double the item stored in the list. The second with be called selfLoop and return a boolean. It will return true if and only if the next member of the IntNode refers to itself. Now write two methods in IntNodeTest to test your new methods. Testing some constructors Add two constructors to IntNode. One will receive an int as an argument and the other will receive an int and a IntNode as arguments. Again, add code to test these two constructors. If time permits If time permits we will look at the classic tortoise and hare algorithm by Robert W. Floyd for determining if there is a loop in a list of IntNode’s.