glarimy / glarimy-quiz-app

The source code and resources for Glarimy Quiz community project
0 stars 0 forks source link

Develop JUnit test cases for com.glarimy.quiz.service.SimpleScoringService #21

Closed glarimy closed 6 years ago

glarimy commented 6 years ago

Develop JUnit test cases for SimpleScoringService.

glarimy commented 6 years ago

Complete this activity by CoB tomorrow and check-in the code.

venumittapalli576 commented 6 years ago

Checked the code of junit sir

glarimy commented 6 years ago

Thanks!

glarimy commented 6 years ago

Develop the unit testcases for the following scenarios and check-in the code.

  1. The call to getCurrentScore() immediately after instantiating SimpleScoringService must return a score with 0 numberOfAttempts and 0 numberOfPoints.
  2. The call to evaluate() must return false in the following cases: 2.1 If the answer parameter is null 2.2 If the answer.questionId is 0 2.3 If the answer.ticketOption is not between 1 and 4. 2.4 If the answer.correctOption is not between 1 and 4. 2.5. If the answer.ticketOption and answer.correctOption are not the same. Otherwise it should return true;
  3. The call to getCurrentScore() immediately after calling evaluate() with the above cases 2.1, 2.2, 2.3 and 2.4 must return a score without incrementing numberOfAttempts and numberOfPoints.
  4. The call to getCurrentScore() immediately after calling evaludate() with the above case of 2.5 must return a score by incrementing numberOfAttempts but not incrementing numberOfPoints.
  5. When the call to evaluate() method returns true, a subsequent call to getCurrentScore() method must return answer by incrementing both number of points and number of attempts.

Complete this by CoB today and check-in the code.

glarimy commented 6 years ago

To give a reference, here is the implementation for the first case:

The call to getCurrentScore() immediately after instantiating SimpleScoringService must return a score with 0 numberOfAttempts and 0 numberOfPoints.

@RunWith(AndroidJUnit4.class) class SimpleScoringServiceTest {

 @test
 public void testInitialScore() {
      ScoringService service = new SimpleScoringService();
      Score score = service.getCurrentScore();
      assertTrue("invalid initial attempts", score.getNumberOfAttempted == 0);
      assertTrue("invalid initial points", score.getNumberOfPoints == 0);      
 }

}

Keep this class under androidTest/java/com/glarimy/quiz/service package Hope this helps!

venumittapalli576 commented 6 years ago

sir i have gone through the test case send by you but it is showing null pointer exception for score.getNumberofPoints and score.getNumberofAttempted in testInitialScore() but evaluate and getcurrentscore test cases are working fine. Can you guide me to find the null pointer exception case

glarimy commented 6 years ago

A test case verifies the correctness of the target code. Here the target code is SimpleScoringService and test case is SimpleScoringServiceTest. If the test case succeeds, it means the target code is correct. If the test case fails, the target code is incorrect.

Proven software engineering process suggests the following process:

  1. Write the interface with javadoc.
  2. Develop the stub implementation for the interface
  3. Write the unit test cases for the stub code
  4. Run the build. Test cases invariably fail because the stub implementation.
  5. Refactor the implementation of the target code for correctness.
  6. Run the build. Test cases passes if the target code is correct.
  7. If one or other test case fail, go back to step 5.
  8. If all test cases pass, it means target code is correct and complete.

In your case, the SimpleScoringService.getCurrentScore() seems to be returning null (because of the stub implementation). You can do this:

  1. You can add one more assertion like the following: @RunWith(AndroidJUnit4.class) class SimpleScoringServiceTest { @Test public void testInitialScore() { ScoringService service = new SimpleScoringService(); Score score = service.getCurrentScore(); assertTrue("invalid score returned", score != null); assertTrue("invalid initial attempts", score.getNumberOfAttempted == 0); assertTrue("invalid initial points", score.getNumberOfPoints == 0);
    } }

When you run this test case against the target code (with stub implementation), the test case fail, but you do not get null pointer exception. When you run the test case against the target code (with real implementation), the test case must pass. If it fails, there is problem in the target code.

Remember, the illustrations I am giving are only for reference. You need to thing through and adjust it to your actual scenarios.

venumittapalli576 commented 6 years ago

executed test cases successfully and checked in the code

glarimy commented 6 years ago

Thanks