Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 331 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring-boot default profile for integration tests

#11
You can put your test specific properties into `src/test/resources/config/application.properties`.

The properties defined in this file will override those defined in `src/main/resources/application.properties` during testing.

For more information on why this works have a look at [Spring Boots docs][1].


[1]:

[To see links please register here]

Reply

#12
I've usually done a base class for all integration tests with common code and annotations. Do not forget make it `abstract` in order not to instatiate. E.g:
```
@SpringBootTest
@Transactional
@AutoConfigureMockMvc
@ActiveProfiles("test")
public abstract class AbstractControllerTest {

@Autowired
protected MockMvc mockMvc;

protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
return mockMvc.perform(builder);
}
}

// All annotations are inherited
class AccountControllerTest extends AbstractControllerTest {
....
```
Reply

#13
The best solution I have found is the last suggestion here:

[To see links please register here]

The author also desribes the problem very clearly and discusses the downside of every other approach I can think of.

Create a file `application-default.properties` in your test resources, containing a single line:
```
spring.profiles.active=test
```

This takes advantage of the fact that Spring automatically enables a "default" profile if no other profiles were explicitly set. Now, your `application-test.properties` file will be used by default, for all tests.
Reply

#14
There are two approaches.


## Load from `config/`

*(2022 update, tested against Spring Boot 2.6)*

Along with the approach below, you can also add config to `src/test/resources/config/application.yml`


```none
src/
├── main/
│ ├── java/
│ │ └── ...
│ └── resources/
│ └── application.yml <- default properties, always loaded
└── test/
├── java/
│ └── ...
└── resources/
└── config/
└── application.yml <- test properties, will override the defaults
```

[To see links please register here]


> Spring Boot will automatically find and load `application.properties` and `application.yaml` files from the following locations when your application starts:
>
> 1. From the classpath
> 1. The classpath root
> 2. The classpath `/config` package
> 2. From the current directory
> 1. The current directory
> 2. The `/config` subdirectory in the current directory
> 3. Immediate child directories of the `/config` subdirectory
>
> The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as `PropertySources` to the Spring `Environment`.



### Manual import using `spring.config.import`

*(original answer from 2021, tested against Spring Boot 2.4)*

One solution is to have 3 properties files and to import

* `src/main/resources/application.yml` - contains the application's default props
* `src/test/resources/application.yml` - sets the profile to 'test', and imports properties from 'main'
* `src/test/resources/application-test.yml` - contains test-specific profiles, which will override 'main'


Here is the content of `src/test/resources/application.yml`:

```yml
# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml
```

For example, if `src/main/resources/application.yml` has the content

```yml
ip-address: "10.7.0.1"
username: admin
```

and `src/test/resources/application-test.yml` has

```yml
ip-address: "999.999.999.999"
run-integration-test: true
```

Then (assuming there are no other profiles)...

when running tests,
```
profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true
```

and when running the application normally

```
profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>
```

---

Note: if `src/main/resources/application.yml` contains `spring.profiles.active: "dev"`, then this won't be overwritten by `src/test/resources/application-test.yml`
Reply

#15
If you use maven, you can add this in pom.xml:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>-Dspring.profiles.active=test ${argLine}</argLine>
</configuration>
</plugin>
...

Then, maven should run your integration tests (*IT.java) using this arugument, and also IntelliJ will start with this profile activated - so you can then specify all properties inside

application-test.yml

and you should not need "-default" properties.

**UPDATE**

added `${argLine}` for this to work together with other plugins (e.g. jacoco)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through