Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUNIT test - why was the constructor called twice

Tags:

java

junit

As my class name suggests: I wanted to test out, if my class was an instance of Iterator. Therefore I wondered, if it only had to implement the Interface to do so and it seems that was enough.

However, when I ran the following class via JUNIT Test I got the following console output:

Do something!

Do something!

I did something!

It seems the class constructor was called twice! However I have no idea from where the second call comes. I already tested variation of the "if" argument to rule out, instanceof is at fault, like

// if (a instanceof Object) {
// if (a instanceof Iterator) {
// if (2 > 3) {

However it was called in all 3 cases. Thus I assume Unit Test first tries, needs to create an object of the class for itself to execute all the tests, as they are part of the class. Is this correct (?)

import java.util.Iterator;

import org.junit.Test;

public class InstanceOfIteratorTest implements Iterator {

    @Test
    public void test1() {
        InstanceOfIteratorTest a = new InstanceOfIteratorTest();
        if (a instanceof Iterator) {
            System.out.println("I did something!");
        } else {
            System.out.println("I did nothing!");
        }
    }

    public InstanceOfIteratorTest() {
        System.out.println("Do something!");
    }

    @Override
    public boolean hasNext() {
            throw new java.lang.UnsupportedOperationException("blub");
    }

    @Override
    public Integer next() {
            throw new java.lang.UnsupportedOperationException("blub");
    }

    @Override
    public void remove() {
            throw new java.lang.UnsupportedOperationException("blub");
    }
}
like image 450
blauerschluessel Avatar asked Oct 26 '25 03:10

blauerschluessel


2 Answers

test1() is an instance method of class InstanceOfIteratorTest, so yes, JUnit needs to create the instance first and then call the method. That's just how instances work.

like image 57
Nautilus Avatar answered Oct 28 '25 16:10

Nautilus


In Java, it's impossible to call non-static method without create Object and call construstor. And of course Junit must call construstor, so real code:

In Junit:

  ...
  InstanceOfIteratorTest l = new InstanceOfIteratorTest(); // print "Do something!"
  l.test1();

In l.test1:

    InstanceOfIteratorTest a = new InstanceOfIteratorTest(); // print "Do something!"
    if (a instanceof Iterator) {
        System.out.println("I did something!"); // print "I did something!"
    } else {
        System.out.println("I did nothing!");
    }

You can change code to:

@Test
public void test1() {
    if (this instanceof Iterator) {
        System.out.println("I did something!");
    } else {
        System.out.println("I did nothing!");
    }
}

In this case "Do something!" is printed only one time.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!