### Question:
HttpServletUserAgentProvider implements Provider<UserAgent> { public UserAgent get() { HttpServletRequest req = reqProvider.get(); if (req != null) { String userAgent = req.getHeader("User-Agent"); if (userAgent != null) { return uaParser.parse(userAgent); } } return null; } @Inject  HttpServletUserAgentProvider(UserAgent.Parser uaParser,
      Provider<HttpServletRequest> reqProvider); UserAgent get();  }### Answer:
@Test public void testProviderWorks() { String agentVersion = "AGENT_VERSION"; HttpServletRequest req = mock(HttpServletRequest.class); expect(req.getHeader("User-Agent")).andReturn(agentVersion).once(); replay(); HttpServletUserAgentProvider provider = new HttpServletUserAgentProvider( parser, new HttpServletRequestProvider(req)); UserAgent entry = provider.get(); assertEquals(UserAgent.Browser.OTHER, entry.getBrowser()); assertEquals(agentVersion, entry.getVersion()); verify(); }
@Test public void testNoRequestGetsNull() { HttpServletUserAgentProvider provider = new HttpServletUserAgentProvider( parser, new HttpServletRequestProvider(null)); assertNull(provider.get()); }
@Test public void testNoUserAgentGetsNull() { HttpServletRequest req = mock(HttpServletRequest.class); expect(req.getHeader("User-Agent")).andReturn(null).once(); replay(); HttpServletUserAgentProvider provider = new HttpServletUserAgentProvider( parser, new HttpServletRequestProvider(req)); assertNull(provider.get()); verify(); }