Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.

Similar presentations


Presentation on theme: "Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until."— Presentation transcript:

1 Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until Week 8 (this is week 6). A sample solution to Assignment 2 is posted (includes the answers to the questions). Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Documentation using Javadoc comments.
Adding the JUnit5 library to your Assn 3 project in Eclipse. Start JUnit Testing. Fall 2018 CISC124 - Prof. McLeod

3 Fall 2013 CISC124 Javadoc Javadoc.exe is a program that comes with the JDK. (It is in the same directory as javac.exe and java.exe – the “bin” folder). It is not included in the JRE only. If I have written a class, “MyClass.java”, that contains properly formatted comments (more below), then running “javadoc MyClass.java” generates a file “MyClass.html”. The html file contains external documentation generated from the formatted comments in the source code. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

4 Javadoc - Cont. Normal block comments in Java are delimited by “/*” and “*/”. Everything between these delimiters, usually multiple lines, is a comment. Javadoc block comments are delimited by “/**” and “*/”. Fall 2018 CISC124 - Prof. McLeod

5 Javadoc - Cont. The general form of a Javadoc comment: /**
* Summary sentence. * More general information about the * class, attribute or method which * follows the comment, using as many lines * as necessary. (html tags can be included) * * javadoc tags to specify more specific * information, such as parameters and * return values for a method */ Fall 2018 CISC124 - Prof. McLeod

6 Javadoc - Cont. The general form of a Javadoc tag is:
@tagName comment The tags you use depend on what you are describing (class, method or attribute). In the case of methods, you can have a tag for each parameter, the return value, and a tag for each thrown exception. Eclipse (really nice!!) will generate the needed blank tags for you after you type “/**” above a method name. Typically, you will only write javadoc comments for public attributes and methods… Fall 2018 CISC124 - Prof. McLeod

7 Eclipse Javadoc Assist
For example, if you have the method header: public static double[] generateRandomArray(int size) Type /** right above this line and press <enter>. You will get: /** * size */ Fall 2018 CISC124 - Prof. McLeod

8 Eclipse Javadoc Assist, Cont.
Then you have to finish the comment: /** * Generates an array of random doubles. * * The method uses the Math.random() method to generate * an array of doubles between 0.0 and 1.0. size The desired size of the array. The array of random doubles. */ Fall 2018 CISC124 - Prof. McLeod

9 Eclipse Javadoc Assist, Cont.
Hold your cursor over the method header and you will get a preview of what the processed Javadoc will look like: (Or view the Javadoc tag in the same view as the Console tag.) Fall 2018 CISC124 - Prof. McLeod

10 Common Javadoc Tags @param Parameter_name description @throws
Exception_name description @return description @see packageName.ClassName, packageNamme.ClassName#methodName, etc. @author @version Fall 2018 CISC124 - Prof. McLeod

11 Javadoc - Cont. Html tags can also be added to the comments to add more formatting to the resulting document: <em> for emphasis <code> for code font <img> for images <ul><li> </li></ul> for bulleted lists Etc… Fall 2018 CISC124 - Prof. McLeod

12 Javadoc Reference The best reference for javadoc is at:
Fall 2018 CISC124 - Prof. McLeod

13 Javadoc - Cont. The output from Javadoc looks exactly like the API documentation you have already seen - since that is the way it has been generated! The advantage is that when source code is changed, the Javadoc comments can be changed in the source, at the same time. The external documentation is then easily re-generated. Javadoc also provides a consistent look and feel to these API documents. Fall 2018 CISC124 - Prof. McLeod

14 Javadoc - Cont. Most modern IDE’s (like NetBeans and Eclipse) allow you to run Javadoc from within the environment, and provide tools and wizards to help you create comments. In Eclipse, select “Project”, then “Generate Javadoc…”. Let’s run javadoc on the Halloween5 class. (Note that the first time you do this, you will have to tell Eclipse where javadoc.exe resides.) Fall 2018 CISC124 - Prof. McLeod

15 Testing in Assignment 3 You are supplied with JUnit5 testing programs to test your Pizza and LineItem classes. JUnit testing is the established and best way to test Java programs. JUnit testing will be covered in class (starting today!), but you don’t need to understand JUnit testing to run the testing programs – you do not need to write any new tests and should not modify the existing tests. Fall 2018 CISC124 - Prof. McLeod

16 Testing in Assignment 3, Cont.
To run the supplied tests you will need to link the JUnit 5 library to your assignment 3 project. The wizard that creates a JUnit testing class can do this for you or you can: First, right click on the project name, choose “Build Path”, then “Add Libraries…” Fall 2018 CISC124 - Prof. McLeod

17 Testing in Assignment 3, Cont.
Choose “JUnit”. Then “Next >”. On the next screen, make sure that JUnit 5 is chosen and click on “Finish”. Fall 2018 CISC124 - Prof. McLeod

18 Testing in Assignment 3, Cont.
If you already have the testing classes in your project, then all those errors should go away! You can run the JUnit testing files on their own. The “suite” program just runs both of them together. Fall 2018 CISC124 - Prof. McLeod

19 Testing Until now “we” have used another class with a main method to run tests on the class we have “harnessed” for testing. See TestHalloween4.java and TestHalloween5.java for example. JUnit is a framework designed for this kind of work and it is very easy to use in Eclipse. Fall 2018 CISC124 - Prof. McLeod

20 JUnit Testing Best reference is junit.org
See the links in the centre column and the API docs. For use in Eclipse see: “Java development user guide” > “Getting Started” > “Basic tutorial” > “Writing and running JUnit tests”. Fall 2018 CISC124 - Prof. McLeod

21 JUnit Testing in Eclipse
In the project containing the class(es) you wish to test, add a Testing class by choosing “New JUnit Test Case”: Name the class and choose the classes to be tested. Choose extra stubs, if needed. Fall 2018 CISC124 - Prof. McLeod

22 JUnit Testing in Eclipse, Cont.
Next, let the wizard create test method stubs in your testing class for all methods you wish to test: Fall 2018 CISC124 - Prof. McLeod

23 JUnit Testing in Eclipse, Cont.
Allow the wizard to add the JUnit library to the Build Path, if prompted. Fall 2018 CISC124 - Prof. McLeod

24 JUnit Testing in Eclipse, Cont.
Fill in the method stubs with tests. You can have multiple tests in a method or multiple methods or both, as required. You could end up with hundreds of tests, just for one object! See Halloween5Test.java. It contains one test that will not pass – just to show what a failed test looks like. Fall 2018 CISC124 - Prof. McLeod

25 JUnit 5 Assertions Use (expected, actual) or (expected, actual, String). The optional argument is a string message that would describe what is being tested. assertArrayEquals() assertEquals() assertFalse() assertNotEquals() assertNotNull() assertNull() assertTrue() Fall 2018 CISC124 - Prof. McLeod

26 IllegalHalloweenException.class
JUnit 5 Assertions, Cont. Use assertThrows() to make sure an exception is thrown when it should be. This assertion uses two arguments that are built using syntax we have not yet discussed. The two are arguments are a Class object and an Executable object. The Class object: Any object has a .class constant attribute that supplies the Class object. So we use: IllegalHalloweenException.class to supply the Class object for the first argument, the expected exception type. Fall 2018 CISC124 - Prof. McLeod

27 JUnit 5 Assertions, Cont. The second argument is the Executable object. This will contain the code that is supposed to throw the identified exception. It is easiest to construct this object using a Lambda Function. Which we will discuss, just not yet! () -> new Halloween5(badYear, numKids, temps, condition) Fall 2018 CISC124 - Prof. McLeod

28 Halloween5Test.java, Cont.
Uses setup and teardown methods. Also uses assertThat (for a silly test…). Fall 2018 CISC124 - Prof. McLeod

29 Setup and Teardown annotation with methods that will run before every test. for methods to run after every test. @BeforeAll runs once before all tests. @AfterAll runs once after all tests. Fall 2018 CISC124 - Prof. McLeod

30 assertThat() Takes a Matcher<T> object for its second (or third) parameter. Possible object types: AllOf, AnyOf, BaseMatcher, CombinableMatcher, CustomMatcher, CustomTypeSafeMatcher, DescribedAs, DiagnosingMatcher, Every, FeatureMatcher, Is, IsAnything, IsCollectionContaining, IsEqual, IsInstanceOf, IsNot, IsNull, IsSame, StringContains, StringEndsWith, StringStartsWith, SubstringMatcher, TypeSafeDiagnosingMatcher, TypeSafeMatcher Fall 2018 CISC124 - Prof. McLeod

31 assertThat(), Cont. For example (from JUnit.org): And, there is a:
assertThat("albumen", both(containstring("a")).and(containsString("b")) And, there is a: import static org.hamcrest.CoreMatchers.*; In the JUnit API docs look up this class for a list of these useful methods. Fall 2018 CISC124 - Prof. McLeod

32 assertThat, Cont. Note that the order is different - the actual comes first followed by the expected. You can still have a String message as the first parameter. You can build more sophisticated assertions with this method. Fall 2018 CISC124 - Prof. McLeod

33 No try/catch Don’t bother with try/catch blocks.
If you are testing code that has a throws decoration, then just have your unit test method throw Exception to satisfy the compiler. If an exception is thrown unexpectedly (a run-time error!) you will get a different kind of failure notice in the report. Note that the testing does not stop! Fall 2018 CISC124 - Prof. McLeod


Download ppt "Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until."

Similar presentations


Ads by Google