你会如何为这段代码编写Junit测试?

时间:2015-08-22 11:23:18

标签: java testing junit

我是java新手并练习编码,如何在不改变的情况下为此代码编写一些Junit测试?我想写一些Junits来查看输出是否正确。有人能提供一个这样的例子吗? 非常感谢任何帮助。

package returnOnInvestment;

import java.util.Scanner;

/**
   This program compares CD /Investment plans input by the year
   broken down by the requirements below:

   This program creates a table of compound interest investment growth over time
   Broken down by: a) year b) balance at end of year
   Finance formula of A= P(1+ r/n)^n*t is used:
   A = Future Value         | P = Initial Investment
   r = annual interest rate |n = times interest is compounded/year
   t = years invested
*/ 

public class BestInvesment
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        String bestBankName = "";
        double bestGrowth = 0;
        boolean done = false;

        while(!done)
        {
            System.out.print("Plan name (one word, Q to quit): ");
            String bankName = in.next();
            if (bankName.equals("Q"))
            {
                done = true;
            }
            else
            {
                System.out.print("Please enter your principal investment: ");
                final double PRINCIPAL_INVESTMENT = in.nextDouble();
                System.out.print("Please enter the annual interest rate: ");
                double iRate = in.nextDouble();
                System.out.print("Please enter number of times interest is compounded per year:  ");
                final double INCREMENT = 1;//in.nextDouble();      
                System.out.print("Enter number of years: ");
                int nyears = in.nextInt();

                iRate = iRate/100; System.out.println("iRate:" + iRate);

                //Print the table of balances for each year

                for (int year = 1; year <= nyears; year++)
                {
                    double MULTIPLIER = INCREMENT * year;
                    System.out.println("Multiplier: " + MULTIPLIER); // I've included this print statement to show that the multiplier changes with each passing year
                    double interest = 1 + (iRate/INCREMENT);
                    double balance = PRINCIPAL_INVESTMENT;
                    double growth =  balance * Math.pow(interest, MULTIPLIER);
                    growth = growth - PRINCIPAL_INVESTMENT;                      
                    balance = balance + growth;                                  
                    System.out.printf("Year: %2d  Interest Earned:   $%.2f\t   Ending Balance:   $%.2f\n", year, growth, balance);

                    if (bestBankName.equals("") || bestGrowth > growth) // || bestBankName > growth
                    {
                        bestBankName = bankName;  // bestBankName = bankName
                        bestGrowth = growth; // mostGrow = growth
                    }
                    System.out.println("Earning with this option: " + growth);        
                }
            }

        } 
        System.out.println("Best Growth: " + bestBankName);
        System.out.println("Amount Earned: " + bestGrowth);       
    }
}

1 个答案:

答案 0 :(得分:2)

实际上,这段代码很难测试,这是一些设计气味的症状。

要认识到的一件事是你严重违反了单一责任原则。

您的代码只是一个blob,它正在执行以下操作:

  • 将内容打印到控制台
  • 从用户那里获得输入
  • 做一些计算
  • 协调所有这些

由于这是在练习领域,我会将代码重构为单独的类。那些应该很容易测试,特别是那个进行计算的人,因为它只有一些简单的方法,你可以将一些值作为参数传递,并检查结果

为了测试输入和输出类,请注意您可以将System.in和System.out更改为指向您自己的实现,因此您可以创建它们以便于测试。你可能想要研究一个模拟框架(例如Mockito),但没有这样的框架它是完全可能的。