All Questions
Tagged with spring-boot junit
2,368
questions
272
votes
4
answers
166k
views
Difference between @Mock, @MockBean and Mockito.mock()
When creating tests and mocking dependencies, what is the difference between these three approaches?
@MockBean:
@MockBean
MyService myservice;
@Mock:
@Mock
MyService myservice;
Mockito.mock()
...
238
votes
16
answers
294k
views
Unable to find a @SpringBootConfiguration when doing a JpaTest
I'm trying to run a simple Junit test to see if my CrudRepositories are indeed working.
The error I keep getting is:
Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration ...
105
votes
13
answers
185k
views
How to test main class of Spring-boot application
I have a spring-boot application where my @SpringBootApplication starter class looks like a standard one. So I created many tests for all my functionalities and send the summary to sonarqube to see my ...
92
votes
14
answers
196k
views
How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?
I tried:
@RunWith(SpringJUnit4ClassRunner.class)
@EnableAutoConfiguration(exclude=CrshAutoConfiguration.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public ...
89
votes
3
answers
76k
views
SpringRunner vs SpringBootTest
In unit test, what are the differences between @Runwith(SpringRunner.class) & @SpringBootTest?
Can you explain to me the use cases of each one?
87
votes
4
answers
105k
views
reason: no instance(s) of type variable(s) T exist so that void conforms to using mockito
I want to throw an Exception when running a void method
when(booking.validate(any())).thenThrow(BookingException.builder().build());
but I have a compilation error:
Required type: T
Provided: void
...
83
votes
5
answers
135k
views
How to write a unit test for a Spring Boot Controller endpoint
I have a sample Spring Boot app with the following
Boot main class
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(...
74
votes
5
answers
61k
views
What is the proper annotation since @SpringApplicationConfiguration, @WebIntegration, is deprecated in Spring Boot Framework?
What is the proper annotation since @SpringApplicationConfiguration and @WebIntegration are deprecated as of Spring Boot Framework 1.4? I'm trying to play around with unit testing.
73
votes
13
answers
151k
views
Spring Boot properties in 'application.yml' not loading from JUnit Test
What am I doing wrong? I'm using this little standalone App which runs and finds my src/main/resources/config/application.yml. The same configuration doesn't work from JUnit, see below:
@...
70
votes
1
answer
109k
views
How to test a component / bean in Spring Boot
To test a component/bean in a Spring Boot application, the testing part of the Spring Boot documentation provides much information and multiple ways :
@Test, @SpringBootTest, @WebMvcTest, @DataJpaTest ...
67
votes
21
answers
227k
views
How do I mock a REST template exchange?
I have a service in which I need to ask an outside server via rest for some information:
public class SomeService {
public List<ObjectA> getListofObjectsA() {
List<ObjectA> ...
63
votes
7
answers
155k
views
Spring boot autowiring an interface with multiple implementations
In normal Spring, when we want to autowire an interface, we define it's implementation in Spring context file.
What about Spring boot?
how can we achieve this?
currently we only autowire classes ...
58
votes
5
answers
134k
views
@Value "Could not resolve placeholder" in Spring Boot Test
I want to take a Junit test for Spring-boot as below:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationTest.class})
public class TestOnSpring {
@Value("${app....
49
votes
7
answers
82k
views
How to test Spring @Scheduled
How do I test @Scheduled job tasks in my spring-boot application?
package com.myco.tasks;
public class MyTask {
@Scheduled(fixedRate=1000)
public void work() {
// task execution ...
46
votes
6
answers
40k
views
How to properly convert domain entities to DTOs while considering scalability & testability
I have read several articles and Stackoverflow posts for converting domain objects to DTOs and tried them out in my code. When it comes to testing and scalability I am always facing some issues. I ...