Mockito文档在枚举用例上没有明确说明:
Powermock 支持:
build.gradle ( **也应该与Maven一起使用)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
testImplementation 'org.mockito:mockito-inline:4.0.0'
testImplementation 'org.mockito:mockito-junit-jupiter:4.1.0'
至于可用的实验功能(Powermock + JUnit 5),可以尝试一下:
implementation group: 'org.powermock', name: 'powermock-module-junit5', version: '1.6.4'
testImplementation 'org.powermock:powermock-mockito-release-full:1.6.4'
枚举:
public enum ConnectionFactory {
INSTANCE;
public Connection get() throws SQLException {
//
// Load url from configuration file ('application.yml')
//
final var connection = DriverManager.getConnection(url);
connection.setAutoCommit(false);
return connection;
}
}
可以使用这些方法:
class DatabaseUnitTests {
private final ConnectionFactory connectionFactory = Mockito.spy(ConnectionFactory.INSTANCE);
//Or
@InjectMocks
private final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class);
@Test
void check_is_database_connection_is_OK(){
Mockito.doReturn(mockedConnection()).when(connectionFactory).get();
// Do something
}