
จากรูปจะเห็นว่าเราได้สร้างคลาสใหม่ขึ้นมาอีกคลาสหนึ่ง ชื่อ CompositeTest.java ซึ่งคลาสนี้ว่างเปล่าไม่มีอะไรเลย แต่เราบอก Junit ไป 2 อย่างคือ
@RunWith(value = Suite.class)
@RunWith เป็น annotation ที่ Junit สร้างมาให้เราใช้ เพื่อให้เราเอาไว้บอก Junit ว่าจะต้องทำงานยังไง
แล้ว annotation มันคืออะไร ? มันก็คือ รูปแบบหนึ่งของ metadata
เอาแล้วไง ! ผมว่าล่ะ โอเค แล้ว metadata มันคืออะไรอีกง่ะ ? metadata คือข้อมูลของข้อมูล
หมายความว่า สำหรับข้อมูลหนึ่งๆ มันจะมีความหมายของมันอีกที. ดังนั้น Annotation คือคือการให้ความหมาย อาจจะคล้ายๆรหัสก็ว่าได้ แล้วโปรแกรม ( Junit ) จะเอาไปแปลอีกที ยกตัวอย่างอีกนิดหน่อยเช่น @Overrided มีความหมายสำหรับ จาวาคอมไพล์เลอร์ แต่ไม่มีความหมายในวันวาเลนไทน์ อุ๊ย+
แล้วข้างในวงเล็บที่ว่า value = Suite.class ก็คือ Suit นะครับ มันจะเป็น container สำหรับบรรจุเทสเคสหลายๆคลาสไว้ด้วยกัน
และอันสุดท้ายที่เราบอก Junit ก็คือ @SuiteClasses ซึ่งจะบอกว่าคลาสไหนที่จะเอามาแพกไว้เพื่อรอสำหรับการเทส
ซอสโค๊ดสำหรับ CompositeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Suite; | |
import org.junit.runners.Suite.SuiteClasses; | |
@RunWith(value = Suite.class) | |
@SuiteClasses(value = { FactorialTest.class, FactorialParametersTest.class }) | |
public class CompositeTest | |
{ | |
} |