Hallo ich benutze Eclipse Luna und ich habe versucht, einen Unitest mit Junit zu schreiben. Ich möchte einer Methode einen Wert (null) geben und eine Ausnahme auslösen. Die Methode ist ein Konstruktor und die Klasse hat eine Hauptkomponente. Was mache ich falsch. Normalerweise kann ich rufen eine Methode wie diese Package.class.MethodName (Parameter Parameter)So rufen Sie einen Konstruktor in einem Junit-Test in Java auf?
Prüfklasse:
public class WebUntisProviderTest {
public static URL url = null;
@Test(expected = IllegalArgumentException.class)
public void WebUntisProvider() {
final URL url = null;
WebUntisProvider prov = new WebUntisProvider(url);
assertNotNull("object was null" , prov.WebUntisProvider(url));
fail("url must not be null.");
}
}
Die getestete Klasse:
public class WebUntisProvider {
private URL url;
public static void main(String[] args) throws MalformedURLException {
URL test = new URL("HTTP://google.com");
System.out.println(test.getProtocol());
}
public WebUntisProvider(URL url) {
if (url == null)
throw new IllegalArgumentException("url must not be null.");
if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https"))
throw new IllegalArgumentException("url protocol must be http or https.");
this.url = url;
}
}
Vielen Dank, diese Zeile zu entfernen funktioniert. – Cherub
Das ist der neue Test. @Test (expected = IllegalArgumentException.class, timeout = 10) \t public void WebUntisProvider() { \t \t final URL url = null; \t WebUntisProvider prov = neuer WebUntisProvider (url); \t \t AssertNotNull (Prov); – Cherub