Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7543

Help: "No registry present for Enchantment. This is a bug." when using Mockito for tests.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor Minor
    • None
    • None
    • None
    • This server is running CraftBukkit version 3966-Spigot-ce0f71e-cb2ea54 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT)
    • Yes

      When compiling my plugin with 1.20.3 or 1.20.4, a lot of my JUnit tests fail that were passing with 1.20.2:

      The error I get is:

       

      Caused by: java.lang.NullPointerException: No registry present for Enchantment. This is a bug.
      	at java.base/java.util.Objects.requireNonNull(Objects.java:235)	at org.bukkit.Registry.<clinit>(Registry.java:115)
      

       

      I tracked that down to this line in Registry.class:

      Registry<Enchantment> ENCHANTMENT = Objects.requireNonNull(Bukkit.getRegistry(Enchantment.class),"No registry present for Enchantment. This is a bug."); 

      I use enchantments in the plugin to, for example, make an item visible or invisible in the player's inventory, e.g.,

      meta.addEnchant(Enchantment.VANISHING_CURSE, 1, true); 

      or to make an item glow:

      meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, glow); 

      and there are a lot of panels/GUI in the plugin, for things like settings, etc.
       
      In my JUnit tests, I mock Bukkit.class so that I can test the plugin code and during panel tests, the Registry.class is pulled in and processed by Mockito for the ENCHANTMENT part, but then it hits this null check and fails. 
       
      If I comment out the places where I use Enchantment.VANISHING_CURSE and Enchantment.ARROW_DAMAGE then everything compiles and tests perfectly, but of course, that would mean I can't have items glow, etc. in inventories.
       
      I have tried to mock around this but I'm really stuck with this change. It seems there is a race condition - in order to mock, it needs to be declared, but in the declaration, it gets initialized, but it hasn't been mocked yet, so it hits the null check. I assume that other developers make tests and use Enchantments and so may hit this issue too. Can you think of any possible solution apart from skipping tests?   
       
      I don't know if it helps, but previously, (prior to 1.20.3) the method was:
       

      /**
      * Server enchantments.
      *
      * @see Enchantment#getByKey(org.bukkit.NamespacedKey)
      */
      Registry<Enchantment>ENCHANTMENT=newRegistry<Enchantment>(){
       
      @Nullable
      @Override
      publicEnchantmentget(@NotNullNamespacedKeykey){
      returnEnchantment.getByKey(key);
      }
       
      @NotNull
      @Override
      publicStream<Enchantment>stream(){
      returnStreamSupport.stream(spliterator(),false);
      }
       
      @NotNull
      @Override
      publicIterator<Enchantment>iterator(){
      returnArrays.asList(Enchantment.values()).iterator();
      }
      }; 

       

      Thanks.

       

            Unassigned Unassigned
            tastybento tastybento
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: