'junit'에 해당되는 글 2건

  1. 2007.12.06 Junit 사용법 2
  2. 2007.02.09 [sample] junit setup 설정

Junit 사용법

JAVA/framework 2007. 12. 6. 14:34
   
  • Junit 3.8
  1. public class TestConvert extends MyTestCase { // TestCase 상속
       
        private boolean t = DES.t;
        private boolean f = DES.f;

        public TestConvert(String name) { // 생성자
            super(name);
        }

        protected void setUp() throws Exception { // 한 메소드 수행전에 실행되는 메소드
            super.setUp();
        }

        protected void tearDown() throws Exception { // 한 메소드 수행후에 실행되는 메소드
            super.tearDown();
        }
       
        public void testConvertToBinary() { // 일반 테스트 메소드 - test로 시작작
            String str = "dddddddd";
            boolean[] text = null;
            boolean[] expected = new boolean[str.length()*8];
            boolean[] oneByte = {f,t,t,f,f,t,f,f};
            int j=0;
            for(int i=0, n=str.length(); i<n;i++) {
                for(int k=0; k<oneByte.length; k++) {
                    expected[j++] = oneByte[k];
                }
            }
           
            text = Convert.convertToBinary(str);
            assertByteEquals(expected, text);
        }
       
        public static Test suite() { // 테스트 수위트 형식식
            TestSuite suite = new TestSuite();
           
            suite.addTestSuite(TestConvert.class);

            TestSetup wrapper = new TestSetup(suite) {
                protected void setUp() {
                    oneTimeSetup(); // 테스트 수행 전에 호출되는 함수
                }
                protected void tearDown() {
                    oneTimeTearDown(); // 테스트 수행 후에 호출되는 함수
                }
            };
            return wrapper;
        }

        protected static void oneTimeSetup() {
        }
       
        protected static void oneTimeTearDown() {
        }


  1. public class TestAll extends MyTestCase { // 전체 테스트

    public TestAll(String method) {
            super(method);
        }
       
        public static Test suite() {
            TestSuite suite = new TestSuite();
           
            suite.addTest(TestDES.suite());
            suite.addTest(TestDESKey.suite());
            suite.addTest(TestDESExecution.suite());
            suite.addTest(TestTripleDes.suite());
            suite.addTest(TestAES.suite());
            suite.addTest(TestAesKey.suite());
            suite.addTest(TestAESExcution.suite());
            suite.addTest(TestSecAlgorithm.suite());
            suite.addTest(TestConvert.suite());
            suite.addTest(TestShiftRegister.suite());
            suite.addTest(TestECBmode.suite());
            suite.addTest(TestCBCmode.suite());
            suite.addTest(TestCFBmode.suite());
            suite.addTest(TestOFBmode.suite());
            suite.addTest(TestCTRmode.suite());
            return suite;
        }
       
    }
  • Junit 4.0

    • Jnuit 4.0 뛰어 들기

      • setupBeforeClass
      1. @BeforeClass
      2. public static void setupBeforeClass() throws Exception { }
      • test 메소드
      1. @Test
      2. pubic void ...() {}
      • 예외 테스트
      1. @Test(expected = IndexOutOfBoundsException.class)
      2. public void verify() throws Exception {
      3. Matcher mtcher = this.pattern.matcher("221010-5051");
      4. boolean isValid = mtcher.matches();
      5. mtcher.group(2);
      6. }
      • 제한 시간 테스트
      1. @Test(timeout=1)
        public void verifyFastZipCodeMatch() throws Exception{       
         Pattern pattern = Pattern.compile("^\\d{5}([\\-]\\d{4})?$");
         Matcher mtcher = pattern.matcher("22011");
         boolean isValid = mtcher.matches();       
         assertTrue("Pattern did not validate zip code", isValid);
        }
      • 테스트 무시
      1. @Ignore("this regular expression isn't working yet")
        @Test
        public void verifyZipCodeMatch() throws Exception{       
         Pattern pattern = Pattern.compile("^\\d{5}([\\-]\\d{4})");
         Matcher mtcher = pattern.matcher("22011");
         boolean isValid = mtcher.matches();       
         assertTrue("Pattern did not validate zip code", isValid);
        }
      • setup
      1. @Before or @BeforeClass
      • tearDown
      1. @After or @AfterClass
      • Suit
      1. @RunWith(Suite.class)
        @SuiteClasses({ParametricRegularExpressionTest.class,
              RegularExpressionTest.class,
              TimedRegularExpressionTest.class})
        public class JUnit4Suite {

        }
import junit.framework.TestCase;

public class ImsiBeanTest extends TestCase {

    public ImsiBean imsiBean = new imsiBean();
    // 본 test Case를 수행하시기 위해서는 MonthlyStatMgtBean 에 있는 conn =    
    connMgr.getConnection(); 을 주석처리 해야 합니다.
    public void setUp() throws Exception {
        String db_url = "jdbc:oracle:thin:@100.100.100.100:1521:SID";
        String db_user = "USER";
        String db_pwd = "PWD";
        imsiBean.initConnection(db_url ,db_user, db_pwd);
        System.out.println ("codeBean : " +imsiBean );
    }

     public void testInsertStarEvent () {
        System.out.println (" -------- testInsertStarEvent -----------");
         boolean returnBol = imsiBean.insertStarEvent(getimsiRec());
         System.out.println ("test 001 " + returnBol);
    }

     public void testSelectStarEvent () {
        System.out.println (" -------- testSelectStarEvent -----------");
        boolean returnBol = imsiBean.selectStarEvent("");
        System.out.println ("test 002 " + returnBol);
     }

     public imsiRec getimsiRec () {
        ImsiRec imsiRec = new ImsiRec();
        imsiRec.setCust_name ("고영민");
        return imsiRec;
    }
}



private Connection conn= null;

public Connection initConnection (String url, String id, String pwd) {
    try {
         DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
         conn = DriverManager.getConnection(url, id, pwd);
     } catch (Exception e) {
         e.printStackTrace();
     }
     return conn;
}
1 

글 보관함

카운터

Total : / Today : / Yesterday :
get rsstistory!