Questions tagged [spring-boot-test]

Spring Boot provides utilities and annotations to help when testing your application.

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.

Utilities: ConfigFileApplicationContextInitializer,EnvironmentTestUtils,OutputCapture and TestRestTemplate

for example

EnvironmentTestUtils.addEnvironment(env, "org=Spring", "name=Boot");

Docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

1399 questions
4
votes
2 answers

Specifying @SpringBootApplication in @WebMvcTest

Using @WebMvcTest will auto-configure all web layer beans by looking for a @SpringBootConfiguration class (such as @SpringBootApplication). If the configuration class is in a different package and can't be found by scanning, can I provide it…
NatFar
  • 2,090
  • 1
  • 12
  • 29
4
votes
1 answer

Envers audit table does not rollback in spring-boot integration tests annotated with @Transactional

I am trying to figure out why @Transactional does not rollback data in envers audit table after each test and how to fix it. How can I make that happen in spring integration tests? I have tried with @DirtiesContext and that makes it work but it's…
4
votes
1 answer

Spring boot test minimal test slice or manual configuration

I have many different SpringBoot tests running. So far the auto configuration slices were really helpful, especially in combination with @MockBean. But in my current test no such slice fits and booting up the complete context using @SpringBootTest…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
4
votes
1 answer

if...else statement issue in mockito test in Spring Boot

how to write a mockito test case for if...else statement which also includes exception test, I am quite confused about this. UserService is an interface UserFactory.java public class UserFactory { @Autowired private List
HungryBird
  • 1,077
  • 1
  • 11
  • 28
4
votes
3 answers

How add setup before @SpringBootTest and only run once?

I have a docker DB setup method, which currently located in @BeforeAll. Currently, Construct as below @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public DockerConstructorTest{ @BeforeAll public…
Neil
  • 2,714
  • 11
  • 29
  • 45
4
votes
2 answers

Spring Boot: How to override default properties in Unit tests

I tried loading a second properties file for my unit tests, that would overwrite some properties. Loading it with @PropertySource on a @Configuration didn't work, loading it with @TestPropertySource didn't work either. Only setting properties…
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49
4
votes
2 answers

SpringBoot 2 + Junit5: null with @Value

I have an application with SpringBoot2 and Junit5, and now I'm trying to make a test. I have a this class called OrderService that looks like this: @Component public class OrderService { @Value("#{'${food.requires.box}'.split(',')}") private…
AleGallagher
  • 1,745
  • 7
  • 30
  • 40
4
votes
3 answers

Spring Boot 2.0 JNDI property values not loading from application-test property file

I am trying to run test cases and jndi properties configured in application-test.properties file @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = { App.class }, webEnvironment =…
Kumar Panchal
  • 186
  • 13
4
votes
3 answers

How to define the spring.config.location on Spring Boot and JUnit tests?

How we can programmatically configure Spring Boot to define new values to the spring.config.name and spring.config.location properties when running JUnit tests? For example, if we would like to define these properties when running the application…
GarouDan
  • 3,743
  • 9
  • 49
  • 75
4
votes
1 answer

Testing using spring boot throws null pointer exception

I am new to spring boot and i am trying to test a very simple class. But when i run the testMe() below I get exception below java.lang.NullPointerException at MyTest.testMe(MyTest.java:25) at…
Neha
  • 745
  • 4
  • 10
  • 18
4
votes
4 answers

Spring Boot: custom properties configuration and tests

I'm using Spring Boot 2.0 with default application.yml properties file. I would like to split it to separate property files because it becomes huge. Also I would like to write tests to check properties correctness: values that will present on…
Bullet-tooth
  • 782
  • 1
  • 10
  • 16
4
votes
1 answer

Undo @Sql after each test

I have some junit tests where I want to pre-populate the DB with some data that actually makes sense for the test: @RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("test") public class MyTest { @Sql("test_insert.sql") @Test public…
Phate
  • 6,066
  • 15
  • 73
  • 138
4
votes
1 answer

SpringBoot2 + Webflux - WebTestClient returns "Content not available yet"

I'm trying to write some tests and I facing the following exception, when I try to post a byte array body: error java.lang.AssertionError: Status expected:<201> but was:<404> > POST /api/foo > WebTestClient-Request-Id: [1] > Content-Length:…
tbo
  • 9,398
  • 8
  • 40
  • 51
4
votes
4 answers

spring boot - application start twice..?

I am new to spring boot and my application has Servlet with eh-cache modules. @EnableCaching @SpringBootApplication @ServletComponentScan public class Application extends SpringBootServletInitializer { private static final Logger log =…
Boss
  • 41
  • 1
  • 1
  • 2
4
votes
3 answers

Avoid proliferation of @MockBeans in Spring Boot @WebMvcTest test

I have a simple controller e.g. @Controller public class FooController { @Autowired private BarService barService; @RequestMapping(value = "/foo", method = RequestMethod.GET) public String displayFoo() { return "foo"; …
user2964500