gpt4 book ai didi

java - 如何使用 Java JUnit 和 Mockito 验证传递给静态函数的参数 (Spring Boot)

转载 作者:行者123 更新时间:2023-11-29 04:20:52 29 4
gpt4 key购买 nike

我对所有提到的技术都不熟悉,所以这可能是一个愚蠢的问题。

我们有一个 Spring Boot 应用程序,需要通过 JDBC 写入 PostgreSQL 数据库。因此,我们需要静态 DriverManager.getConnection() 方法来打开连接。

现在在我的单元测试中我不想直接调用此类。相反,我想检查是否使用正确的字符串调用 DriverManager.getConnection() ,因为这是我预期的可观察的外部行为。

我使用 newConnection(ConnectionType.POSTGRESQL) 方法将此行为封装到 ConnectionFactory 中,因为我们在此应用程序中使用了多个数据库。

现在我找不到一种方法来通过 Mockito 验证是否使用正确的 String 调用此外部依赖项,就像使用实例一样:

DriverManager dm = mock(DriverManager);

connectionFactory.newConnection(ConnectionType.POSTGRESQL);

verify(dm).getConnection("theConnectionStringToBeExpected");

那么如何使用静态依赖来做到这一点?

我尝试了Captor方式,但这似乎只适用于直接使用,如

mockStatic(DriverManager.class);

final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);

// What I have to do to verify
DriverManager.getConnection("theActualConnectionString");

// What I would like to do to verify
// connectionFactory.newConnection(ConnectionType.POSTGRESQL);

verifyStatic();
StaticService.getConnection(captor.capture());

assertEquals("theExpectedConnectionString", captor.getValue());

编辑:

这是我目前用于另一台服务器的令人讨厌的小解决方法...

public void driverManagerIsCorrectlyCalledForAds() throws Exception {
mockStatic(DriverManager.class);

doNothing().when(databaseDriverLoader).load();

final Connection expectedConnection = mock(Connection.class);
when(DriverManager.getConnection("jdbc:extendedsystems:advantage://server:1337/database_name;user=user;password=password;chartype=ansi"))
.thenReturn(expectedConnection);

Connection actualConnection = connectionFactory.newConnection(ConnectionType.ADS);

assertEquals(expectedConnection, actualConnection);
}

编辑2:

测试类:

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.*;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ConnectionFactory.class, DriverManager.class, DatabaseDriverInformation.class})
public class ConnectionFactoryTest {

@InjectMocks
ConnectionFactory connectionFactory;

@Mock
DatabaseDriverInformation databaseDriverInformation;

@Mock
DatabaseProperties databaseProperties;

@Mock
DatabaseProperties.Pg pg;

@Mock
DatabaseDriverLoader databaseDriverLoader;

@Before
public void setUp() {
doReturn(pg).when(databaseProperties).getPg();
doReturn("server").when(ads).getServer();
doReturn(1338).when(ads).getPort();
doReturn("database_name").when(ads).getDatabasename();
doReturn("user").when(ads).getUser();
doReturn("password").when(ads).getPassword();
}

@Test
public void driverManagerIsCorrectlyCalledForPg() throws Exception {
mockStatic(DriverManager.class);
doNothing().when(databaseDriverLoader).load();

Connection expectedConnection = mock(Connection.class);
when(DriverManager.getConnection("jdbc:postgresql://server:1338/database_name;user=user;password=password"))
.thenReturn(expectedConnection);

Connection actualConnection = connectionFactory.newConnection(ConnectionType.POSTGRESQL);

assertEquals(expectedConnection, actualConnection);
}
}

被测类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;

@Service()
public class ConnectionFactory {

@Autowired
private DatabaseDriverLoader databaseDriverLoader;

@Autowired
DatabaseProperties databaseProperties;


public Connection newConnection(ConnectionType connectionType) {
databaseDriverLoader.load();
final String connectionString = connectionStringFor(connectionType);
try {
return DriverManager.getConnection(connectionString);
} catch (SQLException sqlException) {
throw new RuntimeException("Couldn't connect to Server");
}

}

private String connectionStringFor(ConnectionType connectionType) {
switch (connectionType) {
case ADS:
return this.adsConnectionString();
case POSTGRESQL:
return this.pgConnectionString();

default:
throw new RuntimeException("Invalid connection Type requested!");
}
}


private String adsConnectionString() {
return new StringBuilder()
.append("jdbc:extendedsystems:advantage://")
.append(databaseProperties.getAds().getServer())
.append(":")
.append(databaseProperties.getAds().getPort())
.append("/")
.append(databaseProperties.getAds().getDatabasename())
.append(";user=")
.append(databaseProperties.getAds().getUser())
.append(";password=")
.append(databaseProperties.getAds().getPassword())
.append(";chartype=ansi")
.toString();
}

private String pgConnectionString() {
return new StringBuilder()
.append("jdbc:postgresql://")
.append(databaseProperties.getPg().getServer())
.append(":")
.append(databaseProperties.getPg().getPort())
.append("/")
.append(databaseProperties.getPg().getDatabasename())
.append("?user=")
.append(databaseProperties.getPg().getUser())
.append("&password=")
.append(databaseProperties.getPg().getPassword())
.toString();
}
}

我删除了包名称、一些特定的导入和一些有效的不必要的测试。

最佳答案

经过一番搜索,我发现了这个:How to verify static void method has been called with power mockito

本质上:

第一:

mockStatic(ClassWithStaticFunctionToVerify.class)

第二个:

Execute the Code that will call the function you want to verify later

第三个:

verifyStatic();
ClassWithStaticFunctionToVerify.functionYouWantToVerify("ParameterValueYouExpect");

在它的帮助下,我得到了以下效果很好的解决方案:

@Test
public void driverManagerIsCorrectlyCalledForPg() throws Exception {
// Arrange
mockStatic(DatabaseProperties.Pg.class);
doReturn(pg).when(databaseProperties).getPg();
doReturn("server").when(pg).getServer();
doReturn(1338).when(pg).getPort();
doReturn("database_name").when(pg).getDatabasename();
doReturn("user").when(pg).getUser();
doReturn("password").when(pg).getPassword();

mockStatic(DriverManager.class);
doNothing().when(databaseDriverLoader).loadAdsDriverClass();
doNothing().when(databaseDriverLoader).loadPgDriverClass();

when(DriverManager.getConnection(anyString())).thenReturn(expectedConnection);

// Act
connectionFactory.newConnection(ConnectionType.POSTGRESQL);

// Assert
verifyStatic();
DriverManager.getConnection("jdbc:postgresql://server:1338/database_name?user=user&password=password");
}

关于java - 如何使用 Java JUnit 和 Mockito 验证传递给静态函数的参数 (Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49231781/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com