Issue
I want to assing an enum value in test method to check if the calculation method works correctly.
My unum code is like :
public enum ScoreCalculationEnum {
SCE_MTS100("100 Mts", new BigDecimal("25.4347"), new BigDecimal("18"), new BigDecimal("1.81")) {
@Override
public BigDecimal toDoCalculate(String scoreEvent) {
CalculationService cs = new CalculationService();
return cs.calculateForTrackEvent(DistanceAndTimeConvertingService.getSecond(scoreEvent), this);
}
},
SCE_LONGJUMP("Long Jump", new BigDecimal("0.14354"), new BigDecimal("220"), new BigDecimal("1.4")) {
@Override
public BigDecimal toDoCalculate(String scoreEvent) {
CalculationService cs = new CalculationService();
return cs.calculateForFieldEvent(DistanceAndTimeConvertingService.getCentimeters(scoreEvent), this);
}
}
}
and my method is:
public BigDecimal calculateForTrackEvent(BigDecimal P, ScoreCalculationEnum score) {
return blabla;
}
And My test code is:
@DisplayName("Score calculation test for track event")
@Test
public void calculateForTrackEvent() {
BigDecimal P = new BigDecimal(12.609999656677246);
ScoreCalculationEnum scoreCalculationEnum = new ScoreCalculationEnum (new BigDecimal("25.4347"), new BigDecimal("18"), new BigDecimal("1.81"));
}
In my test method I could not assing this enum value to call the original method. How can I change this assinging line;
ScoreCalculationEnum scoreCalculationEnum = new ScoreCalculationEnum (new BigDecimal("25.4347"), new BigDecimal("18"), new BigDecimal("1.81"));
I got "ScoreCalculationEnum is abstract and cannot be instantiated" error.
Solution
Enums are always defined at compile time, you cannot create new ones dynamically. If you want to test that calculateForTrackEvent method, you will need to call the enum that you have declared for that:
SCE_MTS100.toDoCalculate(score);
Answered By - Harry Jones