Skip to content

Nikolaevdo/hippodrome

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Related project: testing and logging

This program is an imitation of a hippodrome

To begin with, as in the previous project, fork yourself from the repository: https://github.com/pavlo-plynko/hippodrome , and clone this fork yourself.

Your task is to add testing and logging.

List of required tests

In the following list, each item must be implemented as a separate test method. When coming up with names for test methods, try to be concise, but at the same time, so that you can roughly understand what exactly is being tested in them. For example, compare these two test results:

In the second case, it is more difficult to understand which tests did not pass, and you will have to look at the logs.

1. Horse class:

  1. constructor
  • Check that when passed to the constructor as the first parameter null, will be thrown IllegalArgumentException. To do this, use the method assertThrows;
  • Check that when passed to the constructor as the first parameter null, the thrown exception will contain the message "Name cannot be null.". To do this, you need to get a message from the caught exception and use the assertEquals;
  • Check that when passing an empty string or a string containing only whitespace characters (space, tab, etc.) as the first parameter to the constructor, . will be thrown IllegalArgumentException. To test with different variants of whitespace characters, you need to make the test parameterized;
  • Check that when passing an empty string or a string containing only whitespace characters (space, tab, etc.) to the constructor as the first parameter, the thrown exception will contain the message "Name cannot be blank.";
  • Check that when passing a negative number as the second parameter to the constructor, ; will be thrown IllegalArgumentException;
  • Check that when a negative number is passed to the constructor as the second parameter, the thrown exception will contain the message "Speed cannot be negative.";
  • Check that when passing a negative number as the third parameter to the constructor, ; will be thrown IllegalArgumentException;
  • Check that when passing a negative number to the constructor as the third parameter, the thrown exception will contain the message "Distance cannot be negative.";
  1. getName method
  • Check that the method returns the string that was passed as the first parameter to the constructor;
  1. getSpeed method
  • Check that the method returns the number that was passed as the second parameter to the constructor;
  1. getDistance method
  • Check that the method returns the number that was passed as the third parameter to the constructor;
  • Check that the method returns zero if the object was created using a constructor with two parameters;
  1. move method
  • Check that the method calls inside the method getRandomDoublewith parameters 0.2 and 0.9. To do this, you need to use MockedStaticits method verify;
  • Check that the method assigns the distance value calculated by the formula: distance + speed * getRandomDouble(0.2, 0.9). To do this, you need to lock getRandomDoubleit so that it returns certain values that you need to set by parameterizing the test.

2. Hippodrome class:

  1. Constructor
  • Check that when passed to the constructor null, ; will be thrown IllegalArgumentException;
  • Check that when passed to the constructor null, the exception thrown will contain the message "Horses cannot be null.";
  • Check that when passing an empty list to the constructor, ; will be thrown IllegalArgumentException;
  • Check that when passing an empty list to the constructor, the thrown exception will contain the message "Horses cannot be empty.";
  1. getHorses method
  • Check that the method returns a list that contains the same objects and in the same sequence as the list that was passed to the constructor. When creating the Hippodrome object , pass a list of 30 different horses to the constructor;
  1. move method
  • Check that the method calls the move method on all horses. When creating the Hippodrome object, pass a list of 50 mock horses to the constructor and use the verify.
  1. getWinner method
  • Check that the method returns the horse with the largest distance value.

3. Main class

  1. main method
  • Check that the method is executed no longer than 22 seconds. To do this, use the Timeout annotation. After writing this test, disable it (use the Disabled annotation). So it will not take time to run all the tests, and if necessary, it can be run manually.

What needs to be logged

1. Main class:

  1. After creating the hippodrome object, add an entry like this to the log:2022-05-31 17:05:26,152 INFO Main: Start of the race. Number of participants: 7
  2. After displaying information about the winners, add an entry like this to the log:2022-05-31 17:05:46,963 INFO Main: Okonchaniye skachek. Pobeditelʹ: Vishnya 35 / 5 000 Translation results End of the race. Winner: Cherry

Hippodrome class:

  1. If null was passed to the constructor, then before throwing the exception, add an entry like this to the log:2022-05-31 17:29:30,029 ERROR Hippodrome: Horses list is null
  2. b. If an empty list was passed to the constructor, then before throwing the exception, add an entry like this to the log:2022-05-31 17:30:41,074 ERROR Hippodrome: Horses list is empty
  3. At the end of the constructor, add an entry like this to the log:2022-05-31 17:05:26,152 DEBUG Hippodrome: Creation Hippodrome, horse [7]

3. Horse class:

  1. If null is passed to the constructor instead of a name, then before throwing the exception, add an entry to the log like:2022-05-31 17:34:59,483 ERROR Horse: Name is null
  2. If the name passed to the constructor is empty, then before throwing the exception, add an entry like this to the log:2022-05-31 17:36:44,196 ERROR Horse: Name is blank
  3. If the speed passed to the constructor is less than zero, then before throwing the exception, add an entry like this to the log:2022-05-31 17:40:27,267 ERROR Horse: Speed is negative
  4. If the distance passed to the constructor is less than zero, then before throwing the exception, add an entry like this to the log:2022-05-31 17:41:21,938 ERROR Horse: Distance is negative
  5. At the end of the constructor, add an entry like this to the log:2022-05-31 17:15:25,842 DEBUG Horse: Creation Horse, name [Lobster], speed [2.8]

Logs should be written to the hippodrome.log file , which should be located in the project root in the logs folder . Every day the file should be renamed according to the pattern hippodrome.2021-12-31.log and a new hippodrome.log should be created instead . To do this, use the RollingFile appender . In this case, files older than 7 days should be deleted. To do this, you can use a construct like:

`'

'<Delete …>' '<IfFileName …/>' '<IfLastModified …/>'

'' ''

About

Unit testing and logging task

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%