Outputstream .write() test runs through?
Can someone please explain to me why my test is going through here.
The off is 7, which is the index position at which the bytes should be read from the byte array. My array, however, only contains 5 numbers.
How can the test then be carried out?
public class AusgabeStream extends OutputStream { @Override public void write(int b) throws IOException { } public void write(byte b[], int off, int len) throws IOException { /* Einschränkungen: byteArray nicht null kein negativer off und len */ if (len < 0) { throw new RuntimeException("Länge ist kleiner null"); } if (b == null) { throw new NullPointerException("Array ist null"); }
Now comes the test:
class AusgabeStreamTest { AusgabeStream os = new AusgabeStream(); @Test void ungueltigIndex() { // Fragen warum durchläuft, off geht bei 7 los? try { os.write(new byte[]{101, 90,30,20,44}, 7, 2); // fail("IndexOutofBoundsException erwartet"); } catch (IndexOutOfBoundsException e) { assertEquals("", e.getMessage()); } catch (IOException e) { } }
Why is the test running here?
You overwrite write() in OutputStream. This will call yours and not the ones from OutputStream.
What is the method going on? Do you want them to call something from OutputStream? There is the writing method with the meaning of offset and length?
Then you need super.write(…
super.write(new byte[] {101, 90,30,20,44}, 7, 2);
In your write() method you call super.write(b, off, len ) if you want.