Mocking static methods with Mockito

0 votes

I've written a factory to produce java.sql.Connection objects:

public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory {

    @Override public Connection getConnection() {
        try {
            return DriverManager.getConnection(...);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

I want to validate the parameters passed to DriverManager.getConnection, but I have no idea about how to mock a static method. Is there a good way to mock/verify this specific use-case?

Dec 12, 2018 in Java by Sushmita
• 6,910 points
6,977 views

1 answer to this question.

0 votes
@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class Mocker {

    @Test
    public void testName() throws Exception {

        //given
        PowerMockito.mockStatic(DriverManager.class);
        BDDMockito.given(DriverManager.getConnection(...)).willReturn(...);

        //when
        sut.execute();

        //then
        PowerMockito.verifyStatic();
        DriverManager.getConnection(...);

    }
answered Dec 12, 2018 by Daisy
• 8,120 points

Related Questions In Java

+1 vote
10 answers

How to make mock to void methods with mockito

You can use partial mocking to serve ...READ MORE

answered Dec 7, 2018 in Java by nitesh
34,285 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
15,556 views
0 votes
1 answer

Static methods can't be abstract in Java

Well, Java doesn't allow this because of ...READ MORE

answered Oct 12, 2018 in Java by v.liyyah
• 1,300 points
861 views
0 votes
2 answers

When to use static methods

Java main() method is always static, so that compiler ...READ MORE

answered Dec 29, 2020 in Java by Reshma
755 views
0 votes
1 answer

How can I run test methods in specific order in JUnit4?

@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class SampleTest { ...READ MORE

answered Jan 7, 2019 in Java by Daisy
• 8,120 points
1,067 views
0 votes
1 answer
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,495 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,952 views
0 votes
1 answer

Can Static methods be inherited in java?

A subclass inherits all of the public ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,120 points
1,653 views
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,120 points
2,075 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP