110 likes | 248 Views
CSE446 Software Quality Management. Orhan Ba şar Evren. Spring 2014. Yazılım ve Uyguluma Geliştirme Yöneticisi. Today’s Overview. JUnit : Java Unit Test Unit Tests What is JUnit ? Test method example JUnit annotations Assert statements Test Suits Parameterized Test. Unit Tests.
E N D
CSE446 Software Quality Management Orhan Başar Evren Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi
Today’s Overview • JUnit: Java Unit Test • Unit Tests • What is JUnit ? • Test method example • JUnit annotations • Assert statements • Test Suits • Parameterized Test CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
Unit Tests • A unit test is a piece of code written by a developer that executes a specific functionality in the code to be tested. • Test critical and complex parts of your application. • Ignoretrivial code. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit : Unit Test Framework for Java • JUnit is a test framework which uses annotations to identify methods that specify a test. • Typically these test methods are contained in a class, which is only used for testing. It is called a Test Class. • Test methods should not depend on other tests. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit : Test method example @Test public void testMultiply() { //Calculator class to be tested Calculator c = new Calculator (); //Check if multiply(2,2) returns 4 assertEquals(“2 x 2 must be 4”, 4, c.multiply(2,2)); } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit :JUnit Annotations • @Test : identifies a method as a test method. • @Test(expected = Exception.class) • @Test(timeout = 100) • @Before : Identified method executed before each test. Used for preparation of test enviroment • @After: Identified method executed after each test. Used for cleanup • @BeforeClass: Identified method executed once before the test. CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit : Assert statements • fail(String) : Let the method fail • assertTrue(String message, boolean condition): Checks that the boolean condition is true • assertEquals(String message, Object expected, Object actual) : Tests that two values ar the same. • assertNotNull(String message, Object object) checks that object is not null CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit : Test Suits • Test classes can be combined into a test suite. Running a test suite will execute all test classes in that suite in the specified order. @RunWith(Suite.class) @SuiteClasses({ ATest.class, BTest.class }) publicclassAllTests { } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit : Parameterized Test @RunWith(Parameterized.class) publicclassMyTest { privateintnum; publicMyTest(intnum) { this.num= num; } @Parameters publicstatic Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 5 }, { 121 } }; returnArrays.asList(data); } @Test publicvoidtestMultiply () { Calculator c = newCalculator(); assertEquals("Result", num * num, c.multiply(num, num)); } } CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit :NetBeans Support • Auto generate test classes CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş
JUnit :NetBeans Support CSE446 Software Quality Management Spring 2014 – Orhan Başar Evren - Netaş