### Question:
RateParameterController extends AbstractPetriNetComponentController<RateParameter> { public void setRate(String expression) throws InvalidRateException { String oldRate = component.getExpression(); if (!oldRate.equals(expression)) { FunctionalResults<Double> results = petriNet.parseExpression(expression); if (results.hasErrors()) { throw new InvalidRateException(results.getErrorString("\n")); } component.setExpression(expression); UndoableEdit rateAction = new ChangeRateParameterRate(component, oldRate, expression); registerUndoableEdit(rateAction); } } protected  RateParameterController(RateParameter component, PetriNet petriNet, UndoableEditListener listener); void setRate(String expression);  }### Answer:
@Test public void setRateModifiesRate() throws InvalidRateException { String oldRate = "5"; String newRate = "10"; when(rateParameter.getExpression()).thenReturn(oldRate); when(petriNet.parseExpression(newRate)).thenReturn(new FunctionalResults<>(10.0, new HashSet<String>())); rateParameterController.setRate(newRate); verify(rateParameter).setExpression(newRate); }
@Test public void checksForInvalidRate() throws InvalidRateException { String oldRate = "5"; String newRate = "10"; when(rateParameter.getExpression()).thenReturn(oldRate); when(petriNet.parseExpression(newRate)).thenReturn(new FunctionalResults<>(10.0, new HashSet<String>())); rateParameterController.setRate(newRate); verify(petriNet).parseExpression(newRate); }
@Test public void throwsInvalidRateException() { String oldRate = "5"; String newRate = "fhajsh"; when(rateParameter.getExpression()).thenReturn(oldRate); when(petriNet.parseExpression(newRate)).thenReturn(new FunctionalResults<Double>(-1., Arrays.asList("error"), new HashSet<String>())); try { rateParameterController.setRate(newRate); } catch (InvalidRateException e) { return; } fail("Did not throw InvalidRateException for an invalid rate!"); }
@Test public void setRateCreatesUndoItem() throws InvalidRateException { String oldRate = "5"; String newRate = "10"; when(rateParameter.getExpression()).thenReturn(oldRate); when(petriNet.parseExpression(newRate)).thenReturn(new FunctionalResults<>(10.0, new HashSet<String>())); rateParameterController.setRate(newRate); UndoableEdit changed = new ChangeRateParameterRate(rateParameter, oldRate, newRate); verify(listener).undoableEditHappened(argThat(Contains.thisAction(changed))); }
@Test public void setRateDoesNotModifiyRateIfEqual() throws InvalidRateException { String oldRate = "5"; String newRate = "5"; when(rateParameter.getExpression()).thenReturn(oldRate); rateParameterController.setRate(newRate); verify(rateParameter, never()).setExpression(anyString()); }
@Test public void setRateDoesCreatesUndoItemIfRateEqual() throws InvalidRateException { String oldRate = "5"; String newRate = "5"; when(rateParameter.getExpression()).thenReturn(oldRate); rateParameterController.setRate(newRate); verify(listener, never()).undoableEditHappened(any(UndoableEditEvent.class)); }