Questions tagged [spring-test]

`spring-test` is the testing module of the Spring Framework, providing support for unit and integration testing with JUnit and TestNG, including various mocks for unit testing and the Spring TestContext Framework & the Spring MVC Test Framework for integration testing Spring-based applications.

spring-test is the testing module of the core Spring Framework, providing support for and and based applications with and , including mocks for things like the and APIs, the Spring TestContext Framework, and the Spring MVC Test Framework.

Online Resources:

Related Tags:

1677 questions
20
votes
2 answers

What is difference between @SpyBean and @MockBean in Mockito?

What is the difference between the @SpyBean and @MockBean annotations in Mockito? I have already gone through the JavaDoc but didn't get the difference. If possible please give an example when to use MockBean and when SpyBean.
Vikrant Chaudhary
  • 273
  • 1
  • 2
  • 11
20
votes
1 answer

@DataJpaTest needing a class outside the test

In a SpringBoot application, I would like to do some test about the repository layer. @RunWith(SpringRunner.class) @DataJpaTest public class VisitRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired …
robert trudel
  • 3,798
  • 12
  • 51
  • 90
20
votes
4 answers

Spring Test returning 401 for unsecured URLs

I am using Spring for MVC tests Here is my test class @RunWith(SpringRunner.class) @WebMvcTest public class ITIndexController { @Autowired WebApplicationContext context; MockMvc mockMvc; @MockBean …
Marco Prado
  • 928
  • 1
  • 7
  • 21
20
votes
7 answers

how to mock spring amqp/rabbit in spring boot test

How to mock spring rabbitmq/amqp so it will not fail during a Spring Boot Test while trying to auto create exchanges/queues? Given I have a simple RabbitListener that will cause the queue and exchange to be auto created like…
domi
  • 1,957
  • 1
  • 20
  • 41
19
votes
2 answers

@Import vs @ContextConfiguration in Spring

Is there any difference in usage of the annotations? Both the annotations allow to use multiple @Configuration classes to create an ApplicationContext. From their docs @ContextConfiguration seems to be more suitable for test configuration and comes…
Dmitry Senkovich
  • 4,543
  • 5
  • 27
  • 58
19
votes
4 answers

How to disable Eureka and Spring Cloud Config in a WebMvcTest?

I play with a simple Spring Boot application which registers itself in Eureka and uses spring cloud configuration to read some properties. Here it goes: @SpringBootApplication @EnableEurekaClient public class Application { public static void…
19
votes
1 answer

spring-boot testing - Could multiple test share a single context?

I created multiple spring-boot testing class, (with spring-boot 1.4.0). FirstActionTest.java: @RunWith(SpringRunner.class) @WebMvcTest(FirstAction.class) @TestPropertySource("classpath:test-application.properties") public class FirstActionTest { …
user218867
  • 16,252
  • 12
  • 112
  • 156
19
votes
3 answers

Is there a way to use AssertJ assertions with Spring MVC Test?

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC…
ashishjmeshram
  • 11,077
  • 50
  • 148
  • 224
19
votes
4 answers

How to flush data into db inside active spring transaction?

I want to test hibernate session's save() method using spring testing framework. @Test method is : @Test @Transactional public void testSave() { User expected = createUser(); getGenericDao().currentSession().save(expected); User actual =…
18
votes
2 answers

Configuring base package for component scan in Spring boot test

When I launch my test with the following annotations: package com.hello.package.p1; @RunWith(SpringRunner.class) @DataMongoTest @SpringBootTest public class ClassATest { @Autowired Service1 serivce1; //fqn = com.hello.package.p1.Service1…
ElArbi
  • 937
  • 3
  • 11
  • 21
18
votes
6 answers

How to disable Spring autowiring in unit tests for @Configuration/@Bean usage

I want configure a component test using spring-test configuration inner class (@Configuration). Tested components has some services which I'd like to mock for the test. These services are classes (no interface used) and have spring annotations…
vuk
  • 351
  • 1
  • 3
  • 9
18
votes
4 answers

Spring Testing: How to enable auto-scan of beans

For example, now in each test class I have to do @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader=AnnotationConfigContextLoader.class) I want to get rid of @ContextConfiguration(loader=AnnotationConfigContextLoader.class) and…
user3594176
  • 183
  • 1
  • 2
  • 6
17
votes
3 answers

Eclipse Photon does not resolve imports in test sources

I have moved to Eclipse Photon with an existing workspace. I have some Maven projects in this workspace. All projects did not have any errors in Eclipse Oxygen. After opening my workspace in Eclipse Photon all test-classes which import…
LuCio
  • 4,285
  • 1
  • 15
  • 29
17
votes
3 answers

Error:java: java.lang.ExceptionInInitializerError IntelliJ

Heyo! I've ran into some troubles when attempting to boot up Intellij (ultimate) with JDK 10.0.1 and "spring-boot-starter-test". If I run the main method, regardless of its content and regardless of if I have any active tests I always receive…
Kavzor
  • 175
  • 1
  • 1
  • 9
17
votes
7 answers

Maven: NoClassDefFoundError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner

I have a classpath issue. Background: Building and running a Spring WebApp. Originally it was one big project including dao/service/controller/webapp. I have just broken my project into a maven module project essentially splitting the webapp from…
Jake
  • 3,348
  • 4
  • 29
  • 63