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.
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.
- constructor
- Check that when passed to the constructor as the first parameter
null
, will be thrownIllegalArgumentException
. To do this, use the methodassertThrows
; - 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 theassertEquals
; - 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.";
- getName method
- Check that the method returns the string that was passed as the first parameter to the constructor;
- getSpeed method
- Check that the method returns the number that was passed as the second parameter to the constructor;
- 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;
- move method
- Check that the method calls inside the method
getRandomDouble
with parameters 0.2 and 0.9. To do this, you need to useMockedStatic
its methodverify
; - 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 lockgetRandomDouble
it so that it returns certain values that you need to set by parameterizing the test.
- Constructor
- Check that when passed to the constructor
null
, ; will be thrownIllegalArgumentException
; - 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.";
- 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;
- 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
.
- getWinner method
- Check that the method returns the horse with the largest distance value.
- 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.
- 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
- 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
- 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
- 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
- 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]
- 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
- 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
- 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
- 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
- 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 …/>'
'' ''