Спок Тестирование получил NoClassDefFoundError: net / bytebuddy / TypeCache при Mocking RestTemplate - PullRequest
0 голосов
/ 07 февраля 2019

Я тестирую свой класс DAO, который использует пользовательский RestTemplate, который расширяет RestTemplate для выполнения postForObject, но я получаю ошибку ниже, даже после того, как я добавил зависимость byte-buddy в 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>       

Мой класс Dao:

@Component
public class DaoClass {

   @Autowired
   private MyCustomRestTemplate restTemplate;

   public SomeObjectType getAddressFromSomewhere(
       String url, String request) {
     ......
     return restTemplate.postForObject(url, request, SomeObjectType.class);     
 }
}

Я настроил класс TestConfiguration так, чтобы тестовый компонент restTemplate использовался втестирование:

    @Configuration
    public class TestConfiguration {

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

Вот мой код Спока, где я высмеял 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 )
    }
} 
*/
}

1 Ответ

0 голосов
/ 07 февраля 2019

Класс TypeCache<T> был введен в byte-buddy 1.6.0, поэтому вам нужна эта версия как минимум.Spock использует необязательную зависимость byte-buddy, что означает, что версия, указанная вами в pom.xml, имеет приоритет.В зависимости от версии Spock, здесь есть версии с байтовыми контактами, используемые конкретной версией Spock:

  • spock-core: 1.2-groovy-2.4 => byte-buddy: 1.8.21
  • spock-core: 1.1-groovy-2.4 => byte-buddy: 1.6.5

Обновите версию зависимости byte-buddy до одной из следующих, и это должно решить вашу проблему.

...