Spock测试在模拟RestTemplate时得到了NoClassDefFoundError:net/bytebuddy/TypeCache [英] Spock Testing got NoClassDefFoundError: net/bytebuddy/TypeCache when Mocking RestTemplate

查看:162
本文介绍了Spock测试在模拟RestTemplate时得到了NoClassDefFoundError:net/bytebuddy/TypeCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的DAO类,该类使用自定义的RestTemplate扩展了RestTemplate来执行postForObject,但是即使将字节伙伴的依赖项添加到pom.xml中,也遇到了以下错误.调用Mock()时似乎发生此错误.有人可以让我知道我做错了什么吗?

I am testing my DAO class which uses a custom RestTemplate that extends RestTemplate to do postForObject, but I am getting the below error even after I added byte-buddy dependency to pom.xml. This error seems happening on the call to Mock(). Can someone please let me know what I did wrong?

 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:

I have set up a TestConfiguration class so that the test restTemplate bean will be used in testing:

    @Configuration
    public class TestConfiguration {

        @Bean
        public MyCustomRestTemplate restTemplate() {
            return new MyCustomRestTemplate();
    }
}

这是我的Spock代码,其中我模拟了restTemplate postForObject:

Here is my Spock code where I mocked restTemplate postForObject:

@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 )
    }
} 
*/
}

推荐答案

TypeCache< T> 类是在字节伙伴1.6.0中引入的,因此您至少需要此版本.Spock使用可选的字节伙伴关系,这意味着您在pom.xml中指定的版本具有优先权.根据Spock版本的不同,以下是特定Spock版本使用的字节伙伴版本:

The TypeCache<T> class was introduced in byte-buddy 1.6.0, so you need this version at the minimum. Spock uses optional byte-buddy dependency, which means that the version you specified in your pom.xml takes precedence. Depending on the Spock version, here are byte-buddy versions used by a specific Spock version:

  • spock-core:1.2-groovy-2.4 =>字节好友:1.8.21
  • spock-core:1.1-groovy-2.4 =>字节伙伴:1.6.5

将字节伙伴的依赖关系版本更新为以下版本之一,它应该可以解决您的问题.

Update byte-buddy dependency version to one of the following and it should fix your problem.

这篇关于Spock测试在模拟RestTemplate时得到了NoClassDefFoundError:net/bytebuddy/TypeCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆