This shows you the differences between two versions of the page.
|
poo-ca-cd:alte-resurse:junit-java [2021/10/03 20:23] florian_luis.micu [Unit testing] |
poo-ca-cd:alte-resurse:junit-java [2021/11/24 00:52] (current) radu_bogdan.pavel [Unit testing] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== JUnit ===== | + | ===== JUnit5 ===== |
| ==== Obiective ==== | ==== Obiective ==== | ||
| Line 99: | Line 99: | ||
| <code java Test.java> | <code java Test.java> | ||
| - | import org.junit.Assert; | + | import org.junit.jupiter.api.* |
| - | import org.junit.Before; | + | |
| - | import org.junit.After; | + | |
| - | public class Test { | + | public class GroupTest { |
| private Group group; | private Group group; | ||
| - | @Before | + | @BeforeEach |
| public void setup() { | public void setup() { | ||
| group = new Group(); | group = new Group(); | ||
| } | } | ||
| - | @org.junit.Test | + | @Test |
| public void testNoStudentInGroup() { | public void testNoStudentInGroup() { | ||
| - | Assert.assertEquals(false, group.areStudentsInGroup()); | + | Assertions.assertEquals(false, group.areStudentsInGroup()); |
| } | } | ||
| - | @org.junit.Test | + | @Test |
| public void testAddStudent() { | public void testAddStudent() { | ||
| Student st = new Student("Elena", "11"); | Student st = new Student("Elena", "11"); | ||
| group.addStudent(st); | group.addStudent(st); | ||
| - | Assert.assertTrue(group.getStudent("Elena").equals(st)); | + | Assertions.assertTrue(group.getStudent("Elena").equals(st)); |
| } | } | ||
| - | @After | + | |
| - | void tearDown() { | + | @AfterEach |
| + | public void tearDown() { | ||
| group = null; | group = null; | ||
| } | } | ||
| Line 133: | Line 132: | ||
| **Observaţii:** | **Observaţii:** | ||
| * fiecare metodă de test are adnotarea: ''@Test'' | * fiecare metodă de test are adnotarea: ''@Test'' | ||
| - | * metodele de **setUp** (având adnotarea: ''@Before'') şi **tearDown** (având adnotarea: ''@After'') se apelează înainte, respectiv după fiecare test. Se întrebuinţează pentru iniţializarea/eliberarea resurselor ce constituie mediul de testare, evitându-se totodată duplicarea codului şi respectându-se principiul de independenţă a testelor. Pentru exemplul dat ordinea este: | + | * metodele de **setUp** (având adnotarea: ''@BeforeEach'') şi **tearDown** (având adnotarea: ''@AfterEach'') se apelează înainte, respectiv după fiecare test. Se întrebuinţează pentru iniţializarea/eliberarea resurselor ce constituie mediul de testare, evitându-se totodată duplicarea codului şi respectându-se principiul de independenţă a testelor. Pentru exemplul dat ordinea este: |
| - | * ''@Before setUp'' | + | * ''@BeforEach setUp'' |
| * ''@Test testNoStudentInGroup'' | * ''@Test testNoStudentInGroup'' | ||
| - | * ''@After tearDown'' | + | * ''@AfterEach tearDown'' |
| - | * ''@Before setUp'' | + | * ''@BeforeEach setUp'' |
| * ''@Test testAddStudent'' | * ''@Test testAddStudent'' | ||
| - | * ''@After tearDown'' | + | * ''@AfterEach tearDown'' |
| * În contextul moștenirii dacă ChildTest extends ParentTest ordinea de execuţie este urmatoarea: | * În contextul moștenirii dacă ChildTest extends ParentTest ordinea de execuţie este urmatoarea: | ||
| - | * ''**ParentTest** @Before setUp'' | + | * ''**ParentTest** @BeforeEach setUp'' |
| - | * ''ChildTest @Before setUpSub'' | + | * ''ChildTest @BeforeEach setUpSub'' |
| * ''ChildTest @Test testChild1'' | * ''ChildTest @Test testChild1'' | ||
| - | * ''ChildTest @After tearDownSub'' | + | * ''ChildTest @AfterEach tearDownSub'' |
| - | * ''**ParentTest** @After tearDown'' | + | * ''**ParentTest** @AfterEach tearDown'' |
| * Pentru compararea rezultatului aşteptat cu rezultatul curent se folosesc apeluri ''assert'': | * Pentru compararea rezultatului aşteptat cu rezultatul curent se folosesc apeluri ''assert'': | ||
| * ''assertTrue'' | * ''assertTrue'' | ||
| Line 156: | Line 155: | ||
| <note important> | <note important> | ||
| - | În cadrul laboratorului vom folosi framework-ul JUnit adăugând fişierele [[http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar|junit.jar]] şi [[https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/hamcrest-all-1.3.jar|hamcrest-all-1.3.jar]] cu acesta la proiectele create. | + | Pentru a folosi framework-ul JUnit5, consultați tutorialul din [[poo-ca-cd:laboratoare:colectii|laboratorul 8]], aflat la secțiunea **Unit testing**. |
| - | + | ||
| - | Pentru a importa un //jar// într-un proiect din ''Eclipse'' parcurgeţi următorii paşi: //click dreapta proiect// -> //Build path// -> //Configure build path// -> //Libraries// -> //Add jars (Add external jars)//. | + | |
| </note> | </note> | ||
| Line 191: | Line 188: | ||
| * <html><a class="media mediafile mf_pdf" href="/poo/laboratoare/exceptii?do=export_pdf">PDF laborator</a></html> | * <html><a class="media mediafile mf_pdf" href="/poo/laboratoare/exceptii?do=export_pdf">PDF laborator</a></html> | ||
| - | * [[http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar|Junit Download]] | ||
| - | * [[http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar|Hamcrest Core]] | ||
| * {{:poo-ca-cd:alte-resurse:junit-java:skel.zip|Schelet}} | * {{:poo-ca-cd:alte-resurse:junit-java:skel.zip|Schelet}} | ||
| - | * {{:poo-ca-cd:alte-resurse:junit-java:sol.zip|Soluție}} | + | * {{:poo-ca-cd:alte-resurse:solutie-junit.zip|Solutie}} |
| ==== Referinţe ==== | ==== Referinţe ==== | ||