Mockito: Java - Is it possible to Change a variable value used in an InjectMocks class?
<p>I am wondering if this is possible. I have tried a few implementations using @mock and @spy to create a new string that has the same variable name as the one in a injectMocked class however I get the following error:</p>

<pre><code>Cannot mock/spy class java.lang.String
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
</code></pre>

<p>In the class that is being used in InjectMocked I have:</p>

<pre><code>public class Service{

   String url = getUrl();

   ...
}
</code></pre>

<p>However I want to use a different url for testing as we have one for a testing environment.</p>

<p>I have tried things such as:</p>

<pre><code>@Mock
private String url = "myUrlString";

@Spy
private String url = "myUrlString";
</code></pre>

<p>What I want is for when I run my Test the new value for <strong>url</strong> will be injected into the inJectMock and will be used instead of the other one. </p>

<p>Example: </p>

<pre><code>@RunWith(MockitoJUnitRunner.class)
public class ServiceTest{
    @Mock // or similar
    private String url = "http://....";

    @InjectMocks
    private Service service = new Service();
}
</code></pre>

<p>So when the test runs the class is like this:</p>

<pre><code>public class Service{

   // Uses the inject url instead of the method that it originally uses
   String url = "http://....";

   ...
}
</code></pre>

<p>Is this possible? If so how and if not how come? I can't be the only person to think to do this, however I cannot find any documentation on it. </p>