11

I have a small project that does not contain a class to run a Spring Boot Application. In that class I only have some configuration and some repositories. I would like to test those repositories inside the small project.

For that, I have the following:

@SpringBootTest
@DataJpaTest
public class TaskRepositoryTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private TaskRepository taskRepository;

    @Test
    public void test() {
        taskRepository.save(new Task("open"));
    }
}

But I get the following error

Caused by: java.lang.NoSuchMethodError: org.springframework.boot.jdbc.DataSourceBuilder.findType(Ljava/lang/ClassLoader;)Ljava/lang/Class;

Any idea what I have to do?

1
  • Have you tired without the @DataJpaTest annotation?
    – radekEm
    Commented Jan 3, 2019 at 10:09

1 Answer 1

9

This works for me with Spring Boot 2.0.3, H2 and latest RELEASE testng:

@EnableAutoConfiguration
@ContextConfiguration(classes = TaskRepository.class)
@DataJpaTest
public class TaskTest extends AbstractTestNGSpringContextTests {

   @Autowired
   private TaskRepository taskRepository;

   @Test
   public void test() {
      taskRepository.save(new Task("open"));
   }

}

LE:

In the previous version of the answer I've used @SpringBootTest @DataJpaTest but that seems to be the wrong way to do it. The following message would appear:

[main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [one.adf.springwithoutapp.TaskTest], using SpringBootContextLoader

Using @ContextConfiguration with @DataJpaTest removes that warning and IntelliJ doesn't mark the mocked taskRepository as Could not autowire. No beans of 'TaskRepository' type found.

1
  • I tried this but I still have same exception: java.lang.IllegalArgumentException: Given type must be an interface! if I start test from class itself, or this exception if I start from Maven->Install in IntelliJ : Caused by: java.lang.ClassNotFoundException: ch.qos.logback.classic.turbo.TurboFilter stackoverflow.com/questions/62313098/… Commented Jun 11, 2020 at 9:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.