Testing Unit testing with JUnit forrs http www

  • Slides: 14
Download presentation
Testing Unit testing with JUnit forrás: http: //www. vogella. com/tutorials/JUnit/article. html

Testing Unit testing with JUnit forrás: http: //www. vogella. com/tutorials/JUnit/article. html

Mit érdemes tesztelni? • A program kód minden egyes állatását (sorát) • Figyelmen kivül

Mit érdemes tesztelni? • A program kód minden egyes állatását (sorát) • Figyelmen kivül hagyhatak a triviális sorok: • getter/setter metódusok • értékadások (pl. : Foo foo = previosly. Computed. Value. bar. foo) • bármilyen metódus, ami nem tartalmaz logikát és/vagy beépített metódusokat használ • grafikai (UI) metódusok

Hogyan készítsünk JUnit teszteket? • Egy Junit teszt egy metódus, amit csak teszteléshez használunk

Hogyan készítsünk JUnit teszteket? • Egy Junit teszt egy metódus, amit csak teszteléshez használunk egy osztályba foglalva (Test class) • A metódust JUnit 4. x framework esetén @org. junit. Test -val annotáljuk. • Ebben a metódusban a JUnit framework egy metódusát használjuk, hogy összehasonlítsuk az elvárt eredmény az aktuálies eredménnyel.

Példa @Test public void multiplication. Of. Zero. Integers. Should. Return. Zero() { // My.

Példa @Test public void multiplication. Of. Zero. Integers. Should. Return. Zero() { // My. Class is tested My. Class tester = new My. Class(); // Tests assert. Equals("10 x 0 must be 0", 0, tester. multiply(10, 0)); assert. Equals("0 x 10 must be 0", 0, tester. multiply(0, 10)); assert. Equals("0 x 0 must be 0", 0, tester. multiply(0, 0)); }

JUnit test suites • Ha több teszt osztályunk is van, akkor ezeket kombinálhatjuk egy

JUnit test suites • Ha több teszt osztályunk is van, akkor ezeket kombinálhatjuk egy teszt sorozatba. Ez végrehajtja az osztályokban lévő összes tesztet sorrendben. • Az alábbi példában két osztályt definiáltunk.

package com. vogella. junit. first; import org. junit. runner. Run. With; import org. junit.

package com. vogella. junit. first; import org. junit. runner. Run. With; import org. junit. runners. Suite. Classes; @Run. With(Suite. class) @Suite. Classes({ My. Class. Test. class, My. Second. Class. Test. class }) public class All. Tests { }

Futtatás parancssorból • A JUnit tesztek futtathatók az Eclipse környezeten kívul is. A beépített

Futtatás parancssorból • A JUnit tesztek futtathatók az Eclipse környezeten kívul is. A beépített framework-ök mint az Apact Ant vagy Apache Maven hajtja végre a teszteket. • A org. junit. runner. JUnit. Core osztály biztosít egy run. Classes() függvényt, mellyel egy vagy több teszt osztály futtatható. • A visszatérési értéke org. junit. runner. Result, melyből megszerezhető a tesztek eredménye.

package de. vogella. junit. first; import org. junit. runner. JUnit. Core; import org. junit.

package de. vogella. junit. first; import org. junit. runner. JUnit. Core; import org. junit. runner. Result; import org. junit. runner. notification. Failure; public class My. Test. Runner { public static void main(String[] args) { Result result = JUnit. Core. run. Classes(My. Class. Test. class); for (Failure failure : result. get. Failures()) { System. out. println(failure. to. String()); } } }

Annotációk Annotation Description @Test public void method() The @Test annotation identifies a method as

Annotációk Annotation Description @Test public void method() The @Test annotation identifies a method as a test method. @Test (expected = Exception. class) Fails if the method does not throw the named exception. @Test(timeout=100) Fails if the method takes longer than 100 milliseconds. @Before public void method() This method is executed before each test. It is used to prepare the test environment (e. g. , read input data, initialize the class). @After public void method() This method is executed after each test. It is used to cleanup the test environment (e. g. , delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures. @Before. Class public static void method() This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit. @After. Class public static void method() This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit. @Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.

Összehasonlító műveletek Statement Description fail(String) Let the method fail. Might be used to check

Összehasonlító műveletek Statement Description fail(String) Let the method fail. Might be used to check that a certain part of the code is not reached or to have a failing test before the test code is implemented. The String parameter is optional. assert. True([message], boolean condition) Checks that the boolean condition is true. assert. False([message], boolean condition) Checks that the boolean condition is false. assert. Equals([String message], expected, actual) Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. assert. Equals([String message], expected, actual, tolerance) Test that float or double values match. The tolerance is the number of decimals which must be the same. assert. Null([message], object) Checks that the object is null. assert. Not. Null([message], object) Checks that the object is not null. assert. Same([String], expected, actual) Checks that both variables refer to the same object. assert. Not. Same([String], expected, actual) Checks that both variables refer to different objects.

Mockolás • Mockolásnál az igazi objektumokat kicseréljük egy helyettesítő objektumra, melynek egy előre definiált

Mockolás • Mockolásnál az igazi objektumokat kicseréljük egy helyettesítő objektumra, melynek egy előre definiált viselkedése van. • Ezzel tesztelhetjük olyan osztályok működését, mely más osztályokra támaszkodnak. pl. : • Hálózatot igénylő alkalmazások esetén, • AI-val rendelkező osztályok esetén (előre definiált lépések), • Véletlenszerű események esetén, determinált viselkedés (Random osztálynak adható ‘seed’, így minden esetben ugyanazt a random szamot generálja, mivel pseudo random algroitmust használ. ) • Program bizonyos állapotba kerülésének tesztelése (pl. nincs több lépés tesztelése esetén előre beállított logikai állapotú logikai objektumokat adunk a controll osztálynak) pl. : • Model predefined. Test. Model. For. No. Steps. Test = new Model(); this. set. Model. For. No. Test. Steps( predefined. Test. Model. For. No. Steps. Test); Control control = new Control(); control. model = predefined. Test. Model. For. No. Steps. Test; assert. False(control. does. Player. Has. More. Moves());

Rules package de. vogella. junit. first; import org. junit. Rule; import org. junit. Test;

Rules package de. vogella. junit. first; import org. junit. Rule; import org. junit. Test; import org. junit. rules. Expected. Exception; public class Rule. Exception. Tester. Example { @Rule public Expected. Exception exception = Expected. Exception. none(); @Test public void throws. Illegal. Argument. Exception. If. Icon. Is. Null() { exception. expect(Illegal. Argument. Exception. class); exception. expect. Message("Negative value not allowed"); Class. To. Be. Tested t = new Class. To. Be. Tested(); t. method. To. Be. Test(-1); } }

package de. vogella. junit. first; import static org. junit. Assert. assert. True; import java.

package de. vogella. junit. first; import static org. junit. Assert. assert. True; import java. io. File; import java. io. IOException; import org. junit. Rule; import org. junit. Test; import org. junit. rules. Temporary. Folder; public class Rule. Tester { @Rule public Temporary. Folder folder = new Temporary. Folder(); @Test public void test. Using. Temp. Folder() throws IOException { File created. Folder = folder. new. Folder("newfolder"); File created. File = folder. new. File("myfile. txt"); assert. True(created. File. exists()); } }

Segítség: http: //www. vogella. com/tut orials/JUnit/article. html

Segítség: http: //www. vogella. com/tut orials/JUnit/article. html