jlp*_*jlp 1 groovy spring spock
我正在测试我的 DAO 类,它使用扩展 RestTemplate 的自定义 RestTemplate 来执行 postForObject,但即使在向 pom.xml 添加字节伙伴依赖项后,我仍然收到以下错误。这个错误似乎发生在对 Mock() 的调用中。有人可以让我知道我做错了什么吗?
NoClassDefFoundError: net/bytebuddy/TypeCache
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.3.16</version>
<scope>test</scope> <!--also tried giving "runtime" here -->
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的道课:
@Component
public class DaoClass {
@Autowired
private MyCustomRestTemplate restTemplate;
public SomeObjectType getAddressFromSomewhere(
String url, String request) {
......
return restTemplate.postForObject(url, request, SomeObjectType.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经设置了一个 TestConfiguration 类,以便测试 restTemplate bean 将用于测试:
@Configuration
public class TestConfiguration {
@Bean
public MyCustomRestTemplate restTemplate() {
return new MyCustomRestTemplate();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我嘲笑 restTemplate postForObject 的 Spock 代码:
@ContextConfiguration(classes = [TestConfiguration.class])
@Import([DaoClass.class])
public class TestDao extends Specification {
@Autowired
private DaoClass dao;
//got the same error regardless of using @SpringBean or @TestConfiguration
@SpringBean
MyCustomRestTemplate restTemplate = Mock() //***** Error occurred here
def "Test Success Senario"() {
def obj = .... // get object
given: "rest template"
1 * restTemplate.postForObject(_, _, _) >> obj
when: "we call Dao"
def actualResponse = dao.getAddressFromSomewhere(_);
then: "we get response"
actualResponse == obj
}
// got the same error regardless of using @SpringBean or @TestConfiguration
/*
@TestConfiguration
static class MockConfig {
def detachedMockFactory = new DetachedMockFactory()
@Bean
MyCustomRestTemplate restTemplate() {
return detachedMockFactory.Mock(MyCustomRestTemplate )
}
}
*/
}
Run Code Online (Sandbox Code Playgroud)
这个TypeCache<T>
类是在 byte-buddy 1.6.0 中引入的,所以你至少需要这个版本。Spock 使用可选的字节伙伴依赖项,这意味着您在 pom.xml 中指定的版本优先。根据 Spock 版本,以下是特定 Spock 版本使用的字节伙伴版本:
将 byte-buddy 依赖版本更新为以下之一,它应该可以解决您的问题。
归档时间: |
|
查看次数: |
542 次 |
最近记录: |