简体   繁体   English

为核心 Java 应用程序编写 Junit 测试用例

[英]Write Junit test cases for core Java application

I have BMI calculator java applications and it can calculate BMI of user to know his/her current body weight, This program will use both Metric (kg/m) and Imperial (lb/in) BMI formula, and After getting the results, user will know his/her BMI category (underweight, obese, overweight or normal).我有 BMI 计算器 java 应用程序,它可以计算用户的 BMI 以了解他/她当前的体重,该程序将使用公制 (kg/m) 和英制 (lb/in) BMI 公式,得到结果后,用户将知道他/她的 BMI 类别(体重过轻、肥胖、超重或正常)。

I need to write Junit test cases for this application, the source code of it is shown below.我需要为这个应用程序编写 Junit 测试用例,它的源代码如下所示。 Can anyone help me to write Junit test cases for this class, please任何人都可以帮我为这个 class 编写 Junit 测试用例吗?

    public class MainSrc {

    final static double KG_TO_LBS = 2.20462;
    final static double M_TO_IN = 39.3701;
    private static DecimalFormat TWO_DECIMAL_PLACES = new DecimalFormat(".##");

    public static void main(String[] args) {
        double height, weight;

        System.out.println("The BMI Calculator by iasjem");     
        System.out.println();

        Scanner askUser= new Scanner(System.in);

        System.out.println("What is your weight (kg)?");
        weight = askUser.nextDouble();

        System.out.println("What is your height (m)?");
        height = askUser.nextDouble();

        System.out.println();
        System.out.println("Your body weight is:");
        System.out.println("In Metric units: " + TWO_DECIMAL_PLACES.format(metricFormula(height, weight)) + " kg");
        System.out.println("In Imperial units: " + TWO_DECIMAL_PLACES.format(imperialFormula(height, weight)) + " lbs");
        System.out.println("As a result, you are " + getCategory(metricFormula(height, weight)));

        askUser.close();
        System.exit(0);
    }

    // THE METRIC FORMULA
    public static double metricFormula(double m, double kg) {
        double result=0;

        result = kg / (Math.pow(m, 2));

        return result;
    }

    // THE IMPERIAL FORMULA
    public static double imperialFormula(double m, double kg) {
        double result=0;
        // convert kg to lbs and m to in
        double lbs = Math.round(kg * KG_TO_LBS);
        double in = Math.round((m * M_TO_IN) *100);

        result = (lbs / (Math.pow((in/100), 2)))* 703;

        return result;
    }

    // THE BMI CATEGORIES
    public static String getCategory (double result) {
        String category;
        if (result < 15) {
            category = "very severely underweight";
        } else if (result >=15 && result <= 16) {
            category = "severely underweight";
        } else if (result >16 && result <= 18.5) {
            category = "underweight";
        } else if (result >18.5 && result <= 25) {
            category = "normal (healthy weight)";
        } else if (result >25 && result <= 30) {
            category = "overweight";
        } else if (result >30 && result <= 35) {
            category = "moderately obese";
        } else if (result >35 && result <= 40) {
            category = "severely obese";
        } else {
            category ="very severely obese";
        }
        return category;
    }
}

Looking at the code this is something that you can use a tool such as Diffblue Cover to generate tests for you.查看代码,您可以使用 Diffblue Cover 等工具为您生成测试。 There is a free online version which will generate tests for this code, for example here are a couple of tests that are generated for the get category method:有一个免费的在线版本会为此代码生成测试,例如这里有几个为 get category 方法生成的测试:

  // Test written by Diffblue Cover.
  @Test
  public void getCategoryInputPositiveOutputNotNull6() {

    // Act and Assert result
    Assert.assertEquals("severely obese", MainSrc.getCategory(36.0));

  }

  // Test written by Diffblue Cover.
  @Test
  public void getCategoryInputPositiveOutputNotNull5() {

    // Act and Assert result
    Assert.assertEquals("very severely obese", MainSrc.getCategory(240.0));

  }

As mentioned in the comments.如评论中所述。 if you need more information about how to write unit tests for this code then please provide some specific examples of tests that you have written and aren't working.如果您需要有关如何为此代码编写单元测试的更多信息,请提供一些您已编写但未运行的测试的具体示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM