[SOLVED] TQS Lab 1- Unit testing

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon lab01-vdehlf.zip (21.2 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (1 vote)

Lab 1

Maven configured to run in the command line. Check with: $ mvn –version

Java capable IDE, such as IntelliJ IDEA (version “Ultimate” suggested) or VS Code. Unit testing (with JUnit 5)

Learning objectives

  • ⎯  Identify relevant unit tests to verify the contract of a module.
  • ⎯  Write and execute unit tests using the JUnit framework.
  • ⎯  Link the unit tests results with further analysis tools (e.g.: code coverage)

    Key points

  • ●  Unit testing is when you (as a programmer) write test code to verify units of (production) code. A unit is a small, coherent subset of a much larger solution. A true “unit” should not depend of the behavior of other (collaborating) modules.
  • ●  Unit tests help the developers to (i) understand the module contract (what to construct); (ii) document the intended use of a component; (iii) prevent regression errors; (iv) increase confidence in the code.
  • ●  JUnit and TestNG are popular frameworks for unit testing in Java.

1.1 Stack contract

In this exercise, you will implement a stack data structure (TqsStack) with appropriate unit tests. Be sure to adopt a write-the-tests-first workflow:

a) Create a new project (maven-based, Java standard application). You may need to update the Java version in the POM.xml:

         <properties>
             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

2 | TQS LABS

45426 Teste e Qualidade de Software

        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    </properties>
  1. b)  Add the required dependencies to run Junit 5 tests1. Here are some sources:
    • –  Adapt from the quick start project for Maven2.
    • –  Adapt from this tutorial.
  2. c)  Create the required class definition (just the “skeleton”, do not implement the methods body yet; you may need to add dummy return values). The code should compile, but the implementation is yet incomplete.
  3. d)  Write the unit tests that will verify the TqsStack contract.
    You may use the IDE features to generate the testing class; note that the IDE support will vary. Be

    sure to use JUnit 5.x.

    Your tests will verify several assertions that should evaluate to true for the test to pass.

  4. e)  Run the tests and prove that TqsStack implementation is not valid yet (the tests should fail for now,

    the first step in Red-Green-Refactor).

  5. f)  Correct/add the missing implementation to the TqsStack;
  6. g)  Run the unit tests.
  7. h)  Iterate from steps d) to f) and confirm that all tests pass.

Suggested stack contract:

  • ⎯  push(x): add an item on the top
  • ⎯  pop: remove the item at the top
  • ⎯  peek: return the item at the top (without removing it)
  • ⎯  size: return the number of items in the stack
  • ⎯  isEmpty: return whether the stack has no items

    What to test3:

  1. a)  A stack is empty on construction.
  2. b)  A stack has size 0 on construction.
  3. c)  After n pushes to an empty stack, n > 0, the stack is not empty and its size is n
  4. d)  If one pushes x then pops, the value popped is x.
  5. e)  If one pushes x then peeks, the value returned is x, but the size stays the same
  6. f)  If the size is n, then after n pops, the stack is empty and has a size 0
  7. g)  Popping from an empty stack does throw a NoSuchElementException [You should test for the Exception occurrence]
  8. h)  Peeking into an empty stack does throw a NoSuchElementException
  9. i)  For bounded stacks only: pushing onto a full stack does throw an IllegalStateException

1 If using IntelliJ: you may skip this step and ask, later, the IDE to fix JUnit imports.
2 Delete the “pom-SNAPSHOT.xml”, if you are cloning the project to use as a quick starter. 3 Adapted from http://cs.lmu.edu/~ray/notes/stacks/

TQS LABS | 3

1.2 EuroMillions

Let us consider the “Euromilhões” use case.

2a/ Pull the “euromillions-play” project and correct the code (or the tests themselves, if needed) to have the existing unit tests passing.

For the (failing) test:

testFormat

testConstructorFromBadArr ays

You should:

Correct the implementation of Dip#format so the tests pass.

Implement new test logic to confirm that an exception will be raised if the arrays have invalid numbers (wrong count of numbers or stars)

Note: you may suspend temporary a test with the @Disabled tag (useful while debugging the tests themselves).

2b/ The sample project was prepared for the scenario in which the range for the “stars” was 1..10. However, the rules have changed, and the range is 1..12.
Be sure to include a test to verify the [new] ranges.

2c/ Note that the code provided includes “magic numbers” (2 for the number of stars, 50 for the max range in numbers, [was] 10 for the max range in starts…). Refactor the code to extract constants and eliminate the “magic numbers”.

2d/ The class SetOfNaturals represents a set (no duplicates should be allowed) of integers, in the range [1, +∞]. Some basic operations are available (add element, find the intersection…).

4 | TQS LABS

45426 Teste e Qualidade de Software

What kind of unit test are worth writing for the entity SetOfNaturals? Complete the project, adding the new tests you identified.

2e/ Assess the coverage level in project “Euromillions-play”.
Configure the maven project to run Jacoco analysis.
Run the maven “test” goal and then “jacoco:report” goal. You should get an HTML report under target/jacoco.
$ mvn clean test jacoco:report

Interpret the results accordingly. Which classes/methods offer less coverage? Are all possible decision branches being covered?

Note: IntelliJ has an integrated option to run the tests with the coverage checks (without setting the Jacoco plugin in POM). But if you do it at Maven level, you can use this feature in multiple tools.

Troubleshooting some frequent errors

➔ “Test are run from the IDE but not from command line.”
Be sure to configure the Surefire plug-in in Maven (example).

Explore

● JetBrains Blog on Writing JUnit 5 tests (with vídeo).
● Book: JUnit in Action. Note that you can access it from the OReilly on-line library.

● JUnit 5 cheat sheet.
● Vogel’s tutorial on JUnit. Useful to compare between JUnit 4 and JUnit 5.

  • lab01-vdehlf.zip