`
Flyingh
  • 浏览: 18299 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Refactoring Test

 
阅读更多

测试:

import org.junit.Before;
import org.junit.Test;

public class CustomerTest {
	private Customer customer;

	@Before
	public void setUp() throws Exception {
		customer = new Customer("flyingh");
		Movie movie = new Movie("haha1_movie", Movie.NEW_RELEASE);
		Movie movie2 = new Movie("haha2_movie", Movie.REGULAR);
		Movie movie3 = new Movie("haha3_movie", Movie.CHILDREN);
		Rental rental = new Rental(movie, 3);
		Rental rental2 = new Rental(movie2, 2);
		Rental rental3 = new Rental(movie3, 1);
		customer.addRental(rental);
		customer.addRental(rental2);
		customer.addRental(rental3);
	}

	@Test
	public void testStatement() {
		System.out.println(customer.statement());
	}
}

 测试结果(Green):

Rental Record for flyingh
	haha1_movie	9.0
	haha2_movie	2.0
	haha3_movie	1.5
Amount owed is 12.5
You earned 4 frequent renter points

 代码如下:

Before:

Movie.java

 

public class Movie {
	public static final int REGULAR = 0;
	public static final int NEW_RELEASE = 1;
	public static final int CHILDREN = 2;

	private String title;
	private int priceCode;

	public Movie(String title, int priceCode) {
		super();
		this.title = title;
		this.priceCode = priceCode;
	}

	public String getTitle() {
		return title;	
	}

	public int getPriceCode() {
		return priceCode;
	}

	public void setPriceCode(int priceCode) {
		this.priceCode = priceCode;
	}
}

 Rental.java

 

public class Rental {
	private Movie movie;
	private int daysRented;

	public Rental(Movie movie, int daysRented) {
		super();
		this.movie = movie;
		this.daysRented = daysRented;
	}

	public Movie getMovie() {
		return movie;
	}

	public int getDaysRented() {
		return daysRented;
	}
}

 Customer.java

 

import java.util.ArrayList;
import java.util.List;

public class Customer {
	private String name;
	private List<Rental> list = new ArrayList<Rental>();

	public Customer(String name) {
		super();
		this.name = name;
	}

	public String statement() {
		double totalAmount = 0;
		int frequentRenterPoints = 0;
		StringBuilder result = new StringBuilder();
		result.append("Rental Record for ").append(getName()).append("\n");
		for (Rental rental : list) {
			double thisAmount = 0;
			switch (rental.getMovie().getPriceCode()) {
			case Movie.REGULAR:
				thisAmount += 2;
				if (rental.getDaysRented() > 2) {
					thisAmount += (rental.getDaysRented() - 2) * 1.5;
				}
				break;
			case Movie.NEW_RELEASE:
				thisAmount += rental.getDaysRented() * 3;
				break;
			case Movie.CHILDREN:
				thisAmount += 1.5;
				if (rental.getDaysRented() > 3) {
					thisAmount += (rental.getDaysRented() - 3) * 1.5;
				}
				break;
			}
			++frequentRenterPoints;
			if (rental.getMovie().getPriceCode() == Movie.NEW_RELEASE
					&& rental.getDaysRented() > 1) {
				++frequentRenterPoints;
			}
			result.append("\t").append(rental.getMovie().getTitle())
					.append("\t").append(String.valueOf(thisAmount))
					.append("\n");
			totalAmount += thisAmount;
		}
		result.append("Amount owed is ").append(String.valueOf(totalAmount))
				.append("\n").append("You earned ")
				.append(String.valueOf(frequentRenterPoints))
				.append(" frequent renter points");
		return result.toString();
	}

	public void addRental(Rental rental) {
		list.add(rental);
	}

	public String getName() {
		return name;
	}

}

 After:

Price.java

 

public abstract class Price {
	public abstract int getPriceCode();

	public abstract double getCharge(int daysRented);
}

 RegularPrice.java

 

public class RegularPrice extends Price {

	@Override
	public int getPriceCode() {
		// TODO Auto-generated method stub
		return Movie.REGULAR;
	}

	@Override
	public double getCharge(int daysRented) {
		// TODO Auto-generated method stub
		double result = 2;
		if (daysRented > 2) {
			result += (daysRented - 2) * 1.5;
		}
		return result;
	}

}

 NewRealsePrice.java

 

public class NewRealsePrice extends Price {

	@Override
	public int getPriceCode() {
		// TODO Auto-generated method stub
		return Movie.NEW_RELEASE;
	}

	@Override
	public double getCharge(int daysRented) {
		// TODO Auto-generated method stub
		return daysRented * 3;
	}

}

 Children.java

 

public class ChildrenPrice extends Price {

	@Override
	public int getPriceCode() {
		// TODO Auto-generated method stub
		return Movie.CHILDREN;
	}

	@Override
	public double getCharge(int daysRented) {
		// TODO Auto-generated method stub
		double result = 1.5;
		if (daysRented > 3) {
			result += (daysRented - 3) * 1.5;
		}
		return result;
	}

}

 Movie.java

 

public class Movie {
	public static final int REGULAR = 0;
	public static final int NEW_RELEASE = 1;
	public static final int CHILDREN = 2;

	private String title;
	private Price price;

	public Movie(String title, int priceCode) {
		super();
		this.title = title;
		setPriceByPriceCode(priceCode);
	}

	public String getTitle() {
		return title;
	}

	public int getPriceCode() {
		return price.getPriceCode();
	}

	public void setPriceByPriceCode(int priceCode) {
		switch (priceCode) {
		case Movie.REGULAR:
			price = new RegularPrice();
			break;
		case Movie.NEW_RELEASE:
			price = new NewRealsePrice();
			break;
		case Movie.CHILDREN:
			price = new ChildrenPrice();
			break;
		default:
			throw new IllegalArgumentException();
		}
	}

	double getCharge(int daysRented) {
		return price.getCharge(daysRented);
	}

	int getFrequentRenterPoints(int daysRented) {
		// TODO Auto-generated method stub
		return getPriceCode() == Movie.NEW_RELEASE && daysRented > 1 ? 2 : 1;
	}
}

 Rental.java

 

public class Rental {
	private Movie movie;
	private int daysRented;

	public Rental(Movie movie, int daysRented) {
		super();
		this.movie = movie;
		this.daysRented=daysRented;
	}

	public Movie getMovie() {
		return movie;
	}

	public int getDaysRented() {
		return daysRented;
	}

	double getCharge() {
		return movie.getCharge(daysRented);
	}

	int getFrequentRenterPoints() {
		return movie.getFrequentRenterPoints(daysRented);
	}

}

 Customer.java

 

import java.util.ArrayList;
import java.util.List;

public class Customer {
	private String name;
	private List<Rental> list = new ArrayList<Rental>();

	public Customer(String name) {
		super();
		this.name = name;
	}

	public String statement() {
		StringBuilder result = new StringBuilder();
		appendInfoTo(result);
		return result.toString();
	}

	private void appendInfoTo(StringBuilder result) {
		appendHeadInfoTo(result);
		appendMiddleInfoTo(result);
		appendTailInfoTo(result);
	}

	private void appendTailInfoTo(StringBuilder result) {
		result.append("Amount owed is ")
				.append(String.valueOf(getTotalCharge())).append("\n")
				.append("You earned ")
				.append(String.valueOf(getTotalFrequentRenterPoints()))
				.append(" frequent renter points");
	}

	private void appendMiddleInfoTo(StringBuilder result) {
		for (Rental rental : list) {
			result.append("\t").append(rental.getMovie().getTitle())
					.append("\t").append(String.valueOf(getCharge(rental)))
					.append("\n");
		}
	}

	private void appendHeadInfoTo(StringBuilder result) {
		result.append("Rental Record for ").append(getName()).append("\n");
	}

	private int getTotalFrequentRenterPoints() {
		int frequentRenterPoints = 0;
		for (Rental rental : list) {
			frequentRenterPoints += getFrequentRenterPoints(rental);
		}
		return frequentRenterPoints;
	}

	private double getTotalCharge() {
		double totalAmount = 0;
		for (Rental rental : list) {
			totalAmount += getCharge(rental);
		}
		return totalAmount;
	}

	private int getFrequentRenterPoints(Rental rental) {
		return rental.getFrequentRenterPoints();
	}

	private double getCharge(Rental rental) {
		return rental.getCharge();
	}

	public void addRental(Rental rental) {
		list.add(rental);
	}

	public String getName() {
		return name;
	}

}


分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    xUnit Test Patterns Refactoring Test

    ### xUnit Test Patterns: Refactoring Test #### 一、概述 《xUnit Test Patterns: Refactoring Test》是一本深入探讨软件测试技术与实践的专业书籍。本书不仅提供了丰富的测试模式,还详细介绍了如何通过重构来...

    xUnit Test.Patterns Refactoring Test Code

    ### xUnit Test Patterns – Refactoring Test Code #### 概述 《xUnit测试模式——测试码重构》是一本深入探讨单元测试模式的书籍,通过详细的内容帮助开发者理解并改进单元测试的方法与技巧。本书覆盖了多种不同...

    RefactoringTest:重构测试Carlos Cantillo-BDC

    C#中的重构测试 描述 要求您重构UserService类,更具体地说,重构其AddUser方法。 假设代码在业务逻辑方面是合理的,并且仅专注于应用干净的代码原则。 请记住首字母缩写词,例如SOLID,KISS,DRY和YAGNI。...

    xUnit.Test.Patterns.Refactoring.Test.Code.May.2007

    《xUnit.Test.Patterns.Refactoring.Test.Code.May.2007》这本书主要探讨的是单元测试框架xUnit在测试代码重构中的应用与实践。xUnit是面向对象编程领域广泛使用的一类单元测试框架,它提供了自动化测试的能力,帮助...

    专家谈C#学习方法与书籍选择

    - 推荐书籍:《xUnit Test Patterns》(xUnit Test Patterns: Refactoring Test Code)和《Continuous Delivery》(Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment ...

    Test-Driven Development with Python, 2nd Edition, 2017

    (And, Refactoring) Chapter 5. Saving User Input: Testing the Database Chapter 6. Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps Chapter 7. Working Incrementally Part II. ...

    敏捷开发:Agile Evolutionary Design:Constant design improvement through Continuous Integration, Test Driven Development and Refactoring

    Agile Evolutionary Design:Constant design improvement through Continuous Integration, Test Driven Development and Refactoring Paulo Caroli, Agile China 2008

    java-refactoring-test:Java重构测试

    Java重构测试项目 请在开始测试之前,仔细通读所有说明! 介绍 这是hybris软件雇用过程使用的一个测试项目,用于测试您对Java / Spring最佳实践和重构的了解。 本练习的目的是评估您识别不良编码实践并通过使用最佳...

    Test-.Driven.Python.Development.1783987928

    Code Smells and Refactoring Chapter 4. Using Mock Objects to Test Interactions Chapter 5. Working with Legacy Code Chapter 6. Maintaining Your Test Suite Chapter 7. Executable Documentation with ...

    Test-Driven Java Development(PACKT,2015)

    Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to ...

    Test-Driven Java Development

    Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to ...

    refactoring-test-master

    C#中的重构测试描述要求您重构UserService类,更具体地说,重构其AddUser方法。 假设代码在业务逻辑方面是合理的,并且仅专注于应用干净的代码原理。 请记住首字母缩写词,例如SOLID,KISS,DRY和YAGNI。...

    Test Driven Development in Ruby:

    Handle refactoring using Ruby Hide implementation details Test precisely and concretely Make your code robust Who This Book Is For Experienced Ruby programmers or web developers with some prior ...

    Test-Driven Development with Python [2017]

    Dive into the TDD workflow, including the unit test/code cycle and refactoring Use unit tests for classes and functions, and functional tests for user interactions within the browser Learn when and ...

    Test-Driven Java Development - Second Edition.pdf

    Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasizes writing a test before writing the necessary code, and then refactoring the code to ...

    test-driven-java-development-2nd2018

    Chapter 1, Why Should I Care for Test-Driven Development?, spells out our goal of becoming a Java developer with a TDD black belt. In order to know where we're going, we'll have to discuss and find ...

    money-note:追踪您在活动上花费的钱

    钱笔记 追踪您在旅行,婚礼,某人的生日,生日等活动上...test: Adding tests, refactoring test; no production code change chore: Updating build tasks, package manager configs, etc; no production code change

    TRex - the TTCN-3 Refactoring and Metrics Tool

    The comprehensive test of modern communication systems leads to large and complex test suites which have to be maintained throughout the system life-cycle. Experience with those written in the ...

    Java Test-Driven Development(PACKT,2015)

    Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to ...

Global site tag (gtag.js) - Google Analytics