- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 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/
我正在为 Spring Boot 应用程序创建一些单元测试。 在名为 ComicService 的类中,有一个名为 getComicByApi 的方法,我想为该方法创建一个测试,但此方法访问同一类的另
我有一个 Spring 集成测试的问题。 行为: 当我单独运行下面的测试时,它成功了。 但是,当所有测试都运行时,包括以下测试在内的许多测试都出错了。 当我忽略下面的测试并运行所有测试时,一切都成功了
我很难将 spy bean 放入我的 ApplicationContext 中。我有一个名为 utilities 的 bean,其类型为 Utilities: @Component("utilitie
我正在尝试使用 Mockito 的 Spy 部分模拟服务,覆盖一个方法以使其返回一致的数据以进行测试,但说 spy 无缘无故抛出 UnfinishedStubbingException。 这是我的测试
我遇到以下问题。我有一个 Spring 启动测试,我在其中注入(inject)和监视 mongoDbChannel bean 。然后我尝试启动正常的工作流程并验证方法 send在 bean 上调用。
@SpyBean 之间有什么区别?来自 org.springframework.boot.test.mock.mockito.SpyBean和 @Spy来自 org.mockito.Spy ? 使用
编写测试的时候,我们必须保证外部依赖行为一致,也需要模拟一些边界条件,所以我们需要使用 Mock 来模拟对象的行为。SpringBoot 提供了 @MockBean 和 @SpyBean 注
@SpyBean 之间有什么区别?和 @MockBean Mockito 中的注释? 我已经阅读了 JavaDoc,但没有得到区别。如果可能,请举例说明何时使用 MockBean 以及何时使用 Spy
我有一个 Spring Boot 应用程序,最近从 v1.3.3.RELEASE 升级到 v1.4.2.RELEASE。 对于 v1.3.3 中的集成测试,我有一个能够成功监视的 bean。我正在使用
我的应用程序中有 2 个 DataSource。 因此,为了获得所需的JdbcTemplate,我使用了@Qualifier。但是,当我像下面那样做时,测试会运行……但如果在“测试方法”中使用了 Jd
我是一名优秀的程序员,十分优秀!