Junit Tests.

Slides:



Advertisements
Similar presentations
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Advertisements

T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Using Visual Basic 6.0 to Create Web-Based Database Applications
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
SilkTest 2008 R2 SP1: Silk4J Introduction. ConfidentialCopyright © 2008 Borland Software Corporation. 2 What is Silk4J? Silk4J enables you to create functional.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
JUnit SWE 619 Summer July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Mark L. Hornick 1.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Classes - Intermediate
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Introduction to Exceptions in Java CS201, SW Development Methods.
Google App Engine using Java 1. Outline Getting started Guestbook example Todo example Simplified Madlib 2.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
The eclipse IDE IDE = “Integrated Development Environment”
Section 4: Graphs and Testing
Unit Testing.
Essentials of UrbanCode Deploy v6.1 QQ147
Unit Testing with JUnit
Testing Tutorial 7.
Eddie Jaquith Alexis Jarvis
Data Virtualization Tutorial… CORS and CIS
Single Sample Registration
Why should we test? How should we test?
JavaDoc CECS277 Mimi Opkins.
Saving, Modifying page, grammar & spell checking, and printing
Stack Data Structure, Reverse Polish Notation, Homework 7
Unit testing C# classes
GDSS – Digital Signature
How to Run a Java Program
Packages and Interfaces
Credit to Eclipse Documentation
Fundamental Error Handling
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Introduction to JUnit IT323 – Software Engineering II
CSE 403 JUnit Reading: These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
Using Lists and Functions to Create Dialogue
CS 240 – Advanced Programming Concepts
Automated test.
CMSC 202 Exceptions 2nd Lecture.
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
JUnit SWE 619 Spring 2008.
Lab 8: GUI testing Software Testing LTAT
CMSC 202 Exceptions 2nd Lecture.
Lec 17 Using Nested Loops and Objects in an Applet Class
Automated test.
Presentation transcript:

Junit Tests

Checking Current Code Coverage We use onap Sonar to track code coverage (sonar.onap.org) To see the appc coverage, click on the “appc” project on the front page (make sure you choose the most recent version of appc) From the project page, you can click on the coverage percentage to see more detail

Junit Plugin Setup The EclEmma plugin for Eclipse IDE allows you to run tests and see code coverage from within Eclipse If you go to the “Eclipse Marketplace” under the “Help” menu in Eclipse, you can search for EclEmma and install it from there Now, when you right click a class in either the Project Explorer or Outline in Eclipse, you will see the option to run “Coverage As…” > “Junit Test”

Naming Test Classes Normally, the test class should start with the word “Test”, followed by the class name you are testing One test class is created for each class you are trying to test For example, if we are testing the “RestartServer.java” class, the test class could be named “TestRestartServer.java”

Where to put test classes Test classes should be placed in the src/test/java folder of the same project that contains the code that you are testing The test classes should be in the same package as the class you are testing, but in the src/test/java folder For example: The “RestartServer.java” class is part of the appc-iaas-adapter-bundle project It can be found in the “src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl” folder Therefore, the test class should be placed in the “src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl” folder

Inside a Test Class For any test class, the “org.junit.Test” class needs to be imported Each method in your test class should test one function of the code At a minimum, you will want one method in your test class for each method of the class that you are testing If you want to test different paths or options in a method, you will want to make a different method in your test class for each of these For example, you might want to test the functionality of a method based on different input values. Each of these tests should have its own method in your test class Each test method in your test class needs the annotation “@Test” on the line before the method declaration

Naming Test Methods One way to name the methods in your test class is to start with the word “test” and then the name of the method you are testing For example, if we are testing the “executeProviderOperation()” method, the test method could be named “testExecuteProviderOperation()” For cases where you will have multiple test methods for each method being tested, one way to name these is to use an underscore after the method name For example, if we are testing the “executeProviderOperation()” method with a null input value to make sure it functions correctly, we could name this test “TestExecuteProviderOperation_NullInput()”

Checking Behavior It is not enough to simply run the code that you are testing. You want to make sure that is running correctly Junit provides several functions to do this: Import the “org.junit.Assert” class Assert.fail(“description of what failed”): This method marks the test as a failure For example, you might call this if an exception is thrown from the code that you are testing Assert.assertEquals(expected value, actual value): This method compares the value you expect to get back, with the value you actually get back If they do not match, it marks the test as a failure More examples can be found in the junit docs

Rules for Tests Tests need to run fast Each test should run in under a second A test cannot wait for a timeout or other time based event Tests should never make a connection to an external service of any kind The idea of unit tests is that they run quickly and internally in any environment The PowerMock framework should not be used Tests done using the PowerMock framework will not be counted towards the Sonar coverage results

Mocking Classes Some classes cannot be used in tests For example, a class that goes out to an OpenStack server, gets a list of its inventory, and stores it Since we can’t make connections to remote systems in junit tests, we can’t use this class in a test We can create a fake version of this class to be used during the test This will allow us to test the functionality of other classes, even though we can’t test this one class

Org.Mockito The mockito framework provides an easy way to create fake or “mocked” versions of classes We can define behavior for these mocked classes so that they can still provide correct return values to the rest of the code that we are testing After the test, we can query the mocked class and confirm whether certain methods were called on it, and if certain values were passed to it Mockito does have some limitations Return values can not be set for private methods of mocked classes

Mocking a Class Let’s say we are trying to create a mock of a class named “OpenStackProvider.java” We will need to import “org.mockito.Mockito” To create the mock: OpenStackProvider openStackProvider = Mockito.mock(OpenStackProvider.class);

Making the Mocked Class Return values By default, the mocked class will always return a default value (null for objects, 0 for ints, etc..) when a method is called on it We can change this Let’s say the OpenStackProvider class has a getProviderName() method We want to make this return a real value Import the static class (import static…) “org.mockito.Mockito.doReturn” doReturn(“Provider 1 Name”).when(openStackProvider).getProviderName(); The red name is the instance of OpenStackProvider that we created on the last slide

Verify that a method was called on the mocked class Let’s say that the OpenStackProvider class has a “public void startInstance(String instanceId)” method We want to verify that this method of our mocked version of the class was called with the parameter “instance1” Import the static class “org.mockito.Mockito.verify” verify(openStackProvider).startInstance(“instance1"); If the method startInstance was not called during the test with the “instance1” value, this will cause the test to be marked as failure