Jelajahi Sumber

Alter PreparationPanelTest to properly use mocking/injection for tests.

Sam Jaffe 5 tahun lalu
induk
melakukan
672d67e13a

+ 5 - 2
src/main/lombok/org/leumasjaffe/recipe/view/DurationPanel.java

@@ -12,7 +12,7 @@ import org.leumasjaffe.recipe.model.Duration;
 @SuppressWarnings("serial")
 public class DurationPanel extends JPanel {
 	private JLabel lblTime;
-	public DurationPanel(String name, Duration duration) {
+	public DurationPanel(String name) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -32,7 +32,10 @@ public class DurationPanel extends JPanel {
 		gbc_lblTime.gridx = 1;
 		gbc_lblTime.gridy = 0;
 		add(lblTime, gbc_lblTime);
-		
+	}
+
+	public DurationPanel(String name, Duration duration) {
+		this(name);
 		setModel(duration);
 	}
 

+ 15 - 7
src/main/lombok/org/leumasjaffe/recipe/view/PreparationPanel.java

@@ -5,11 +5,11 @@ import javax.swing.JPanel;
 import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
+import org.leumasjaffe.recipe.model.Duration;
 import org.leumasjaffe.recipe.model.Ingredient;
 import org.leumasjaffe.recipe.model.Preparation;
 
 import lombok.AccessLevel;
-import lombok.Getter;
 import lombok.experimental.FieldDefaults;
 
 import java.awt.GridBagLayout;
@@ -27,9 +27,9 @@ public class PreparationPanel extends JPanel {
 	ReplaceChildrenController<Preparation, Ingredient> controller;
 	ObservableListener<JPanel, Preparation> childListener;
 	
-	@Getter(AccessLevel.PACKAGE) JPanel panelIngredients;
+	DurationPanel panelDuration;
 		
-	public PreparationPanel(Preparation preparation) {
+	public PreparationPanel() {
 		controller = new ReplaceChildrenController<>(Preparation::getIngredients,
 				IngredientPreparationPanel::new);
 		
@@ -69,14 +69,14 @@ public class PreparationPanel extends JPanel {
 		gbc_horizontalGlue.gridy = 0;
 		panelLeft.add(horizontalGlue, gbc_horizontalGlue);
 		
-		DurationPanel panelDuration = new DurationPanel("Requires", preparation.getDuration());
+		panelDuration = new DurationPanel("Requires", Duration.ZERO);
 		GridBagConstraints gbc_panelDuration = new GridBagConstraints();
 		gbc_panelDuration.insets = new Insets(0, 0, 5, 0);
 		gbc_panelDuration.gridx = 2;
 		gbc_panelDuration.gridy = 0;
 		panelLeft.add(panelDuration, gbc_panelDuration);
 		
-		panelIngredients = new JPanel();
+		JPanel panelIngredients = new JPanel();
 		panelIngredients.setLayout(new VerticalLayout(5));
 		GridBagConstraints gbc_panelIngredients = new GridBagConstraints();
 		gbc_panelIngredients.gridwidth = 3;
@@ -85,11 +85,19 @@ public class PreparationPanel extends JPanel {
 		gbc_panelIngredients.gridx = 0;
 		gbc_panelIngredients.gridy = 1;
 		panelLeft.add(panelIngredients, gbc_panelIngredients);
-	
+		
 		// This indirection allows for testing of controller
 		childListener = new ObservableListener<>(panelIngredients,
 				(c, v) -> controller.accept(c, v));
-		
+	}
+	
+	public PreparationPanel(final Preparation preparation) {
+		this();
+		setModel(preparation);
+	}
+	
+	public void setModel(final Preparation preparation) {
+		panelDuration.setModel(preparation.getDuration());
 		childListener.setObserved(preparation);
 	}
 

+ 25 - 23
src/test/java/org/leumasjaffe/recipe/view/PreparationPanelTest.java

@@ -1,58 +1,60 @@
 package org.leumasjaffe.recipe.view;
 
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
 import static org.mockito.Mockito.*;
 
-import java.util.Arrays;
-
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.junit.platform.runner.JUnitPlatform;
 import org.junit.runner.RunWith;
 import org.leumasjaffe.observer.ObserverDispatch;
-import org.leumasjaffe.recipe.model.Amount;
+import org.leumasjaffe.recipe.controller.ReplaceChildrenController;
 import org.leumasjaffe.recipe.model.Duration;
 import org.leumasjaffe.recipe.model.Ingredient;
 import org.leumasjaffe.recipe.model.Preparation;
-import org.mockito.Spy;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
 @ExtendWith(MockitoExtension.class)
 @RunWith(JUnitPlatform.class)
 class PreparationPanelTest extends SwingTestCase {
 	
-	@Spy Preparation stuff;
-	PreparationPanel panel;
+	@Mock DurationPanel panelDuration;
+	@Mock ReplaceChildrenController<Preparation, Ingredient> controller;
+	Preparation stuff;
+	@InjectMocks PreparationPanel panel = new PreparationPanel();
 	
 	@BeforeEach
 	void setUp() {
+		stuff = mock(Preparation.class);
 		Duration dur = new Duration(Duration.Display.SECONDS, 0, 30);
 		doReturn(dur).when(stuff).getDuration();
-		doReturn(Arrays.asList(
-				new Ingredient("Butter", "", new Amount("10 g")),
-				new Ingredient("Salt", "", new Amount("0.25 tsp"))
-				)).when(stuff).getIngredients();
-
-		panel = new PreparationPanel(stuff);
+		
+		panel.setModel(stuff);
 	}
 
 	@Test
 	void testHasContent() {
-		assertThat(panel.getPanelIngredients().getComponents(),
-				arrayWithSize(2));
+		verify(panelDuration).setModel(any());
+		verify(controller, times(1)).accept(any(), same(stuff));
+	}
+
+	@Test
+	void testDoesNotUpdateDurationWhenNotified() {
+		clearInvocations(panelDuration);
+
+		ObserverDispatch.notifySubscribers(stuff);		
+
+		verify(panelDuration, never()).setModel(any());
 	}
 
 	@Test
 	void testUpdatesNumberOfChildrenWhenNotified() {
-		doReturn(Arrays.asList(
-				new Ingredient("Salt", "", new Amount("0.25 tsp"))
-				)).when(stuff).getIngredients();
+		clearInvocations((Object) controller);
 		
-		ObserverDispatch.notifySubscribers(stuff);
-		
-		assertThat(panel.getPanelIngredients().getComponents(),
-				arrayWithSize(1));
+		ObserverDispatch.notifySubscribers(stuff);		
+
+		verify(controller, times(1)).accept(any(), same(stuff));
 	}
 }