• 10 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 1/4/21

Get some practice implementing subtraction using red-green-refactor

It's your turn!

You've finished your calculator app and placed it in the capable hands of the resident tech expert at the school. Hurrah! Only when you open your inbox, you find the following email from the product owner:

Hey,  

I hope you're doing well.  This is just to let you know that a student has reported a bug on the calculator app you created for us.  Apparently, it doesn't allow you to subtract two numbers.  Looks like we prioritized the multiplication, division, addition, and trigonometry, but forgot about subtraction! Here's what I need to you to do: 

  • Create test cases for subtraction to the CalculatorActivityTest class and calculator class.

    • These should be put in a new source class: com.openclassrooms.testing.calculator.domain.CalculatorActivityTest

  • Add test cases to a new  CalculatorServiceActivityTest class and implement the required additions in CalculatorService to support subtraction.

    • These should be put in a new source class: com.openclassrooms.testing.calculator.service.CalculatorServiceActivityTest. 

    • You will have to provide support for subtraction in the Solver interface.

You'll want to include variety of happy and sad paths, but make sure you've got at least six tests.  Also, be sure to follow the same code standards as the rest of the app. Your code should be succinct, readable, and have clear variable names. In other words, you shouldn't need comments to understand what's being done in the code.  Here are a few additional tips: 

  • Make sure you use mocking when needed. Specifically, mock your Calculator instance when writing CalculatorService tests.

  • Verify your mocks. 

  • Structure your unit tests using the AAA method and use appropriate annotations. 

  • Use a consistent naming convention that clearly labels what the test is for. 

  • Use TDD, of course! 

No need to reinvent the wheel!  Your solution should be very similar for what we've done for multiplication, division, and addition.  You find the code you need in this repository; you just need to checkout the p2-activity branch.

Sorry for the last minute job, but I'm sure you can do it! 

Thanks! 
LP

Looks like you've got your work cut out for you! Onward!

Check your work!

Check that the following elements are present:

  • mvn test  should result in all tests passing.

  • Two new test classes exist: 

    • The folder src/test/java/com/openclassrooms/domain should contain a CalculatorActivityTest class.

    • The folder src/test/java/com/openclassrooms/service should contain a CalculatorServiceActivityTest class.

  • Test cases for subtraction are added to CalculatorServiceActivityTest, and implemented in CalculatorService to support subtraction.

    • Test cases add support for subtraction in the Solver interface.

    • The Calculator collaborator is mocked with Mockito in the CalculatorService tests. 

    • The behavior of a mock is defined using when().

    • The Calculator mock is verified using verify().

  • There are a least six tests total, including at least one happy and one sad path. 

  •  @Test  annotations are used to enable test methods.

  •  @Before,  @After,  @BeforeClass, and  @AfterClass are used to set up and tear down fixtures. 

Example of certificate of achievement
Example of certificate of achievement