title: How to mock an object (InetAddress.getLocalHost.getHostName) in Scala
body: <p>I have some code like this:</p>
  <pre><code>import java.net.{InetAddress, UnknownHostException}
  import org.junit.Test
  import org.junit.runner.RunWith
  import org.mockito.BDDMockito.`given`
  import org.mockito.Mock
  import org.powermock.api.mockito.PowerMockito.mockStatic
  import org.powermock.core.classloader.annotations.PrepareForTest
  import org.powermock.modules.junit4.PowerMockRunner
  import org.scalatest.FunSuite
  class XYZ {
    def getAppendedName(prependage: String): String = {
      s&quot;$prependage ${InetAddress.getLocalHost.getHostName}&quot;
    }
  }
  
  @RunWith(classOf[PowerMockRunner])
  class SomeTestSuite extends FunSuite {
  
    @Mock val inetAddress: InetAddress = InetAddress.getLocalHost
  
    @Test
    @throws[UnknownHostException]
    def shouldReturnMocked(): Unit = { //given
      mockStatic(classOf[InetAddress])
      given(InetAddress.getLocalHost).willReturn(inetAddress)
      given(inetAddress.getHostName).willReturn(&quot;mocked&quot;)
      //when
      val name = (new XYZ).getAppendedName(&quot;is&quot;)
      //then
      assert(name == &quot;is mocked&quot;)
    }
  }
  </code></pre>
  <p>And ... I have modeled this code after another <a href="https://stackoverflow.com/questions/51768180/when-testing-a-method-in-scalatest-is-it-possible-to-mock-stub-an-object-which">stackoverflow question</a>, but I cannot seem to get this to run.</p>
  <p>I get 2 errors:</p>
  <p>There were 2 failures:</p>
  <pre><code>1) Test mechanism 
  java.lang.NullPointerException
  
  and
    2) shouldReturnMocked(com.xyz.abc)
                       org.mockito.exceptions.misusing.MissingMethodInvocationException: 
                       when() requires an argument which has to be 'a method call on a mock'.
                       For example:
                           when(mock.getArticles()).thenReturn(articles);
                       
                       Also, this error might show up because:
                       1. you stub either of: final/private/equals()/hashCode() methods.
                          Those methods *cannot* be stubbed/verified.
                          Mocking methods declared on non-public parent classes is not supported.
                       2. inside when() you don't call method on mock but on some other object.
  </code></pre>
  <p>The stacktrace was omitted but points to:
  <code>given(InetAddress.getLocalHost).willReturn(inetAddress)</code> or
  <code>given(inetAddress.getHostName).willReturn(&quot;mocked&quot;)</code> depending on which is first when switching order.</p>
  <p>BONUS QUESTION:</p>
  <p>I would like to changed the format to be more like what I am familiar with so that the test runs like this:</p>
  <pre><code>import somestuff
  import xxxx.XYZ
  
  @RunWith(classOf[JUnitRunner])
  class ValidationSuite extends FunSuite {
   
    test(&quot;this mock test&quot;) {
        // Setup
        val name = (new XYZ).getAppendedName(&quot;is&quot;)
        assert(name == &quot;is mocked&quot;)
    }
  }
  </code></pre>
  <p>How can I make this aforementioned code work?  I'm also open to use scalatestplus mockito 3.2 solutions, but not open to refactoring my code (which follows the pattern of example above).</p>
  <p>NOTE: I have found away around this example, by doing something like:</p>
  <pre><code>val xyz = new XYZ {
  
      override def getAppendedName(prependage: String): String = {
      s&quot;$prependage mocked host&quot;
    }
  }
  </code></pre>
  <p>HOWEVER, the code in my case is called below <code>getAppendedName</code> where the method calls other class and object methods that subsequently call <code>InetAddress</code>. In these deeper layers, the code should be mocked.</p>