Skip to content

Commit

Permalink
Add MainTest and update pom.xml for testing
Browse files Browse the repository at this point in the history
Created a new MainTest class in the test directory and added configurations for JUnit and Mockito to pom.xml for testing purposes. Also made slight adjustments to the Main class in the src/main directory to ensure compatibility with the testing environment.
  • Loading branch information
SmartGecko44 committed Dec 4, 2023
1 parent a65967e commit f60f019
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,29 @@
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
7 changes: 5 additions & 2 deletions src/main/java/org/gecko/wauh/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public final class Main extends JavaPlugin {
private WaterBucketListener waterBucketListener;
private TNTListener tntListener;
private CreeperListener creeperListener;
private ConfigurationManager configManager;
private FileConfiguration config;
ConfigurationManager configManager;
FileConfiguration config;

@Override
public void onEnable() {
Expand Down Expand Up @@ -119,4 +119,7 @@ public TNTListener getTntListener() {
public CreeperListener getCreeperListener() {
return creeperListener;
}
public ConfigurationManager getConfigManager() {
return configManager;
}
}
71 changes: 71 additions & 0 deletions src/test/java/org/gecko/wauh/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.gecko.wauh;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.gecko.wauh.data.ConfigurationManager;
import org.gecko.wauh.listeners.*;
import org.gecko.wauh.commands.SetRadiusLimitCommand;
import org.gecko.wauh.commands.StopWauh;
import org.gecko.wauh.commands.ToggleRemovalView;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

public class MainTest {

private Main plugin;
private FileConfiguration config;
private ConfigurationManager configManager;
private Server server;
private PluginManager pluginManager;
private ConsoleCommandSender sender;

@Test
public void testOnEnable() {

plugin = Mockito.spy(Main.class);
server = mock(Server.class);
pluginManager = mock(PluginManager.class);
sender = mock(ConsoleCommandSender.class);
config = mock(FileConfiguration.class);
configManager = new ConfigurationManager(plugin);

// Mock Plugin methods
when(plugin.getServer()).thenReturn(server);
when(server.getPluginManager()).thenReturn(pluginManager);
when(server.getConsoleSender()).thenReturn(sender);

when(plugin.getCommand(anyString())).thenReturn(null);

// Mock ConfigurationManager methods
when(config.getInt(anyString(), anyInt())).thenReturn(10);

// Mock Main methods
when(plugin.getConfigManager()).thenReturn(configManager);
when(configManager.getConfig()).thenReturn(config);

plugin.onEnable();

// Verify methods called
verify(plugin, times(6)).getServer();
verify(server, times(6)).getPluginManager();
verify(server, times(2)).getConsoleSender();
verify(sender, times(2)).sendMessage(anyString());
verify(plugin, times(3)).getCommand(anyString());

// Verify listeners registration
verify(pluginManager).registerEvents(any(BucketListener.class), eq(plugin));
verify(pluginManager).registerEvents(any(BarrierListener.class), eq(plugin));
verify(pluginManager).registerEvents(any(BedrockListener.class), eq(plugin));
verify(pluginManager).registerEvents(any(WaterBucketListener.class), eq(plugin));
verify(pluginManager).registerEvents(any(TNTListener.class), eq(plugin));
verify(pluginManager).registerEvents(any(CreeperListener.class), eq(plugin));
}
}

0 comments on commit f60f019

Please sign in to comment.