### Question:
FeedbackManager { public void addMenuItems(Toolbox toolbox, SplitButton button) { myToolbox = toolbox; myPreferences = myToolbox.getPreferencesRegistry().getPreferences(FeedbackManager.class); final String provideFeedbackStr = myPreferences.getString(PROVIDE_FEEDBACK, null); if (provideFeedbackStr != null) { final JMenuItem provideFeedbackItem = new JMenuItem("Provide Feedback"); provideFeedbackItem.addActionListener(e -> transmit(provideFeedbackStr)); button.addMenuItem(provideFeedbackItem); } final String reportBugString = myPreferences.getString(REPORT_BUG, null); if (reportBugString != null) { final JMenuItem reportbugItem = new JMenuItem("Report Bug"); reportbugItem.addActionListener((e) -> transmit(reportBugString)); button.addMenuItem(reportbugItem); } final String requestFeatureStr = myPreferences.getString(REQUEST_FEATURE, null); if (requestFeatureStr != null) { final JMenuItem requestFeatureItem = new JMenuItem("Request Feature"); requestFeatureItem.addActionListener(e -> transmit(requestFeatureStr)); button.addMenuItem(requestFeatureItem); } }  void addMenuItems(Toolbox toolbox, SplitButton button); static final String PROVIDE_FEEDBACK; static final String REPORT_BUG; static final String REQUEST_FEATURE; }### Answer:
@Test public void testAddMenuItems() { SplitButton button = new SplitButton(null, null); int initialComponentCount = button.getMenu().getComponentCount(); Set<Component> initialComponents = new HashSet<>(); initialComponents.addAll(Arrays.asList(button.getMenu().getComponents())); Toolbox mockToolbox = createStrictMock(Toolbox.class); PreferencesRegistry mockPreferencesRegistry = createStrictMock(PreferencesRegistry.class); Preferences mockPreferences = createStrictMock(Preferences.class); expect(mockToolbox.getPreferencesRegistry()).andReturn(mockPreferencesRegistry); expect(mockPreferencesRegistry.getPreferences(FeedbackManager.class)).andReturn(mockPreferences); String provideFeedback = "ProvideFeedback"; expect(mockPreferences.getString(FeedbackManager.PROVIDE_FEEDBACK, null)).andReturn(provideFeedback); String reportBug = "ReportBug"; expect(mockPreferences.getString(FeedbackManager.REPORT_BUG, null)).andReturn(reportBug); String requestFeature = "requestFeature"; expect(mockPreferences.getString(FeedbackManager.REQUEST_FEATURE, null)).andReturn(requestFeature); replay(mockToolbox, mockPreferencesRegistry, mockPreferences); myTestObject.addMenuItems(mockToolbox, button); verify(mockToolbox, mockPreferencesRegistry, mockPreferences); assertEquals(3, button.getMenu().getComponentCount() - initialComponentCount); Component[] components = button.getMenu().getComponents(); for (Component component : components) { if (!initialComponents.contains(component)) { assertTrue(component instanceof JMenuItem); } } }
@Test public void testAddMenuItemsNoPrefs() { SplitButton button = new SplitButton(null, null); int initialComponentCount = button.getMenu().getComponentCount(); Toolbox mockToolbox = createStrictMock(Toolbox.class); PreferencesRegistry mockPreferencesRegistry = createStrictMock(PreferencesRegistry.class); Preferences mockPreferences = createStrictMock(Preferences.class); expect(mockToolbox.getPreferencesRegistry()).andReturn(mockPreferencesRegistry); expect(mockPreferencesRegistry.getPreferences(FeedbackManager.class)).andReturn(mockPreferences); expect(mockPreferences.getString(FeedbackManager.PROVIDE_FEEDBACK, null)).andReturn(null); expect(mockPreferences.getString(FeedbackManager.REPORT_BUG, null)).andReturn(null); expect(mockPreferences.getString(FeedbackManager.REQUEST_FEATURE, null)).andReturn(null); replay(mockToolbox, mockPreferencesRegistry, mockPreferences); myTestObject.addMenuItems(mockToolbox, button); verify(mockToolbox, mockPreferencesRegistry, mockPreferences); assertEquals(initialComponentCount, button.getMenu().getComponentCount()); }