Skip to content

Commit

Permalink
Extract mock creation to utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBailey committed Jul 8, 2024
1 parent aca7e89 commit 0bf3424
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import org.jetbrains.annotations.NotNull;

/**
* Defines a channel and how it should be configured.
*/
public interface ChannelDefinition {

String name();
Expand Down
19 changes: 19 additions & 0 deletions terra/src/test/java/dev/jacksonbailey/wheel/terra/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import java.util.concurrent.Future;
import java.util.function.Consumer;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;

public final class TestUtils {

Expand All @@ -30,4 +35,18 @@ public static <T> Future<T> anyFutureOf(T t) {
return toReturn;
}

public static TextChannel mockTextChannel(String channelName) {
var channel = mock(TextChannel.class);
given(channel.getType()).willReturn(ChannelType.TEXT);
given(channel.getName()).willReturn(channelName);
return channel;
}

public static VoiceChannel mockVoiceChannel(String channelName) {
var channel = mock(VoiceChannel.class);
given(channel.getType()).willReturn(ChannelType.VOICE);
given(channel.getName()).willReturn(channelName);
return channel;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package dev.jacksonbailey.wheel.terra.model;

import static dev.jacksonbailey.wheel.terra.TestUtils.mockTextChannel;
import static dev.jacksonbailey.wheel.terra.TestUtils.mockVoiceChannel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import org.junit.jupiter.api.Test;

class ChannelDefinitionTest {

@Test
void testTextChannel() {
var textChannel = mock(TextChannel.class);
var textChannelName = "text channel";
given(textChannel.getName()).willReturn(textChannelName);
given(textChannel.getType()).willReturn(ChannelType.TEXT);
var textChannel = mockTextChannel(textChannelName);

var definition = ChannelDefinition.from(textChannel);

Expand All @@ -30,10 +25,8 @@ void testTextChannel() {

@Test
void testVoiceChannel() {
var voiceChannel = mock(VoiceChannel.class);
var voiceChannelName = "voice channel";
given(voiceChannel.getName()).willReturn(voiceChannelName);
given(voiceChannel.getType()).willReturn(ChannelType.VOICE);
var voiceChannel = mockVoiceChannel(voiceChannelName);

var definition = ChannelDefinition.from(voiceChannel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package dev.jacksonbailey.wheel.terra.model;

import static dev.jacksonbailey.wheel.terra.TestUtils.mockTextChannel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.junit.jupiter.api.Test;

class TextChannelDefinitionTest {

@Test
void test() {
var textChannel = mock(TextChannel.class);
var textChannelName = "text channel";
given(textChannel.getName()).willReturn(textChannelName);
given(textChannel.getType()).willReturn(ChannelType.TEXT);
var textChannel = mockTextChannel(textChannelName);

var definition = TextChannelDefinition.from(textChannel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package dev.jacksonbailey.wheel.terra.model;

import static dev.jacksonbailey.wheel.terra.TestUtils.mockVoiceChannel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import org.junit.jupiter.api.Test;

class VoiceChannelDefinitionTest {

@Test
void test() {
var voiceChannel = mock(VoiceChannel.class);
var voiceChannelName = "voice channel";
given(voiceChannel.getName()).willReturn(voiceChannelName);
given(voiceChannel.getType()).willReturn(ChannelType.VOICE);
var voiceChannel = mockVoiceChannel(voiceChannelName);

var definition = ChannelDefinition.from(voiceChannel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jacksonbailey.wheel.terra.service;

import static dev.jacksonbailey.wheel.terra.TestUtils.mockTextChannel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
Expand All @@ -9,8 +10,6 @@
import java.util.Set;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.junit.jupiter.api.Test;
Expand All @@ -31,10 +30,8 @@ void test() {
var event = mock(SlashCommandInteractionEvent.class);
var guild = mock(Guild.class);
given(event.getGuild()).willReturn(guild);
var textChannel = mock(TextChannel.class);
var textChannelName = "text channel";
given(textChannel.getName()).willReturn(textChannelName);
given(textChannel.getType()).willReturn(ChannelType.TEXT);
var textChannel = mockTextChannel(textChannelName);
List<GuildChannel> channels = List.of(textChannel);
given(guild.getChannels()).willReturn(channels);

Expand Down

0 comments on commit 0bf3424

Please sign in to comment.