我在RssServiceTest service.start();
行上的方法testStartEnbaledFeed上获得了NullPointerException,然后在RssService job.addJobChangeListener(new JobChangeAdapter() {
关键问题似乎是job.addChangelistener方法是真实的。有谁知道如何解决它?下面列出了RssServiceTest和RssService类:
package org.eclipse.recommenders.internal.news.rcp;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
import org.eclipse.mylyn.commons.notifications.core.NotificationEnvironment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.google.common.collect.ImmutableList;
import com.google.common.eventbus.EventBus;
@SuppressWarnings("restriction")
@RunWith(MockitoJUnitRunner.class)
public class RssServiceTest {
private static final String FIRST_ELEMENT = "first";
private static final String SECOND_ELEMENT = "second";
@Test
public void testStartEnabledFeed() {
FeedDescriptor feed = FeedDescriptorsTest.enabled(FIRST_ELEMENT);
PollFeedJob job = mock(PollFeedJob.class);
JobProvider provider = mock(JobProvider.class);
NewsRcpPreferences preferences = mock(NewsRcpPreferences.class);
when(preferences.isEnabled()).thenReturn(true);
when(preferences.getFeedDescriptors()).thenReturn(ImmutableList.of(feed));
NotificationEnvironment environment = mock(NotificationEnvironment.class);
EventBus bus = mock(EventBus.class);
when(provider.getPollFeedJob(Mockito.eq(feed), Mockito.eq(preferences), Mockito.eq(environment)))
.thenReturn(job);
RssService service = new RssService(preferences, bus, environment, provider);
assertThat(preferences, is(notNullValue()));
assertThat(bus, is(notNullValue()));
assertThat(environment, is(notNullValue()));
assertThat(provider, is(notNullValue()));
assertThat(job, is(notNullValue()));
assertThat(feed, is(notNullValue()));
assertThat(service, is(notNullValue()));
service.start();
}
}
`
package org.eclipse.recommenders.internal.news.rcp;
import static java.lang.Long.parseLong;
import static org.eclipse.recommenders.internal.news.rcp.FeedEvents.createNewFeedItemsEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.mylyn.commons.notifications.core.NotificationEnvironment;
import org.eclipse.recommenders.internal.news.rcp.FeedEvents.FeedMessageReadEvent;
import org.eclipse.recommenders.news.rcp.IFeedMessage;
import org.eclipse.recommenders.news.rcp.IRssService;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@SuppressWarnings("restriction")
public class RssService implements IRssService {
private static final long DEFAULT_DELAY = TimeUnit.DAYS.toMinutes(1);
private static final long START_DELAY = 0;
private final NewsRcpPreferences preferences;
private final EventBus bus;
private final NotificationEnvironment environment;
private final JobProvider provider;
private final Set<String> readIds;
private final HashMap<FeedDescriptor, List<IFeedMessage>> groupedMessages = Maps.newHashMap();
public RssService(NewsRcpPreferences preferences, EventBus bus, NotificationEnvironment environment,
JobProvider provider) {
this.preferences = preferences;
this.bus = bus;
this.environment = environment;
this.provider = provider;
bus.register(this);
readIds = ReadFeedMessagesProperties.getReadIds();
}
@Override
public void start() {
for (final FeedDescriptor feed : preferences.getFeedDescriptors()) {
if (feed.isEnabled()) {
start(feed);
}
}
}
@Override
public void start(final FeedDescriptor feed) {
// final PollFeedJob job = new PollFeedJob(feed, preferences, environment);
final PollFeedJob job = provider.getPollFeedJob(feed, preferences, environment);
job.setSystem(true);
job.setPriority(Job.DECORATE);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
boolean newMessage = false;
if (!groupedMessages.containsKey(feed)) {
groupedMessages.put(feed, Lists.<IFeedMessage>newArrayList());
}
List<IFeedMessage> feedMessages = groupedMessages.get(feed);
for (IFeedMessage message : job.getMessages()) {
if (!feedMessages.contains(message)) {
feedMessages.add(message);
if (!readIds.contains(message.getId())) {
newMessage = true;
}
}
}
if (groupedMessages.size() > 0 && newMessage) {
bus.post(createNewFeedItemsEvent());
}
if (!preferences.isEnabled() || !isFeedEnabled(feed)) {
return;
}
if (feed.getPollingInterval() != null) {
job.schedule(TimeUnit.MINUTES.toMillis(parseLong(feed.getPollingInterval())));
return;
}
job.schedule(TimeUnit.MINUTES.toMillis(DEFAULT_DELAY));
}
});
if (!provider.jobExists(feed, preferences, environment)) {
job.schedule(START_DELAY);
}
}
@Override
public Map<FeedDescriptor, List<IFeedMessage>> getMessages(final int countPerFeed) {
Map<FeedDescriptor, List<IFeedMessage>> transformedMap = Maps.transformValues(groupedMessages,
new Function<List<IFeedMessage>, List<IFeedMessage>>() {
@Override
public List<IFeedMessage> apply(List<IFeedMessage> input) {
return FluentIterable.from(input).limit(countPerFeed).filter(new Predicate<IFeedMessage>() {
@Override
public boolean apply(IFeedMessage input) {
return !readIds.contains(input.getId());
}
}).toList();
}
});
Map<FeedDescriptor, List<IFeedMessage>> filteredMap = Maps.filterValues(transformedMap,
new Predicate<List<IFeedMessage>>() {
@Override
public boolean apply(List<IFeedMessage> input) {
if (input == null) {
return false;
}
return !input.isEmpty();
}
});
return ImmutableMap.copyOf(filteredMap);
}
private boolean isFeedEnabled(FeedDescriptor feed) {
for (FeedDescriptor fd : preferences.getFeedDescriptors()) {
if (feed.getId().equals(fd.getId())) {
return true;
}
}
return false;
}
@Subscribe
public void handle(FeedMessageReadEvent event) {
readIds.add(event.getId());
ReadFeedMessagesProperties.writeReadIds(readIds);
}
}
`
java.lang.NullPointerException
at org.eclipse.core.internal.jobs.InternalJob.addJobChangeListener(InternalJob.java:161)
at org.eclipse.core.runtime.jobs.Job.addJobChangeListener(Job.java:182)
at org.eclipse.recommenders.internal.news.rcp.RssService.start(RssService.java:77)
at org.eclipse.recommenders.internal.news.rcp.RssService.start(RssService.java:66)
at org.eclipse.recommenders.internal.news.rcp.RssServiceTest.testStartEnabledFeed(RssServiceTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
at org.eclipse.pde.internal.junit.runtime.PlatformUITestHarness$1.run(PlatformUITestHarness.java:47)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:136)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4147)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3764)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:832)
at org.eclipse.jface.window.Window.open(Window.java:808)
at org.eclipse.jface.dialogs.MessageDialog.open(MessageDialog.java:341)
at org.eclipse.recommenders.internal.rcp.RcpModule$2.run(RcpModule.java:248)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:136)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4147)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3764)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:135)
at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runApp(NonUIThreadTestApplication.java:54)
at org.eclipse.pde.internal.junit.runtime.UITestApplication.runApp(UITestApplication.java:47)
at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.start(NonUIThreadTestApplication.java:48)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
答案 0 :(得分:1)
我要做的第一件事是改变
when(provider.getPollFeedJob(Mockito.eq(feed), Mockito.eq(preferences), Mockito.eq(environment)))
.thenReturn(job);
到
when(provider.getPollFeedJob(any(FeedDescriptor.class), any(NewsRcpPreferences.class), any(NotificationEnvironment.class)))
.thenReturn(job);
这只是为了确保您的提供商实际上是在为您提供模拟工作。
然后我会添加一个
doNothing().when(job).addJobChangeListener(any(JobChangeAdapter.class));
或者你可以先做第二个。无论哪种方式,你都可以试试这两件事。