Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring initializes differently when run from intellij

My desired workflow within intellij is to right click a junit @Test annotated method, and choose "run method()".

This is not working right now due to this error: Could not resolve placeholder 'spring.datasource.url' in string value "${spring.datasource.url}"

This is happening bc that value, spring.datasource.url is not defined in my application.properties file, but rather in separate profiles.

My integration test looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Api.class)
@IntegrationTest
@WebAppConfiguration
@ActiveProfiles(profiles = "h2")
public class ServiceIntegrationTest {

The application-h2.properties has spring.datasource.url defined.

When I run mvn test from the command line, I do not get the error above.

QUESTIONS:

  • Why are my tests running correctly under maven and not intellij?
  • Is intellij overriding the spring.profiles.active somehow?
  • Is it possible to override active profiles that are set in an @ActiveProfiles annotation?

Note: these are integration tests, but I'm getting the same behavior when running unit tests from intellij.


Edit:

C:\intellij-terminal>mvn -version
Apache Maven 3.3.1 (cab6659f9874fa96462afef40fcf6bc033d58c1c; 2015-03-13T15:10:27-05:00)
Maven home: C:\bin\apache-maven-3.3.1\bin\..
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: C:\bin\java\jdk1.8.0_60\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

Edit: Also tried adding spring.profiles.active=h2 to intellij test run configuration.


Edit: I noticed intellij starts my tests with this command:

C:\bin\java\jdk1.8.0_60\bin\java  
  -Dmaven.multiModuleProjectDirectory=C:\dev 
  -Dmaven.home=C:\bin\apache-maven-3.3.1 
  -Dclassworlds.conf=C:\bin\apache-maven-3.3.1\bin\m2.conf 
  -Dfile.encoding=windows-1252 
  -classpath 
     C:\bin\apache-maven-3.3.1\boot\plexus-classworlds-2.5.2.jar 
     org.codehaus.classworlds.Launcher 
  -Didea.version=14.1.5 
  org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run

Questions:

  • Why is intellij trying to run my app?
  • Is there a way to tell intellij to use maven commands pointed at a single test?
like image 990
Eric Francis Avatar asked Feb 18 '26 20:02

Eric Francis


2 Answers

I deleted .idea/ and my project's iml file and reimported. That fixed the issue.

like image 188
Eric Francis Avatar answered Feb 20 '26 08:02

Eric Francis


I am glad that deleting the .iml file did the trick.

Another possibility... mvn package -U followed by File-> Invalidate Caches and Restart.

like image 41
rajah9 Avatar answered Feb 20 '26 10:02

rajah9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!