2 回答
TA贡献2051条经验 获得超10个赞
当我创建objectUnderTest的实例并将其类名称放在@PrepareForTest批注下时,该解决方案最终会起作用(忽略out控制台中的警告)。尽管我不知道为什么会这样工作:
@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();
...
}
添加回答
举报