Skip to content

Commit

Permalink
Fix tests after mockito upgrade.
Browse files Browse the repository at this point in the history
Mockito now errors if a mock is unnecessary, plus a few other minor
API changes.
  • Loading branch information
csmith committed Oct 28, 2016
1 parent 9bb1d26 commit 232ee99
Show file tree
Hide file tree
Showing 21 changed files with 33 additions and 147 deletions.
5 changes: 0 additions & 5 deletions src/test/java/com/dmdirc/ServerManagerTest.java
Expand Up @@ -61,7 +61,6 @@ public class ServerManagerTest {
@Mock private IdentityFactory identityFactory;
@Mock private ConfigProviderMigrator configProviderMigrator;
@Mock private Profile profile;
@Mock private AggregateConfigProvider configProvider;
@Mock private WindowManager windowManager;
@Mock private ServerFactoryImpl serverFactoryImpl;
@Mock private Server server;
Expand All @@ -79,14 +78,11 @@ public void setUp() throws Exception {
serverManager = new ServerManager(profileManager, identityFactory, windowManager, serverFactoryImpl, eventBus);

when(server.getState()).thenReturn(ServerState.DISCONNECTED);
when(server.getGroupChatManager()).thenReturn(groupChatManager);
when(server.getWindowModel()).thenReturn(windowModel);

when(profileManager.getProfiles()).thenReturn(Collections.singletonList(profile));
when(profileManager.getDefault()).thenReturn(profile);
when(identityFactory.createMigratableConfig(anyString(), anyString(), anyString(),
anyString())).thenReturn(configProviderMigrator);
when(configProviderMigrator.getConfigProvider()).thenReturn(configProvider);

when(serverFactoryImpl.getServer(eq(configProviderMigrator),
any(ScheduledExecutorService.class), uriCaptor.capture(), eq(profile)))
Expand Down Expand Up @@ -194,7 +190,6 @@ public void testDevChatNoServers() {

final Server serverB = mock(Server.class);
when(serverB.isNetwork("Quakenet")).thenReturn(false);
when(serverB.getState()).thenReturn(ServerState.CONNECTED);

serverManager.registerServer(serverA);
serverManager.registerServer(serverB);
Expand Down
Expand Up @@ -55,7 +55,6 @@ public class AliasCommandHandlerTest {
@Mock private CommandController commandController;
@Mock private CommandParser commandParser;
@Mock private CommandContext context;
@Mock private CommandInfo commandInfo;
@Mock private EventBus eventbus;
@Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;

Expand All @@ -65,8 +64,6 @@ public void setup() {
when(inputModel.getCommandParser()).thenReturn(commandParser);
when(commandController.getCommandChar()).thenReturn('#');
when(commandController.getSilenceChar()).thenReturn('/');
when(context.getSource()).thenReturn(container);
when(context.getCommandInfo()).thenReturn(commandInfo);
when(container.getEventBus()).thenReturn(eventbus);
}

Expand Down
Expand Up @@ -32,8 +32,6 @@
import com.dmdirc.interfaces.GroupChatUser;
import com.dmdirc.interfaces.User;
import com.dmdirc.interfaces.WindowModel;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.parser.interfaces.ChannelInfo;

import java.util.Optional;

Expand Down Expand Up @@ -61,20 +59,14 @@ public class KickReasonTest {
@Mock private CommandController controller;
@Mock private WindowModel container;
@Mock private EventBus eventbus;
@Mock private ChannelInfo channelInfo;
@Mock private AggregateConfigProvider configProvider;
@Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
private KickReason command;

@Before
public void setup() {
when(container.getConfigManager()).thenReturn(configProvider);
when(configProvider.getOption("general", "kickmessage")).thenReturn("KICKREASON");
when(channel.getChannelInfo()).thenReturn(channelInfo);
when(channel.getConnection()).thenReturn(Optional.of(connection));
when(connection.getUser("user")).thenReturn(user1);
when(connection.getUser("user1")).thenReturn(user2);
when(groupChatUser.getHostname()).thenReturn(Optional.of("HOSTNAME"));
when(channel.getUser(user1)).thenReturn(Optional.of(groupChatUser));
when(channel.getUser(user2)).thenReturn(Optional.empty());
when(controller.getCommandChar()).thenReturn('/');
Expand Down
Expand Up @@ -46,7 +46,6 @@
public class NamesTest {

@Mock private CommandController controller;
@Mock private ChannelInfo channelinfo;
@Mock private Channel channel;
@Mock private Connection connection;
@Mock private Parser parser;
Expand All @@ -57,7 +56,6 @@ public class NamesTest {
public void setUp() throws InvalidIdentityFileException {
when(channel.getConnection()).thenReturn(Optional.of(connection));
when(connection.getParser()).thenReturn(Optional.of(parser));
when(channel.getChannelInfo()).thenReturn(channelinfo);
when(channel.getName()).thenReturn("#chan");

command = new Names(controller);
Expand Down
Expand Up @@ -50,7 +50,6 @@ public class MeTest {

@Before
public void setUp() {
when(chat.getWindowModel()).thenReturn(frameContainer);
when(frameContainer.getEventBus()).thenReturn(eventbus);
command = new Me(controller);
}
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
Expand All @@ -56,7 +57,6 @@ public class NewServerTest {
@Mock private EventBus eventBus;
@Mock private CommandController controller;
@Mock private ProfileManager profileManager;
@Mock private Profile identity;
@Mock private WindowModel container;
@Mock private ServiceManager serviceManager;
@Mock private ConnectionFactory factory;
Expand All @@ -66,9 +66,7 @@ public class NewServerTest {
@Before
public void setup() {
when(container.getEventBus()).thenReturn(eventBus);
when(factory.createServer(any(URI.class), any(Profile.class))).thenReturn(connection);
when(profileManager.getProfiles()).thenReturn(
Collections.singletonList(identity));
when(factory.createServer(any(URI.class), isNull())).thenReturn(connection);
command = new NewServer(controller, factory, serviceManager, profileManager, new URIParser());
}

Expand All @@ -77,7 +75,7 @@ public void testBasicUsage() throws URISyntaxException {
command.execute(container, new CommandArguments(controller, "/foo irc.foo.com"),
new CommandContext(null, NewServer.INFO));

verify(factory).createServer(eq(new URI("irc://irc.foo.com")), any(Profile.class));
verify(factory).createServer(eq(new URI("irc://irc.foo.com")), isNull());
verify(connection).connect();
}

Expand All @@ -86,7 +84,7 @@ public void testPortUsage() throws URISyntaxException {
command.execute(container, new CommandArguments(controller, "/foo irc.foo.com:1234"),
new CommandContext(null, NewServer.INFO));

verify(factory).createServer(eq(new URI("irc://irc.foo.com:1234")), any(Profile.class));
verify(factory).createServer(eq(new URI("irc://irc.foo.com:1234")), isNull());
verify(connection).connect();
}

Expand All @@ -95,7 +93,7 @@ public void testUriUsage() throws URISyntaxException {
command.execute(container, new CommandArguments(controller, "/foo otheruri://foo.com:123/blah"),
new CommandContext(null, NewServer.INFO));

verify(factory).createServer(eq(new URI("otheruri://foo.com:123/blah")), any(Profile.class));
verify(factory).createServer(eq(new URI("otheruri://foo.com:123/blah")), isNull());
verify(connection).connect();
}

Expand Down
Expand Up @@ -81,7 +81,6 @@ public void setup() {
when(container.getConnection()).thenReturn(Optional.of(connection));
when(connection.getGroupChatManager()).thenReturn(groupChatManager);
when(groupChatManager.isValidChannelName("#channel1")).thenReturn(true);
when(groupChatManager.isValidChannelName("#channel2")).thenReturn(true);
when(groupChatManager.getChannel("#channel1")).thenReturn(Optional.of(channel));

commandParser = new TestCommandParser(configProvider, commandController, eventBus);
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/com/dmdirc/config/ConfigValueRetrieverTest.java
Expand Up @@ -65,8 +65,6 @@ public void testRetrievesBasicString() {

@Test
public void testRetrievesStringWithFallback() {
when(configProvider.getOptionString(DOMAIN, KEY, true,
ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
when(configProvider.getOptionString(
DOMAIN, KEY, true, ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR,
FALLBACK_DOMAIN, FALLBACK_KEY))
Expand All @@ -80,16 +78,12 @@ public void testRetrievesStringWithFallback() {
public void testRetrievesStringWithRequiresFalse() {
when(configProvider.getOptionString(DOMAIN, KEY, false,
ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
when(configProvider.getOptionString(DOMAIN, KEY, true,
ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn("value!");

assertNull(retriever.getValue(String.class, DOMAIN, KEY, false));
}

@Test
public void testRetrievesStringWithRequiresTrue() {
when(configProvider.getOptionString(DOMAIN, KEY, false,
ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn(null);
when(configProvider.getOptionString(DOMAIN, KEY, true,
ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR)).thenReturn("value!");

Expand Down
Expand Up @@ -66,7 +66,6 @@ public void setup() {
@Test
public void testDefaults() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");
final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
assertNotNull(pm.getCategory("General"));
Expand All @@ -82,7 +81,6 @@ public void testDefaults() {
@Test
public void testDismiss() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");
final PreferencesCategory category = mock(PreferencesCategory.class);
final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
Expand All @@ -97,8 +95,6 @@ public void testSaveNoRestart() {
final PreferencesCategory category = mock(PreferencesCategory.class);
when(category.save()).thenReturn(false);
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
pm.addCategory(category);
Expand All @@ -112,8 +108,6 @@ public void testSaveRestart() {
final PreferencesCategory category = mock(PreferencesCategory.class);
when(category.save()).thenReturn(true);
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
pm.addCategory(category);
Expand All @@ -125,8 +119,6 @@ public void testSaveRestart() {
@Test
public void testGetCategory() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
assertNull(pm.getCategory("unittest123"));
Expand All @@ -135,8 +127,6 @@ public void testGetCategory() {
@Test
public void testGetCategories() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
assertNotNull(pm.getCategories());
Expand All @@ -150,8 +140,6 @@ public void testGetCategories() {
@Test
public void testSaveListener() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
final PreferencesInterface tpi = mock(PreferencesInterface.class);
Expand All @@ -164,7 +152,6 @@ public void testSaveListener() {
@Test
public void testOpenAction() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

new PreferencesDialogModel(null, null, null, null, cm, null, serviceManager, eventBus);

Expand All @@ -174,7 +161,6 @@ public void testOpenAction() {
@Test
public void testCloseAction() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");
final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
pm.close();
Expand All @@ -185,8 +171,6 @@ public void testCloseAction() {
@Test
public void testCategoryObjectSaveListeners() {
final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
when(cm.getOption("domain", "option")).thenReturn("fallback");

final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
null, null, cm, null, serviceManager, eventBus);
final PreferencesCategory category = mock(PreferencesCategory.class);
Expand Down
Expand Up @@ -56,7 +56,6 @@ public void setup() {
instance = new IdentitiesProfileMigrator(identityManager, profileManager);
when(configProvider1.getName()).thenReturn("name1");
when(configProvider1.getOption("profile", "realname")).thenReturn("realname1");
when(configProvider1.hasOptionString("profile", "ident1")).thenReturn(false);
when(configProvider1.getOptionList("profile", "nicknames")).thenReturn(
Lists.newArrayList("nickname1")
);
Expand Down
54 changes: 0 additions & 54 deletions src/test/java/com/dmdirc/harness/CommandArgsMatcher.java

This file was deleted.

Expand Up @@ -62,7 +62,6 @@ public class DiskLoggingErrorManagerTest {
public void setUp() throws Exception {
when(error.getTimestamp()).thenReturn(LocalDateTime.now());
when(error.getError()).thenReturn(programError);
when(programError.getThrowable()).thenReturn(Optional.of(new IllegalArgumentException()));
when(programError.getThrowableAsString()).thenReturn(Optional.of("test"));
when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
when(config.getBinder()).thenReturn(configBinder);
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -77,8 +78,7 @@ public void setUp() throws Exception {
@Test
public void testHandleErrorEvent() throws Exception {
instance.handleErrorEvent(appErrorEvent);
verify(sentryErrorReporter).sendException(anyString(), any(ErrorLevel.class),
any(LocalDateTime.class), any());
verify(sentryErrorReporter).sendException(isNull(), isNull(), isNull(), any());
}

@Test
Expand Down Expand Up @@ -120,8 +120,7 @@ public void testSendReports_Submit_NoReporting() throws Exception {
instance.handleSubmitErrors(true);
instance.handleNoErrorReporting(false);
instance.handleErrorEvent(appErrorEvent);
verify(sentryErrorReporter).sendException(anyString(), any(ErrorLevel.class),
any(LocalDateTime.class), any());
verify(sentryErrorReporter).sendException(isNull(), isNull(), isNull(), any());
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/dmdirc/ui/WindowManagerTest.java
Expand Up @@ -252,7 +252,6 @@ public void testFindCustomWindowWithParent() {
final CustomWindow customContainer = mock(CustomWindow.class);
final CustomWindow customChild = mock(CustomWindow.class);

when(customContainer.getName()).thenReturn("test");
when(customChild.getName()).thenReturn("test1");

manager.addWindow(customContainer);
Expand All @@ -266,7 +265,6 @@ public void testFindCustomWindowWithParentNotFound() {
final CustomWindow customContainer = mock(CustomWindow.class);
final CustomWindow customChild = mock(CustomWindow.class);

when(customContainer.getName()).thenReturn("test");
when(customChild.getName()).thenReturn("test1");

manager.addWindow(customContainer);
Expand Down
Expand Up @@ -66,7 +66,6 @@ public class CoreAliasDialogModelTest {
@Before
public void setupAliases() {
aliases = new HashSet<>();
when(commandController.getCommandChar()).thenReturn('/');
}

@Before
Expand Down

0 comments on commit 232ee99

Please sign in to comment.