1

我正在尝试 mockStatic DriverManager.class 并获取 mockConnection 但是一个真正的静态 void 方法被称为 getConnection 。

测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class MyClassTest {

  @Before
  public void setUp() throws Exception {
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);

    PowerMockito.mockStatic(DriverManager.class);
    PowerMockito.doReturn(connection).when(DriverManager.class, "getConnection", anyString(), anyString(), anyString());
  }

  @Test
  public void testMain() {
    // arrange
    String[] args = {"name", "password", "database"};
  }
}

Pom.xml

  <properties>
    <powermock.version>1.7.4</powermock.version>
  </properties>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito2</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

尝试执行 PowerMockito.doReturn(connection).when(DriverManager.class, "getConnection", anyString(), anyString(), anyString()) 时遇到的异常:

java.sql.SQLException: No suitable driver found for 
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:810)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:790)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:110)
at vez.MyClassTest.setUp(MyClassTest.java:26)

我做错了什么?如何正确模拟 DriverManager.class?PS。当我查看 org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:110) 我看到它总是调用“Whitebox.invokeMethod(classMock, methodToExpect, parameters);” 因此,我不明白它一般是如何工作的。

4

2 回答 2

2

该类DriverManager将尝试加载"jdbc.drivers"系统属性中引用的驱动程序类作为其初始化的一部分。这发生在DriverManager加载类时,即,甚至在getConnection()调用任何类似的方法之前。所以它不是你得到的例外是因为一个真实getConnection()的被调用。这是因为您没有在类路径中包含一个真正的驱动程序类,从您的pom中可以看出,它DriverManager可以在其初始化期间加载。因此,在您的驱动程序中包含一个驱动程序,pom然后尝试。

请参阅https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.htmlstaticDriverManager 类源代码中的块https://github.com/JetBrains/jdk8u_jdk/ blob/master/src/share/classes/java/sql/DriverManager.java

于 2018-07-13T17:09:18.777 回答
1

当我创建 objectUnderTest 的实例并将其类名放在 @PrepareForTest 注释下时最终起作用的解决方案(忽略输出控制台中的警告)。虽然我不知道为什么它会这样工作:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class, MyClass.class)
public class MyClassTest {

  @Before
  public void setUp() throws Exception {
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);

    PowerMockito.mockStatic(DriverManager.class);
    PowerMockito.when(DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(connection);
  }

  @Test
  public void testMain() {
    // arrange
    String[] args = {"name", "password", "database"};
    MyClass myClass = new MyClass();
    myClass.method();
    ...
  }
于 2018-07-14T18:39:00.977 回答