Java Reference
In-Depth Information
meter is present. First, we set up the mocks in the @Before method so
that it is executed before each test method:
Download email_27/src/stripesbook/test/plainmock/MyLocalePickerTest.java
package stripesbook.test.plainmock;
import static org.junit.Assert. * ;
import static org.mockito.Mockito. * ;
public class MyLocalePickerTest {
private LocalePicker localePicker;
private HttpServletRequest req;
private HttpSession session;
@Before
public void setup() {
localePicker = new MyLocalePicker();
req = mock(HttpServletRequest. class );
session = mock(HttpSession. class );
stub(req.getSession()).toReturn(session);
}
}
Now, we can test different scenarios by getting Mockito to stub the
request and session methods to return specific values and testing that
MyLocalePicker returns the correct Locale . Here's how we verify the result
when the request contains "fr" :
Download email_27/src/stripesbook/test/plainmock/MyLocalePickerTest.java
@Test
public void testLocaleFrInRequest() {
stub(req.getParameter(MyLocalePicker.LOCALE)).toReturn("fr");
Locale locale = localePicker.pickLocale(req);
assertEquals(Locale.FRENCH, locale);
}
Testing the result with a value of "fr" in the session instead of the request
is done in a similar manner:
Download email_27/src/stripesbook/test/plainmock/MyLocalePickerTest.java
@Test
public void testLocaleFrInSession() {
stub(session.getAttribute(MyLocalePicker.LOCALE)).toReturn("fr");
Locale locale = localePicker.pickLocale(req);
assertEquals(Locale.FRENCH, locale);
}
 
Search WWH ::




Custom Search