title: Undoing Whitebox.setInternalState in a @AfterMethod without setting original state
body: <p>I have a class which I cannot easily use a dependency injection to mock due to an obligatory implementation of an interface - in a nutshell, for that reason I will be using Whitebox and my concern here is not related to the design, instead it is just to figure out how to properly "tearDown" the behavior caused by Whitebox. Bear with me for a second, I will give you more details - this is the main dummy class:</p>
  
  <pre><code>public class Dummy implements MandatoryInterface {
      private static final Logger logger = Logger.getLogger(MethodHandles.lookup().lookupClass());
      private final ObjectMapper mapper = new ObjectMapper();
  
      @Override
      public Object convertArgumentToJson(Object arg) {
          if (arg != null) {
              try {
                  return mapper.writeValueAsString(arg);
              } catch (IOException e) {
                  // doSomething();
                  logger.error("Error tracking request", e);
              }
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p>Supposing that I want to cover what happens if an exception occurs here, the only way that I see is to use a <em>Whitebox.setInternalState</em>. Here the test:</p>
  
  <pre><code>public class DummyTest {
      private Dummy dummy = new Dummy();
  
      @Test
      public void testA() throws IOException {
  
          final ObjectMapper mock = Mockito.mock(ObjectMapper.class);
          Whitebox.setInternalState(dummy, "mapper", mock);
  
  
          Mockito.when(mock.writeValueAsString(Mockito.any()))
                   .thenThrow(new IOException());
  
          Assert.assertNull(dummy.convertArgumentToJson("testA"));
  
      }
  
      @Test
      public void testB() {
          Assert.assertNotNull(dummy.convertArgumentToJson("testB"));
      }
  
  }
  </code></pre>
  
  <p>As you can see, I cannot define the mapper within the Dummy class as static, because of the Whitebox (it wouldn't work).
  Having said that, after the execution of <em>testA()</em> we have the mapper being mocked:</p>
  
  <p><a href="https://i.stack.imgur.com/OMO4k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OMO4k.png" alt="enter image description here"></a></p>
  
  <p>The problem is: when executing the <em>testB</em> I don't want mock anymore - it should be the old instanced ObjectMapper initially included in Dummy. But what appears:</p>
  
  <p><a href="https://i.stack.imgur.com/o4bA8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o4bA8.png" alt="enter image description here"></a></p>
  
  <p>Now, my question:</p>
  
  <p>What is the proper way to undo the </p>
  
  <pre><code> Whitebox.setInternalState(dummy, "mapper", mock);
  </code></pre>
  
  <p>P.S.: I have considered using a tearDown() like this:</p>
  
  <pre><code>@AfterMethod
  public void tearDown(){
      Whitebox.setInternalState(dummy, "mapper", originalState);
  }
  </code></pre>
  
  <p>However, in this scenario, my pitest (mutation test) would consider that I'm not covering the initialization of ObjectMapper, so: is there a way just to undo the Whitebox for the rest of the tests without setting manually the old one?</p>
  
  <p>Sorry for the long description and thanks in advance.</p>
  
  <p>Regards,</p>