0% found this document useful (0 votes)
56 views11 pages

(Optional) Lab 4.1: Using Junit

This document discusses two optional labs on using JUnit and Spring testing frameworks: 1. Lab 4.1 introduces JUnit by having students set up a project to test a simple Calculator class with assertions. It has them write more tests and fix an implementation error found by testing. 2. Lab 4.2 introduces Spring testing by having students import a project and review how the test class is configured to run in the Spring context. Tests for a Catalog class are completed by fixing a test order issue with annotations. Spring Boot and Spring Test make testing Spring applications easy.

Uploaded by

darwinvargas2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views11 pages

(Optional) Lab 4.1: Using Junit

This document discusses two optional labs on using JUnit and Spring testing frameworks: 1. Lab 4.1 introduces JUnit by having students set up a project to test a simple Calculator class with assertions. It has them write more tests and fix an implementation error found by testing. 2. Lab 4.2 introduces Spring testing by having students import a project and review how the test class is configured to run in the Spring context. Tests for a Catalog class are completed by fixing a test order issue with annotations. Spring Boot and Spring Test make testing Spring applications easy.

Uploaded by

darwinvargas2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

[OPTIONAL] LAB 4.

1: USING
JUNIT In this lab you will set up and run JUnit tests

You can skip this lab if comfortable with JUnit


LAB
SYNOPSI Lab


S
Overview: In this (optional) lab, we will work with a simple
JUnit test case, including doing the following:
– Set up an IDE project to use JUnit
– Test a simple Calculator class using some basic
assertions

 Builds on previous labs: None


– The new root lab directory is workspace\Lab04.1

 Approximate Time: 15-20 minutes

Lab 4.1: Using JUnit 2


LAB PREPARATION - Lab
BRING IN PROJECT
Tasks to Perform
 Close all open files and projects

 Import an existing Maven project called Lab04.1 into the


workspace
– File | Import … | Maven | Existing Maven Projects
– Click Next, Browse to the workspace\Lab04.1 folder, click
Finish
– There is no need to add Java libraries to this project
– Maven will pull in all dependencies needed via pom.xml

 Open pom.xml for review


– We bring in the JUnit dependency, and that's about it
– We don't need Spring or Spring Boot - but maven is useful even in this
simple situation
– It pulls in a number of dependencies that JUnit has Lab 4.1: Using JUnit 3
REVIEW LAB CLASSES Lab
AND RUN TESTS
Tasks to Perform
 Note the structure of the project and the names of the classes
– Parallel src and test directories
– Test classes and business classes in same package

 Review the Calculator class – yes, it's dumb-simple BUT:


– Understand its API and what to expect from its methods
• This forms the basis for your test cases
– This simple class gives us a good example to start with

 Review the CalculatorTest test class


– Run its test cases (methods) via right-click €
Run As € JUnit Test
– Eclipse JUnit view appears, showing a green bar

Lab 4.1: Using JUnit 4


WRITE/RUN Lab
MORE TESTS
Tasks to Perform
 Write additional test methods for the Calculator class,
following the example testAddPositive() method
– testDividePositive(): Use 5/2 for your test
• Use the 3-argument assertEquals(expected, actual, delta) method
(because of the double return type) – for example:

assertEquals(2.5, calc.divide(5, 2), .001);

– testIsEvenPositive()
• Use assertTrue(), passing in a should-be-true condition – for example:

assertTrue(calc.isEven(10));

 NOTE: tests run in isolation, and in no guaranteed order (1)


– You need to create a new Calculator object in each test

 Run the tests as before: right-click € Run As € JUnit


Test Lab 4.1: Using JUnit 5
WHAT Lab
GIVES Tasks to Perform
 You should have seen a failure in one of the tests (divide)
– Look at Calculator.divide()- the arguments are int
– int / int result is an int - so what happens if you have a remainder
• It gets truncated
– Change the implementation to the below
return 1.0 * a / b; // Convert to double, then divide

 Run the tests as before: right-click € Run As € JUnit


Test
– They should all pass
– You can see, that even in a small class, there is room for error
– That's why we test !
 [Optional] Add a negative test and run it
– Note: You can run a single test in a test class also
STOP
• Expand the test class in Package Explorer, right click on a
single method, select Run As | JUnit Test Lab 4.1: Using JUnit 6
LAB 4.2: SPRING TESTING
In this lab, we will work with the features of
Spring Testing
LAB
SYNOPSI Lab


S
Overview: In this lab, we will work with Spring Testing
– We'll review the POM for it (Spring Boot-based)
– We'll integrate our test class with a Spring context

 Builds on previous labs: None


– The new root lab directory is workspace\Lab04.2

 Approximate Time: 20-30 minutes

Lab 4.2: Spring Testing 8


LAB PREPARATION - Lab
BRING IN PROJECT
Tasks to Perform
 Close all open files and projects

 Import an existing Maven project called Lab04.2 into the


workspace
– File | Import … | Maven | Existing Maven Projects
– Click Next, Browse to the workspace\Lab04.2 folder, click
Finish
– There is no need to add Java libraries to this project
– Maven will pull in all dependencies needed via pom.xml

 Open pom.xml for review


– We bring in the spring-boot-starter-test, and that's
about it !
– Look at the Dependency Hierarchy tab to see what is brought
in
Lab 4.2: Spring Testing 9
REVIEW AND
COMPLETE TEST Lab

CLASS Tasks to Perform


 Briefly review the types under the src folder
– They are the JavaTunes types we've seen earlier

 Open CatalogTest (com.javatunes.service under test


folder)
– Annotate the class to set the JUnit runner to Spring's custom runner
– Annotate the class to read the config class SpringConfig.class
• Use the annotation that initializes Spring Boot
• And note how we inject a Catalog into the test class
– We used a JUnit annotation to set the test method execution order (1)
– It runs the methods in alphabetical order (test1…, test2… , test3…)
 Run the CatalogTest test methods
– Right-click | Run As | JUnit Test
– Hmm - some failures again - let's see what's up
Lab 4.2: Spring Testing 10
FIX TEST Lab
CLASS Tasks to Perform
 What's the problem?
– test3_testSizePositive() tests the number of items in the repo
• We know what that should be in this repository
– However, test2_testDeletePositive() deletes an item
• And it runs before test3 (because we've set it up this way just for this)

 Annotate test2_testDeletePositive() to indicate that the


context should be recreated after this method runs
– See the manual slides for the correct annotation
 Run the CatalogTest test methods again
– Right-click | Run As | JUnit Test - They should run without failure
now
 Spring Boot and Spring Test make it very easy to do testing
– Create your test class to use the capabilities and you're set
– You can do everything the rest of your Spring program can Lab 4.2: Spring Testing 11

You might also like