Do not use org.junit.rules.ExpectedException 64/87664/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 09:50:34 +0000 (10:50 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 09:51:17 +0000 (10:51 +0100)
We have assertThrows() which is much more useful, refactor the test
to be more explicit and eliminate use of deprecated elements.

Change-Id: I8613afbd3028d602a17f9ea7024e8abde6b8cf48
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/yang/types/Bug4621.java

index 278e33a0c114f21f73880d32353b107c4fd5622a..0f1f9ffb55bd3433d461d58eb4f3a306f7c4e071 100644 (file)
@@ -7,12 +7,12 @@
  */
 package org.opendaylight.mdsal.binding.yang.types;
 
-import static org.junit.Assert.assertNotNull;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
 
 import java.net.URI;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -24,24 +24,22 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class Bug4621 {
-
-    @Rule
-    public ExpectedException expectedEx = ExpectedException.none();
-
     @Test
     public void bug4621test() {
         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug-4621/foo.yang");
         final Module moduleValid = schemaContext.findModules(URI.create("foo")).iterator().next();
         final TypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
 
-        expectedEx.expect(IllegalArgumentException.class);
-
         final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
         final QName leafrefNode = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
-        DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
+        final DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
                 .getDataChildByName(leafrefNode);
-        LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
-        TypeDefinition<?> leafTypeRel = leafRel.getType();
-        assertNotNull(typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel));
+        final LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
+        final TypeDefinition<?> leafTypeRel = leafRel.getType();
+
+        final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel));
+        assertThat(ex.getMessage(),
+            startsWith("Failed to find leafref target: /foo:neighbor/foo:mystring1 in module foo"));
     }
 }
\ No newline at end of file