@Test public void should_return_context_in_thread_local() throws Exception { context = mock(Http.Context.class); context.args = args; ThreadLocal<Http.Context> tlContext = new ThreadLocal<>(); tlContext.set(context); Whitebox.setInternalState(LogTrackerUtil.class, "context", tlContext); PowerMockito.when(Http.Context.current()).thenThrow(RuntimeException.class); Assert.assertEquals(LogTrackerUtil.getTrackerId(), "123"); }
public static String getTrackerId() throws LogTrackerException { Http.Context ctx; try { ctx = Http.Context.current(); } catch (RuntimeException e) { ctx = LogTrackerUtil.context.get(); } try { return ctx.args.get("TrackerId").toString(); } catch (Exception e) { throw new LogTrackerException("Error occurred when getting the context", e); } }
LogTrackerUtil { public static String getTrackerId() throws LogTrackerException { Http.Context ctx; try { ctx = Http.Context.current(); } catch (RuntimeException e) { ctx = LogTrackerUtil.context.get(); } try { return ctx.args.get("TrackerId").toString(); } catch (Exception e) { throw new LogTrackerException("Error occurred when getting the context", e); } } }
LogTrackerUtil { public static String getTrackerId() throws LogTrackerException { Http.Context ctx; try { ctx = Http.Context.current(); } catch (RuntimeException e) { ctx = LogTrackerUtil.context.get(); } try { return ctx.args.get("TrackerId").toString(); } catch (Exception e) { throw new LogTrackerException("Error occurred when getting the context", e); } }  }
LogTrackerUtil { public static String getTrackerId() throws LogTrackerException { Http.Context ctx; try { ctx = Http.Context.current(); } catch (RuntimeException e) { ctx = LogTrackerUtil.context.get(); } try { return ctx.args.get("TrackerId").toString(); } catch (Exception e) { throw new LogTrackerException("Error occurred when getting the context", e); } }  static String getTrackerId(); static void setContext(Http.Context ctx); static void clearContext(); }
LogTrackerUtil { public static String getTrackerId() throws LogTrackerException { Http.Context ctx; try { ctx = Http.Context.current(); } catch (RuntimeException e) { ctx = LogTrackerUtil.context.get(); } try { return ctx.args.get("TrackerId").toString(); } catch (Exception e) { throw new LogTrackerException("Error occurred when getting the context", e); } }  static String getTrackerId(); static void setContext(Http.Context ctx); static void clearContext();  }