Migrate yang-parser-spi to JUnit5 09/106909/8
authormatus.matok <matus.matok@pantheon.tech>
Thu, 13 Jul 2023 08:57:02 +0000 (10:57 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Oct 2023 14:01:49 +0000 (16:01 +0200)
Migrated all tests to use JUnit5 Assertions, using
openrewrite:rewrite-testing-frameworks. Also clean up mocking and use
MockitoExtension.

JIRA: YANGTOOLS-1521
Change-Id: Icc91eda7a6e15003aa1a1135bd31a960869ded14
Signed-off-by: matus.matok <matus.matok@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/meta/ModelProcessingPhaseTest.java
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/meta/NamespaceTest.java
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/source/ExplicitStatementTest.java
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/source/ImplicitStatementTest.java
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/source/QNameToStatementDefinitionMapTest.java
parser/yang-parser-spi/src/test/java/org/opendaylight/yangtools/yang/parser/spi/source/SourceExceptionTest.java

index c03bfbfe1b86c9fcda5ad798c8205eb0d2c62f07..13c3e43a0a02f417f7311defd0d6a7ae532aa041 100644 (file)
@@ -7,14 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
-public class ModelProcessingPhaseTest {
+class ModelProcessingPhaseTest {
     @Test
-    public void testSequencing() {
+    void testSequencing() {
         assertNull(ModelProcessingPhase.INIT.getPreviousPhase());
         assertEquals(ModelProcessingPhase.INIT, ModelProcessingPhase.SOURCE_PRE_LINKAGE.getPreviousPhase());
         assertEquals(ModelProcessingPhase.SOURCE_PRE_LINKAGE, ModelProcessingPhase.SOURCE_LINKAGE.getPreviousPhase());
index b4d9ca6c7e6c398526e207219d3001ef96e31a9f..12c75ab4c78e3da0bde65bd17d3036933266d202 100644 (file)
@@ -7,13 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
-public class NamespaceTest {
+class NamespaceTest {
     @Test
-    public void testNamespaces() {
+    void testNamespaces() {
         // Touch behaviors
         // FIXME: add more checks/split this up when behaviours are testable
         assertNotNull(StatementDefinitions.BEHAVIOUR);
index 9147fe2df60ad18af6954a189e12b308b5fe235a..b3f87925774ed5e9202ab47a211d9f903a822b02 100644 (file)
@@ -7,19 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
 
-public class ExplicitStatementTest {
+class ExplicitStatementTest {
     @Test
-    public void testStatementSource() {
+    void testStatementSource() {
         assertEquals(StatementOrigin.DECLARATION, ExplicitStatement.inFile("foo").statementOrigin());
     }
 
     @Test
-    public void testToString() {
+    void testToString() {
         assertEquals("foo", ExplicitStatement.inFile("foo").toString());
         assertEquals("<UNKNOWN>:5:10", ExplicitStatement.atPosition(5, 10).toString());
         assertEquals("foo:5:10", ExplicitStatement.atPosition("foo", 5, 10).toString());
index 22548c3cdf9009408b157b0a88de44261a974ad0..d296f363b68910fce57784ce4bfe405feb6a207a 100644 (file)
@@ -7,18 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
 
-public class ImplicitStatementTest {
+@ExtendWith(MockitoExtension.class)
+class ImplicitStatementTest {
+    @Mock
+    private StatementSourceReference ref;
+
     @Test
-    public void testForwarding() {
-        final StatementSourceReference ref = mock(StatementSourceReference.class);
-        final ImplicitSubstatement stmt = ImplicitSubstatement.of(ref);
+    void testForwarding() {
+        final var stmt = ImplicitSubstatement.of(ref);
         assertEquals(StatementOrigin.CONTEXT, stmt.statementOrigin());
 
         doReturn("ref").when(ref).toString();
index 91e6e087c4646c7f868d531567eae6088e641892..f7c26fda08817e2c5044b5b0369d9be88cdca543 100644 (file)
@@ -7,46 +7,48 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 
-public class QNameToStatementDefinitionMapTest {
+@ExtendWith(MockitoExtension.class)
+class QNameToStatementDefinitionMapTest {
     private static final QName QNAME = QName.create("", "a");
 
     private final QNameToStatementDefinitionMap map = new QNameToStatementDefinitionMap();
-    private final StatementSupport<?, ?, ?> support = mock(StatementSupport.class);
-    private final StatementDefinition definition = mock(StatementDefinition.class);
 
-    @Before
-    public void before() {
-        doReturn(definition).when(support).definition();
-    }
+    @Mock
+    private StatementSupport<?, ?, ?> support;
+    @Mock
+    private StatementDefinition definition;
 
     @Test
-    public void testPutNullNull() {
+    void testPutNullNull() {
         assertThrows(NullPointerException.class, () -> map.put(null, null));
     }
 
     @Test
-    public void testPutNullSome() {
+    void testPutNullSome() {
         assertThrows(NullPointerException.class, () -> map.put(null, support));
     }
 
     @Test
-    public void testPutSomeNull() {
+    void testPutSomeNull() {
         assertThrows(NullPointerException.class, () -> map.put(QName.create("", "a"), null));
     }
 
     @Test
-    public void testPut() {
+    void testPut() {
+        doReturn(definition).when(support).definition();
+
         map.put(QNAME, support);
         assertSame(definition, map.get(QNAME));
         assertSame(support, map.getSupport(QNAME));
index 63f2e95b55e371934fb93d7511c79a1d2063cacc..0c9c4b3111d4c85860ef21a1244526ecb6fd2a5c 100644 (file)
@@ -7,65 +7,65 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.doReturn;
 
 import java.util.Optional;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class SourceExceptionTest {
+@ExtendWith(MockitoExtension.class)
+class SourceExceptionTest {
     @Mock
-    public StatementSourceReference mock;
-
-    @Before
-    public void before() {
-        doReturn("mock").when(mock).toString();
-    }
+    private StatementSourceReference mock;
 
     @Test
-    public void testThrowIfFalse() {
+    void testThrowIfFalse() {
         SourceException.throwIf(false, mock, "");
     }
 
     @Test
-    public void testThrowIfTrueMockNull() {
+    void testThrowIfTrueMockNull() {
         assertThrows(NullPointerException.class, () -> SourceException.throwIf(true, mock, null));
     }
 
     @Test
-    public void testThrowIfTrueMockEmpty() {
+    void testThrowIfTrueMockEmpty() {
+        doReturn("mock").when(mock).toString();
+
         assertThrows(SourceException.class, () -> SourceException.throwIf(true, mock, ""));
     }
 
     @Test
-    public void testThrowIfNullNullMockNull() {
+    void testThrowIfNullNullMockNull() {
         assertThrows(NullPointerException.class, () -> SourceException.throwIfNull(null, mock, null));
     }
 
     @Test
-    public void testThrowIfNullNullMockEmpty() {
+    void testThrowIfNullNullMockEmpty() {
+        doReturn("mock").when(mock).toString();
+
         assertThrows(SourceException.class, () -> SourceException.throwIfNull(null, mock, ""));
     }
 
     @Test
-    public void testThrowIfNullMock() {
+    void testThrowIfNullMock() {
         assertSame(mock, SourceException.throwIfNull(mock, mock, ""));
     }
 
     @Test
-    public void testUnwrapPresent() {
+    void testUnwrapPresent() {
         assertEquals("test", SourceException.unwrap(Optional.of("test"), mock, ""));
     }
 
     @Test
-    public void testUnwrapAbsent() {
+    void testUnwrapAbsent() {
+        doReturn("mock").when(mock).toString();
+
         assertThrows(SourceException.class, () -> SourceException.unwrap(Optional.empty(), mock, ""));
     }
 }