java - 如何在通过构造函数接收参数的类中使用@SpyBean?

标签 java spring junit spring-test

我正在为 Spring Boot 应用程序创建一些单元测试。

在名为 ComicService 的类中,有一个名为 getComicByApi 的方法,我想为该方法创建一个测试,但此方法访问同一类的另一个方法,称为getHash

我需要配置 getHash 的行为,因此我在创建 ComicService 对象时使用了 @SpyBean 注释。

问题是,运行测试时,在我使用 Mockito.when().thenReturn() 配置 getHash 行为的部分出现错误。

我发现该错误与我使用 @BeforeEach public void setUp() 使用 @SpyBean 传递其构造函数参数来实例化带注释的类有关,但我仍然不知道怎么解决。

有人知道如何解决这个问题吗?

漫画服务

@Service
public class ComicService {
    
    private String publicKey;
    
    private String privateKey;
    
    private MarvelClient marvelClient;
    
    public ComicService(@Value("${marvel.public_key}")String publicKey, 
            @Value("${marvel.private_key}") String privateKey, MarvelClient marvelClient) {
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.marvelClient = marvelClient;
    }
    
    public MarvelAPIModelDTO getComicByApi(Integer idComicMarvel) {
        String timeStamp = String.valueOf((int)(System.currentTimeMillis() / 1000));
        String hash = getHash(timeStamp);
        
        MarvelAPIModelDTO comic = marvelClient.getComic(idComicMarvel, timeStamp, timeStamp, hash);
        
        return comic;
    }
    
    public String getHash(String timeStemp) {
        String value = timeStemp+privateKey+publicKey;          
        
        MessageDigest md;
        
        try {
            md = MessageDigest.getInstance("MD5");
        } catch(NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        
        BigInteger hash = new BigInteger(1, md.digest(value.getBytes()));
    
        return hash.toString(16);
    }

}

ComicService测试

@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
public class ComicServiceTest {
    
    @SpyBean
    ComicService comicService;
    
    @MockBean
    MarvelClient marvelClient;
    
    @BeforeEach
    public void setUp() {
        this.comicService = new ComicService("ae78641e8976ffdf3fd4b71254a3b9bf", "eb9fd0d8r8745cd0d554fb2c0e7896dab3bb745", marvelClient);      
    }

    @Test
    public void getComicByApiTest() {
        // Scenario
        MarvelAPIModelDTO foundMarvelAPIModelDTO = createMarvelAPIModelDTO();
        
       //It's giving an error on this line 
        Mockito.when(comicService.getHash(Mockito.anyString())).thenReturn("c6fc42667498ea8081a22f4570b42d03"); 

        Mockito.when(marvelClient.getComic(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(foundMarvelAPIModelDTO);
        
        // Execution
        MarvelAPIModelDTO marvelAPIModelDTO = comicService.getComicByApi(1);
        
        // Verification
        Assertions.assertThat(marvelAPIModelDTO.getData().getResults().get(0).getId()).isEqualTo(1);        
    }

}

错误

at com.gustavo.comicreviewapi.services.ComicServiceTest.getComicByApiTest(ComicServiceTest.java:58)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(any());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

最佳答案

看起来@SpyBean无法找到您的服务类的实例。快速替代方法是从 ComicService ComicService; 中删除 @SpyBean 并在 @BeforeEach 中执行以下操作:

@BeforeEach
    public void setUp() {
        this.comicService = Mockito.spy(new ComicService("ae78641e8976ffdf3fd4b71254a3b9bf", "eb9fd0d8r8745cd0d554fb2c0e7896dab3bb745", marvelClient));

    }

在这里,您正在创建 spy ,然后在测试类中使用它。

关于java - 如何在通过构造函数接收参数的类中使用@SpyBean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72828384/

相关文章:

java - Java MAVEN 示例中的 XpathException 表达式使用未绑定(bind)的命名空间前缀

java - 更改EmptyBorder空间颜色

unit-testing - 如果我@Ignore JUnit4 中的测试类,@BeforeClass 是否仍然运行?

java - 数组列表不会使用我创建的方法

java - Android命名一个P2P设备

java - user-by-username-query 需要超过 1 个参数

java - 如何使用 spring RestTemplate 生成带有二进制数据的curl 请求?

java - 使用 Spring Integration 时抛出不同的 JSchException

android - 使用 robolectric 测试图像处理例程

android - 新 gradle 和 android sdk 版本的 Roboelectric 问题