⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfoo.java

📁 一个短小但实用的junit测试示例,适合初学者
💻 JAVA
字号:
import junit.framework.*;
import junit.textui.*;

public class TestFoo extends TestCase {

	// Toto instance used for tests
	protected Toto toto;
	protected int testSize;

	// standard constructor
    public TestFoo(String name) {
        super(name);
	}

   /**
    * This method is called every time before particular test execution.
    * It creates new instance of tested class and it can perform some more
    * actions which are necessary for performs tests.
    */
    protected void setUp(){
		testSize = 3;
        toto = new Toto(testSize);
	}

	/**
	 * test original method getHelloWorld, example of Assert.assertEquals
	 */
	public void testGetHelloWorld() {
		System.out.println("testGetHelloWorld");

		/* This test code makes use of assertEquals - one of a number of
		assertion methods provided by the JUnit interface Assert.
		There are overloaded variants of assertEquals to deal with various types
		of parameter, including the base types, containers, and Strings
		- if the two parameters are equal then it simply returns, otherwise it
		throws an unchecked exception used within the framework to report on the error. */

		// as the methodes returns "Hello World", the test should fail
		String result = "Hello World";
        Assert.assertEquals(result, toto.getHelloWorld());
    }

	/**
	 * test original method getTruth, example of Assert.assertTrue
	 */
	public void testGetTruth() {
        System.out.println("testGetTruth");
		/* assertTrue checks if its parameter equals true, otherwise it throws
		an unchecked exception used within the framework to report on the error */
		Assert.assertTrue(toto.getTruth());
    }

	/**
	 * test original method setElement, example of Assert.fail
	 */
	/*public void testSetElement() {
		 This test consists in checking that an exception is throwed in method getException
		trying to access element at position "size" of an array should throw an ArrayIndexOutOfBoundsException.
		
	    try {
		     System.out.print("testSetElement, exception throwed :   -->  ");
			 toto.setElement(testSize, 0);

		     /*	if exception is not catched, line Assert.fail is executed : test fails 
	          Assert.fail("setElement should raise an Exception");
        } catch (Exception e) { System.out.println(e.toString()); }

		// test with valid parameter (no exception supposed to be throwed)
	    try {
		     System.out.println("testSetElement, OK ");
			 int position = testSize - 1;
		     int result = 10;
			 toto.setElement(position, result);
			 Assert.assertEquals(result, toto.getElement(position));

        } catch (Exception e) { System.out.println(e.toString()); }

	}*/

	/**
	 * test original method getElement : to do !
	 */
	public void testGetElement() {
	}

	/**
    * Returns all tests which should be performed for testing class.
    * By default it returns only name of testing class. Instance of this
    * is then created with its constructor.
    */
	public static Test suite() {
		/* Junit uses reflexion to add automaticallu all the methods od TestFoo
		whose name begins by "test"
		*/
		return new TestSuite(TestFoo.class);
    }

	/**
    * This main method is used for run tests for this class only
    * from command line.
    */
	public static void main(String[] args) {

        /* to user command line interface  */
		junit.textui.TestRunner.run(suite());

		// to use GUI interface (no Suite paramater permitted)
		//junit.swingui.TestRunner.run(TestFoo.class);

    } // end of main(Stringp[] args)
}

⌨️ 快捷键说明

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