testproduct.java

来自「这是一个coffee店管理系统,Gourmet咖啡是一个商店」· Java 代码 · 共 84 行

JAVA
84
字号
import  java.io.*;

/**
 * Test driver for class <code>Product</code>.
 *
 * @author  author name
 * @version  1.0.0
 * @see Product
 */
public class TestProduct  {

	/* Standard output stream */
	private static PrintWriter  stdOut = new  PrintWriter(System.out, true);

	/* Standard error stream */
	private static PrintWriter  stdErr = new  PrintWriter(System.err, true);

	/**
	 * Displays a message in the standard error stream if the value specified
	 * by parameter <code>condition<code> is <code>false</code>.
	 *
	 * @param message  the error message.
	 * @param condition  the test condition.
	 */
	public static void assertTrue(String message, boolean condition) {

		if (! condition) {
			stdErr.print("** Test failure ");
			stdErr.println(message);
		}
	}

	/**
	 * Test driver for class <code>Product</code>.
	 *
	 * @param args  not used.
	 */
	public static void  main(String[] args)  {

		String code = "C001";
		String description = "Colombia, Whole, 1 lb";
		double price = 10.0;

		// Test accessors
		Product product = new Product(code, description, price);

		assertTrue("1: testing method getCode",
		           code.equals(product.getCode()));
		assertTrue("2: testing method getDescription",
		           description.equals(product.getDescription()));
		assertTrue("3: testing method getPrice",
		           price == product.getPrice());

		// Test method equals
		String codeOne = "C001";
		String descriptionOne = "Colombia, Whole, 1 lb";
		double priceOne= 17.99;

		String codeTwo = "B111";
		String descriptionTwo = "Home Coffee Brewer";
		double priceTwo= 150.00;

		Product productOne = new Product(codeOne, descriptionOne, priceOne);
		Product productTwo = new Product(codeOne, descriptionTwo, priceTwo);
		Product productThree = new Product(codeTwo, descriptionTwo, priceTwo);

		assertTrue("4: testing method equals",
		           productOne.equals(productTwo));
		assertTrue("5: testing method equals",
		           !productOne.equals(productThree));
		assertTrue("6: testing method equals",
		           !productOne.equals("Product"));

		// Test method toString
		product = new Product(code, description, price);
		String result = code + "_" + description + "_" + price;

		assertTrue("7: testing method toString",
		           result.equals(product.toString()));

		stdOut.println("done");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?