Java test with expected = exception fails with assertion error
<p>I'm having an issue with a test case.</p>

<p>The method being tested has a try / catch that catches a <code>MalformedURLException</code> but during testing I get a failure due to a Junit AssertionError that expects a MalformedURLException. But I can't find out what it is actually throwing! Here is my code (created as a MWE in eclipse).</p>

<p>My method that I want to test</p>

<pre><code>public void throwMalFormedURLException(){

    String s = new String("www.google.com");

    try{
        url = new URL(s);
    }catch (Exception e){
        e.printStackTrace();
        e.getClass();
        e.getCause();
    }


}
</code></pre>

<p>the test method</p>

<pre><code>@Test (expected = MalformedURLException.class)
public void testThrowMalFormedURLException() {
    MWE testClass = new MWE();
    testClass.throwMalFormedURLException();
    System.out.println("End of test");
}
</code></pre>

<p>This is the output in the console</p>

<blockquote>
  <p>End of test error details are: java.net.MalformedURLException: no protocol: www.google.com<br>
    at java.net.URL.(URL.java:593)<br>
    at java.net.URL.(URL.java:490)<br>
    at java.net.URL.(URL.java:439)<br>
    at MWE.throwMalFormedURLException(MWE.java:12)<br>
    at testMWE.testThrowMalFormedURLException(testMWE.java:12)  </p>
</blockquote>

<p>In the Junit console it says : </p>

<blockquote>
  <p>java.lang.AssertionError: Expected exception: Java.net.MalformedURLException</p>
</blockquote>

<p>But the Junit is reporting failure, even though the console is telling me I've got a MalformedURLException.</p>

<p>What am I doing wrong with this test ?</p>

<p>Thanks for your thoughts.</p>

<p>David </p>