【发布时间】:2019-02-06 23:09:00
【问题描述】:
我正在测试我的 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>
我的道课:
@Component
public class DaoClass {
@Autowired
private MyCustomRestTemplate restTemplate;
public SomeObjectType getAddressFromSomewhere(
String url, String request) {
......
return restTemplate.postForObject(url, request, SomeObjectType.class);
}
}
我已经设置了一个 TestConfiguration 类,以便在测试中使用测试 restTemplate bean:
@Configuration
public class TestConfiguration {
@Bean
public MyCustomRestTemplate restTemplate() {
return new MyCustomRestTemplate();
}
}
这是我模拟 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 )
}
}
*/
}
【问题讨论】:
-
你添加了哪个 bytebuddy 依赖?
-
@tim_yates 我刚刚用依赖更新了帖子
-
Spock 的哪个版本?
-
那个版本的 bytebuddy 太旧了
-
Ahhh Szymon 你在下面介绍过吗????