SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
COMP23420 Sem 2 week 7
                          Unit testing and JUnit
                                                     John Sargeant
                                                  johns@cs.man.ac.uk


                         REMINDER: PLEASE ENSURE YOUR PHONE IS
                              SWITCHED OFF DURING LECTURES

C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Overview

•       Introduction
•       Java features for Junit 4
•       JUnit 4
•       Example unit tests




C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Introduction
• Reminder: unit testing is testing one unit (class) at a
        time – although most classes depend on other
        classes
• Can be black-box (based on the javadoc) or white
        box (based on the code).
• In most cases black-box is better.
• JUnit is a Java framework for unit testing
• JUnit 4 is a big improvement on previous versions,
        but requires some Java features you may not know.


C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Static import
Added in Java 1.5. Reminder: a normal import, e.g.
import java.util.Calendar;
Allows us to refer just to Calendar rather than having to
   say java.util.Calendar; every time.


Similarly a static import.e.g.
import static java.util.Calendar;
Allows us to refer to all the static features of the
   Calendar class by their short names, e.g.
   DAY_OF_WEEK rather than Calendar.DAY_OF_WEEK
C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Annotations
• Also added in Java 1.5. Ignored by the compiler, but
        intended to be used by other tools.
• E.g. suppose you have a tool which reminds you of
        programming tasks you need to do:

@Remind(period=weekly)
public Driver pickOptimumDriver() {
       return null; // Not yet implemented
}
Annotation applies to the immediately following class or
  method.
C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Reflection
• The ability to represent programming language
        constructs such as classes and methods within the
        language itself.
• “Not a first year topic” – JTL.
• Instances of the class called Class represent classes.
• Can be obtained with .class, e.g. Calendar.class
        gives an object representing the Calendar class.
• Can get mind-bending, e.g. Class.class is an
        instance of class Class which represents the class
        Class.

C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Example code to test
public class Sorter {
    private int[] _numbers;

               public Sorter(int[] numbers) {
                   _numbers = numbers;
               }

               public int[] getNumbers() {
                   return _numbers;
               }

               public void sort() {
                 // Not implemented yet
               }
}om VictoriathUenive rs ity os fof U M ISeTs te r
C
Th e
     b ining     s tre ngth
                                 M anch
                                              and
Test class (1)
import org.junit.*;
import static org.junit.Assert.*;

public class TestSorter {
   private static Sorter _sorter;
   private static boolean isSorted(int[] array) { …. }

      @Before
      public void setUp() {
         _sorter = new Sorter(new int[] { 3, 7, 1, 4, 6 })
      }



C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Test class (2)
          @Test
          public void testUnsorted() {
                     assertFalse(isSorted(_sorter.getNumbers()));
          }


              @Test
              public void testSorted() {
                     _sorter.sort();
                     assertTrue(isSorted(_sorter.getNumbers()));
              }
}

C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
What happens
The first test succeeds, the second fails, giving an
  AssertionError.
Now if we actually implement sorting:

public void sort() {
        Arrays.sort(_numbers);
}


Both tests should succeed.


C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
@Before and @BeforeClass
• Code run before or after a test to set things up to tidy
        up afterwards is called a Fixture.
• @Before indicates a fixture to be run before every
  test. The setup method which follows must not be
  static.
• @BeforeClass indicates a fixture to be run just once.
  The setup method must be static.
• @After and @AfterClass are the same for code run
  afterwards.


C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Running tests
The test class has no main() so how do we run it?


Either from the command line, with the JUnit Jar file on
    the CLASSPATH:
java org.junit.runner.JUnitCore TestClass


Or from within an IDE, e.g. in Netbeans, right click on
   the Java file, select Tools -> JUnit (make sure you
   select JUnit 4).

C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Test suites
• Often we want to run a bunch of tests together – a
        test suite. This is done with the @RunWith annotation
        and the test classes to use listed using reflection:

@Runwith(Suite.class)
@SuiteClasses(DepartureTimesTest.class,
  RosterOrderingTest.class,
  DriverAllocationTest.class)
public class RosterTest() {
}


C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Other things you can do
JUnit 4 allows you to other good things such as:


• Parameterise tests so you can run the same test
        many times with different parameters
• Test the exception handling behaviour of your code.

More information at JUnit,org and there are many
  tutorials on the web (make sure it’s JUnit 4, not 3).


C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Black box vs. White Box
• JUnit test classes are separate from the classes they
        are testing, so can only use the public interface
• If you have access to the private stuff, you could
        make use of that knowledge..
• E.g. an amount of Money represented as an int –
        won’t work for large numbers
• But generally Black Box testing is preferable – also
        helps to clarify requirements.
• E.g. testing for large amounts is an obvious thing to
        do anyway.
C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
General testing hints
• Real-world data (e.g. 358 timetable) is messy – check
        that you’ve covered all the cases.
• Test small parts before assembling them into bigger
        parts – make sure the easy tests get done.
• Make sure known bugs and things not yet
        implemented are documented using e.g. Bugzilla.
• When doing system testing, use a variety of real
        users – people will use it in ways you don’t expect.
• Don’t assume – check!

C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r
Weeks 8-11 (Provisional)


• Weeks 8 - 10 Software Architecture design (LZ)
• Week 11 review of second semester (JS/LZ)
• ~ 1 week before the exam: exam revision session
        (JS/LZ). Mock online exam will also be available.




C om b ining th e s tre ngth s of U M IS T and
Th e Victoria U nive rs ity o f M anch e s te r

Weitere ähnliche Inhalte

Was ist angesagt?

Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalorerajkamaltibacademy
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest modulePyCon Italia
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 
Junit Recipes - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)Junit Recipes  - Elementary tests (1/2)
Junit Recipes - Elementary tests (1/2)Will Shen
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classesTech_MX
 

Was ist angesagt? (20)

Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest module
 
Control statements
Control statementsControl statements
Control statements
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
study of java
study of java study of java
study of java
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Java
JavaJava
Java
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Junit Recipes - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)Junit Recipes  - Elementary tests (1/2)
Junit Recipes - Elementary tests (1/2)
 
3.C#
3.C#3.C#
3.C#
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classes
 

Ähnlich wie Week7 unit-testing (20)

Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Junit
JunitJunit
Junit
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
Unit testing 101
Unit testing 101Unit testing 101
Unit testing 101
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
Junit
JunitJunit
Junit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Esem2014 presentation
Esem2014 presentationEsem2014 presentation
Esem2014 presentation
 
Junit
JunitJunit
Junit
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Advance unittest
Advance unittestAdvance unittest
Advance unittest
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Test ng
Test ngTest ng
Test ng
 

Kürzlich hochgeladen

Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataSafe Software
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncObject Automation
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
20200723_insight_release_plan
20200723_insight_release_plan20200723_insight_release_plan
20200723_insight_release_planJamie (Taka) Wang
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 

Kürzlich hochgeladen (20)

Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation Inc
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
20200723_insight_release_plan
20200723_insight_release_plan20200723_insight_release_plan
20200723_insight_release_plan
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 

Week7 unit-testing

  • 1. COMP23420 Sem 2 week 7 Unit testing and JUnit John Sargeant johns@cs.man.ac.uk REMINDER: PLEASE ENSURE YOUR PHONE IS SWITCHED OFF DURING LECTURES C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 2. Overview • Introduction • Java features for Junit 4 • JUnit 4 • Example unit tests C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 3. Introduction • Reminder: unit testing is testing one unit (class) at a time – although most classes depend on other classes • Can be black-box (based on the javadoc) or white box (based on the code). • In most cases black-box is better. • JUnit is a Java framework for unit testing • JUnit 4 is a big improvement on previous versions, but requires some Java features you may not know. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 4. Static import Added in Java 1.5. Reminder: a normal import, e.g. import java.util.Calendar; Allows us to refer just to Calendar rather than having to say java.util.Calendar; every time. Similarly a static import.e.g. import static java.util.Calendar; Allows us to refer to all the static features of the Calendar class by their short names, e.g. DAY_OF_WEEK rather than Calendar.DAY_OF_WEEK C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 5. Annotations • Also added in Java 1.5. Ignored by the compiler, but intended to be used by other tools. • E.g. suppose you have a tool which reminds you of programming tasks you need to do: @Remind(period=weekly) public Driver pickOptimumDriver() { return null; // Not yet implemented } Annotation applies to the immediately following class or method. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 6. Reflection • The ability to represent programming language constructs such as classes and methods within the language itself. • “Not a first year topic” – JTL. • Instances of the class called Class represent classes. • Can be obtained with .class, e.g. Calendar.class gives an object representing the Calendar class. • Can get mind-bending, e.g. Class.class is an instance of class Class which represents the class Class. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 7. Example code to test public class Sorter { private int[] _numbers; public Sorter(int[] numbers) { _numbers = numbers; } public int[] getNumbers() { return _numbers; } public void sort() { // Not implemented yet } }om VictoriathUenive rs ity os fof U M ISeTs te r C Th e b ining s tre ngth M anch and
  • 8. Test class (1) import org.junit.*; import static org.junit.Assert.*; public class TestSorter { private static Sorter _sorter; private static boolean isSorted(int[] array) { …. } @Before public void setUp() { _sorter = new Sorter(new int[] { 3, 7, 1, 4, 6 }) } C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 9. Test class (2) @Test public void testUnsorted() { assertFalse(isSorted(_sorter.getNumbers())); } @Test public void testSorted() { _sorter.sort(); assertTrue(isSorted(_sorter.getNumbers())); } } C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 10. What happens The first test succeeds, the second fails, giving an AssertionError. Now if we actually implement sorting: public void sort() { Arrays.sort(_numbers); } Both tests should succeed. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 11. @Before and @BeforeClass • Code run before or after a test to set things up to tidy up afterwards is called a Fixture. • @Before indicates a fixture to be run before every test. The setup method which follows must not be static. • @BeforeClass indicates a fixture to be run just once. The setup method must be static. • @After and @AfterClass are the same for code run afterwards. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 12. Running tests The test class has no main() so how do we run it? Either from the command line, with the JUnit Jar file on the CLASSPATH: java org.junit.runner.JUnitCore TestClass Or from within an IDE, e.g. in Netbeans, right click on the Java file, select Tools -> JUnit (make sure you select JUnit 4). C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 13. Test suites • Often we want to run a bunch of tests together – a test suite. This is done with the @RunWith annotation and the test classes to use listed using reflection: @Runwith(Suite.class) @SuiteClasses(DepartureTimesTest.class, RosterOrderingTest.class, DriverAllocationTest.class) public class RosterTest() { } C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 14. Other things you can do JUnit 4 allows you to other good things such as: • Parameterise tests so you can run the same test many times with different parameters • Test the exception handling behaviour of your code. More information at JUnit,org and there are many tutorials on the web (make sure it’s JUnit 4, not 3). C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 15. Black box vs. White Box • JUnit test classes are separate from the classes they are testing, so can only use the public interface • If you have access to the private stuff, you could make use of that knowledge.. • E.g. an amount of Money represented as an int – won’t work for large numbers • But generally Black Box testing is preferable – also helps to clarify requirements. • E.g. testing for large amounts is an obvious thing to do anyway. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 16. General testing hints • Real-world data (e.g. 358 timetable) is messy – check that you’ve covered all the cases. • Test small parts before assembling them into bigger parts – make sure the easy tests get done. • Make sure known bugs and things not yet implemented are documented using e.g. Bugzilla. • When doing system testing, use a variety of real users – people will use it in ways you don’t expect. • Don’t assume – check! C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r
  • 17. Weeks 8-11 (Provisional) • Weeks 8 - 10 Software Architecture design (LZ) • Week 11 review of second semester (JS/LZ) • ~ 1 week before the exam: exam revision session (JS/LZ). Mock online exam will also be available. C om b ining th e s tre ngth s of U M IS T and Th e Victoria U nive rs ity o f M anch e s te r