This program is an imitation of a hippodrome
To begin with, as in the previous projects, make a fork from the repository: https://github.com/CodeGymCC/hippodrome, and then clone it to your computer.
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:
On the right side of the above example, it is more difficult to understand what kind of tests did not pass, and you will have to look at the logs.
1. Horse class:
- constructor
- Check that an
IllegalArgumentException
is thrown whennull
is passed to the constructor as the first parameter. To do this, use theassertThrows
method; - Check that when
null
is passed to the constructor as the first parameter, the thrown exception contains the "Name cannot be null." message. To do this, you need to get a message from the caught exception and useassertEquals
. The test should fail if no exception is thrown; - Check that an
IllegalArgumentException
is thrown when an empty string or a string containing only whitespace characters (space, tab, etc.) is passed as the first parameter to the constructor. To test with different variants of whitespace characters, you need to make the test parameterized; - Check that when an empty string or a string containing only whitespace characters (space, tab, etc.) is passed to the constructor as the first parameter, the thrown exception contains the "Name cannot be blank." message. The test should fail if no exception is thrown;
- Check that an
IllegalArgumentException
is thrown when a negative number is passed to the constructor as the second parameter; - Check that when a negative number is passed to the constructor as the second parameter, the thrown exception contains the "Speed cannot be negative." message;
- Check that an
IllegalArgumentException
is thrown when a negative number is passed to the constructor as the third parameter; - Check that when passing a negative number to the constructor as the third parameter, the thrown exception contains the "Distance cannot be negative." message;
- getName method
- Check that the method returns the string that was passed to the constructor as the first parameter;
- getSpeed method
- Check that the method returns the number that was passed to the constructor as the second parameter;
- getDistance method
- Check that the method returns the number that was passed to the constructor as the third parameter;
- Check that the method returns zero if the object was created using a constructor with two parameters;
- move method
- Check that the
getRandomDouble
method is called inside the move method with parameters 0.2 and 0.9. To do this, you need to useMockedStatic
and itsverify
method; - 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 mock thegetRandomDouble
method so that it returns certain values that you need to set by parameterizing the test.
2. Hippodrome class:
- Constructor
- Check that an
IllegalArgumentException
is thrown whennull
is passed to the constructor; - Check that when
null
is passed to the constructor, the thrown exception contains the "Horses cannot be null." message; - Check that an
IllegalArgumentException
is thrown when an empty list is passed to the constructor; - Check that when an empty list is passed to the constructor, the thrown exception contains the "Horses cannot be empty." message;
- getHorses method
- Check that the method returns a list that contains the same objects and in the same order 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. Use the
verify
method to check that method move was called for every horse. - getWinner method
- Check that the method returns the horse with the largest distance value.
3. Main class
- 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). This way it will not take up time when running all the tests, and if necessary, it can be run manually.
What needs to be logged
1. Main class:
- 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: End of the race. Winner: Cherry
2. Hippodrome class:
- 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
- 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: Created a Hippodrome with [7] horses
3. Horse class:
- If null is passed to the constructor instead of a name, then before throwing the exception, add an entry like this to the log:
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: Created a Horse named [Lobster] with 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 this:
<DefaultRolloverStrategy>
<Delete …>
<IfFileName …/>
<IfLastModified …/>
</Delete>
</DefaultRolloverStrategy>
Google what to substitute the three dots with.😊
Project analysis . Watch after completion!
GO TO FULL VERSION