Przeglądaj źródła

Merge branch 'main' into master

master
bart.de.lepeleer@breedbeeld.cc 2 lat temu
rodzic
commit
726fda1ff6

+ 23
- 0
src/main/java/com/javafanatics/ActivityCalculator.java Wyświetl plik

@@ -0,0 +1,23 @@
package com.javafanatics;

public class ActivityCalculator {

private static final int WORKOUT_DURATION_MIN = 45;

public static String rateActivityLevel(int weeklyCardioMinutes, int weeklyWorkoutSessions) {

if (weeklyCardioMinutes <0 || weeklyWorkoutSessions <0) {
throw new RuntimeException("Input below 20");
}

int totalMinutes = weeklyCardioMinutes + weeklyWorkoutSessions * WORKOUT_DURATION_MIN;
double avgDailyActivityMinutes = totalMinutes / 7.0;

if (avgDailyActivityMinutes < 20) {
return "bad";
} else if (avgDailyActivityMinutes < 40) {
return "average";
}
return "good";
}
}

+ 70
- 0
src/test/java/com/javafanatics/ActivityCalculatorTest.java Wyświetl plik

@@ -0,0 +1,70 @@
package com.javafanatics;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;


import static org.junit.jupiter.api.Assertions.*;

class ActivityCalculatorTest {

@Test
void should_ReturnBad_When_AgeBelow20(){
//given
int weeklyCardioMinutes = 40;
int weeklyWorkouts = 1;

//when
String actual = ActivityCalculator.rateActivityLevel(weeklyCardioMinutes, weeklyWorkouts);

//then
assertEquals("bad", actual);
}

@Test
void should_ReturnAverage_When_AgeBetween20and40(){
//given
int weeklyCardioMinutes = 40;
int weeklyWorkouts = 3;

//when
String actual = ActivityCalculator.rateActivityLevel(weeklyCardioMinutes, weeklyWorkouts);

//then
assertEquals("average", actual);

}

@Test
void should_ReturnGood_When_AgeAbove40(){
//given
int weeklyCardioMinutes = 40;
int weeklyWorkouts = 7;

//when
String actual = ActivityCalculator.rateActivityLevel(weeklyCardioMinutes, weeklyWorkouts);

//then
assertEquals("good", actual);

}

@Test
void should_ThrowException_When_InputBelowZero(){
//given
int weeklyCardioMinutes = -40;
int weeklyWorkouts = 7;

//when
Executable executable = () -> ActivityCalculator.rateActivityLevel(weeklyCardioMinutes, weeklyWorkouts);


//then
assertThrows(RuntimeException.class, executable);

}




}

+ 0
- 21
src/test/java/com/javafanatics/AppTest.java Wyświetl plik

@@ -1,21 +0,0 @@
package com.javafanatics;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;


/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

+ 157
- 0
src/test/java/com/javafanatics/BMICalculatorTest.java Wyświetl plik

@@ -0,0 +1,157 @@
package com.javafanatics;

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class BMICalculatorTest {

private String environment="prod";

@BeforeAll
static void beforeAll() {
System.out.println("Run expensive operations before executing all tests, eg start servers, open db connections");
}

@AfterAll
static void afterAll() {
System.out.println("After all unit tests clean up this mess");
}

@Nested
class isDietRecommendedTests {
@ParameterizedTest(name ="weight={0},height={1}")
@CsvFileSource(resources ="/diet-recommended-input-data.csv", numLinesToSkip = 1)
void should_ReturnTrue_When_DietRecommended(Double coderWeight, Double coderHeight) {

//given
double weight = coderWeight;
double height = coderHeight;

//when
boolean recommended = BMICalculator.isDietRecommended(weight,height);

//then
assertTrue(recommended);
}

@Test
void should_ReturnFalse_When_DietNotRecommended() {

//given
double weight = 50.0;
double height = 1.72;

//when
boolean recommended = BMICalculator.isDietRecommended(weight,height);

//then
assertFalse(recommended);
}

@Test
void should_ThrowArithmeticException_When_HeightZero() {

//given
double weight = 50.0;
double height = 0.0;

//when
Executable executable = () -> BMICalculator.isDietRecommended(weight,height);

//then
assertThrows(ArithmeticException.class, executable);
}
}


@Nested
@DisplayName(">>>>> sample inner class display name")
class findCoderWithWorstBMI {
@Test
@DisplayName(">>>>> sample method display name")
@DisabledOnOs(OS.LINUX)
void should_ReturnCoderWithWorstBMI_When_CoderlistNotEmpty() {
//given
List<Coder> coders = new ArrayList<>();
coders.add(new Coder(1.80,60.0));
coders.add(new Coder(1.82,98.0));
coders.add(new Coder(1.82,64.7));

//when
Coder coderWorstBMI = BMICalculator.findCoderWithWorstBMI(coders);

//then
assertAll (
() -> assertEquals(1.82, coderWorstBMI.getHeight()),
() -> assertEquals(98, coderWorstBMI.getWeight())
);
}

@Test
void should_returnCoderWithWorstBMIIn1Ms_When_CoderListHas10000Elements() {

//given
assumeTrue(BMICalculatorTest.this.environment.equals("prod"));
List<Coder> coders=new ArrayList<>();
for(int i=0;i<10000;i++) {
coders.add(new Coder(1.0+i, 10.0+i));
}

//when
Executable executable =() ->BMICalculator.findCoderWithWorstBMI(coders);

//then
assertTimeout(Duration.ofMillis(500), executable);
}



@Test
void should_ReturnCorrectBMIScoreArray_When_CoderListNotEmpty() {
//given
List<Coder> coders = new ArrayList<>();
coders.add(new Coder(1.80,60.0));
coders.add(new Coder(1.82,98.0));
coders.add(new Coder(1.82,64.7));
double[] expected = {18.52,29.59,19.53};

//when
double[] bmiScores = BMICalculator.getBMIScores(coders);

//then
//Do not use assertEquals -> compares objects
assertArrayEquals(expected,bmiScores);

}
}

@Nested
class getBmiScores {

@Test
void should_ReturnNullWithWorstBMI_When_CoderlistEmpty() {
//given
List<Coder> coders = new ArrayList<>();

//when
Coder coderWorstBMI = BMICalculator.findCoderWithWorstBMI(coders);

//then
assertNull(coderWorstBMI);
}

}
}

+ 43
- 0
src/test/java/com/javafanatics/DietPlannerTest.java Wyświetl plik

@@ -0,0 +1,43 @@
package com.javafanatics;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class DietPlannerTest {

private DietPlanner dietPlanner;

@BeforeEach
void setup() {
this.dietPlanner = new DietPlanner(20,30,50);
}

@AfterEach
void afterEach() {
System.out.println("A unit test was finished.");
}


@RepeatedTest(value = 10, name = RepeatedTest.LONG_DISPLAY_NAME )
void should_ReturnCorrectDietPlan_When_CorrectCoder() {
//given
Coder coder=new Coder(1.82, 75.0, 26, Gender.MALE);
DietPlan expected = new DietPlan(2202, 110,73,275);

//when
DietPlan actual=dietPlanner.calculateDiet(coder);

//then
assertAll(
() ->assertEquals(expected.getCalories(), actual.getCalories()),
() ->assertEquals(expected.getProtein(), actual.getProtein()),
() ->assertEquals(expected.getFat(), actual.getFat()),
() ->assertEquals(expected.getCarbohydrate(), actual.getCarbohydrate())
);
}

}

+ 8
- 0
src/test/resources/diet-recommended-input-data.csv Wyświetl plik

@@ -0,0 +1,8 @@
weight,height
82.0,1.68
85.0,1.69
89.0,1.72
95.0,1.75
110.0,1.78
120.0,1.89
125.5,1.92

Ładowanie…
Anuluj
Zapisz