Unit Test using Material?

Discussion in 'Spigot Plugin Development' started by R4zorax, Jul 22, 2018.

  1. Hi.

    I'm in the process of porting some plugins from 1.12 to 1.13 - but ran into some weird problems regarding the Material class.

    Apparently I cannot use that in JUnit tests, since it's not "class-loadable" outside a Bukkit-server?
    Code (Text):
    testIsTool(us.talabrek.ultimateskyblock.util.MaterialUtilTest)  Time elapsed: 0 sec  <<< ERROR!
    java.lang.NoClassDefFoundError: Could not initialize class org.bukkit.Material
            at us.talabrek.ultimateskyblock.util.MaterialUtilTest.testIsTool(MaterialUtilTest.java:13)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
            at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
            at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
            at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
            at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
            at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
            at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
            at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
            at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
     
    That seems odd to me - anyone found a good way to spinup tests that uses stuff from the Bukkit API?

    Code is this:
    https://github.com/rlf/uSkyBlock/bl...k/ultimateskyblock/util/MaterialUtilTest.java
    Code (Text):
        @Test
        public void testIsTool() {
            assertThat(MaterialUtil.isTool(Material.WOOD), is(false));
            assertThat(MaterialUtil.isTool(Material.STONE_BUTTON), is(false));
            assertThat(MaterialUtil.isTool(Material.DIAMOND_BARDING), is(false));
            assertThat(MaterialUtil.isTool(Material.GOLDEN_CARROT), is(false));
            assertThat(MaterialUtil.isTool(Material.WOOD_SWORD), is(true));
            assertThat(MaterialUtil.isTool(Material.DIAMOND_AXE), is(true));
            assertThat(MaterialUtil.isTool(Material.STONE_PICKAXE), is(true));
        }
    EDIT: The tests works fine on versions below 1.13 btw (been there since 1.7ish)
     
  2. Start a server and run it in an asynctask..
    Or just run it off the main thread as noone but you cares about the server crashing.
     
  3. What's the error you get related to initialising the class? Like it said, it failed to load the class, so there must be an error somewhere which shows us why it failed.
     
  4. Looks like it's the Guava version:
    Code (Text):
    java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

        at org.bukkit.NamespacedKey.<init>(NamespacedKey.java:47)
        at org.bukkit.NamespacedKey.minecraft(NamespacedKey.java:135)
        at org.bukkit.Material.<init>(Material.java:1913)
        at org.bukkit.Material.<init>(Material.java:1904)
        at org.bukkit.Material.<init>(Material.java:1892)
        at org.bukkit.Material.<clinit>(Material.java:76)
        at us.talabrek.ultimateskyblock.util.MaterialUtilTest.testIsTool(MaterialUtilTest.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at com.intellij.junit4.JUnit45ClassesRequestBuilder$1$1$2$2.runChild(JUnit45ClassesRequestBuilder.java:83)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
     
    What version is 1.13 using? Then I'll add it to the test-scope of my project and see if that helps.
    Never mind, found it v21.0
     
  5. Just to answer my own question, adding a dependency on:
    Code (Text):
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>21.0</version>
            </dependency>
    Fixed the unit-tests (i.e. made the tests that referred to Material directly be executable again).