Reputation: 127
I have the following snippet where I have an abstract class with a parameterized constructor.
public abstract class Cat {
protected final Connection con;
protected Cat(Connection con) throws SQLException {
if (con == null || con.isClosed()) {
throw new SQLException("failed);
}
this.con = con;
}
}
Is it possible to test the behaviour of the constructor using Java mockito? For example, can I test that constructor fails if a closed connection is supplied?
Upvotes: 0
Views: 179
Reputation: 858
I think it's enough if you test the constructor of your derived class which extends this abstract class. As you can't create instance of abstract class it doesn't make sense to test it alone. Even private methods are not tested alone. If you try to create anonymous object for this class to test that also will be considered as derived class
So test the derived class constructor which obviously will be calling super constructor so your abstract class constructor code will be already covered.
Upvotes: 0
Reputation: 2017
You can instantiate your abstract class like this and pass a Mockito Mock for the connection as parameter.
@Mock
Connection conn;
// in your test method
new Cat(conn){};
Upvotes: 1