0

I want a to create a test for my spring boot application that uses specific JVM arguments, I want the JVM arguments to be used with only this test.

Is this possible ? My goal is to set up a proxy for just one test, so if there is another approach to achieve that, please suggest it.

oussema
  • 322
  • 4
  • 15
  • Did you consider `@BeforeClass` or `@Before`? In those you can set the properties using `System.setProperty(...)`. – Betlista Mar 11 '20 at 21:30
  • Thank you for your suggestion, I used the `System.setProperty(...)` but with a static bloc. – oussema Mar 12 '20 at 12:41

2 Answers2

10

I solved this by adding a static bloc in the class and setting the system properties:

static {
    System.setProperty("http.proxyHost", "myproxyserver.com");
    System.setProperty("http.proxyPort", "80");
    System.setProperty("https.proxyHost", "myproxyserver.com");
    System.setProperty("https.proxyPort", "80");
}
oussema
  • 322
  • 4
  • 15
  • well, I suggested `@Before`/`@BeforeClass` as you should unset it after the test using `@After`/`@AfterClass`... Using static block you set it for all tests I'd say... – Betlista Mar 12 '20 at 12:49
  • yes I tried with the `@Before` annotation and it didn't change anything, also the `@BeforeClass` annotation causes an error (I'm not sure why). Using the static bloc is good solution for me because the test class only has one test, besides, the `@Before` runs before every test so there's no difference. – oussema Mar 12 '20 at 13:24
  • Well probably it didn't work, because the component that needs it (is it HttpClient?) was created (I mean probably context was initialized and the bean that is using the setting) before you were setting it. If you will have new test now, which doesn't want to use the settings it's not possible... – Betlista Mar 12 '20 at 14:08
  • I don't know if there's a solution for that (the settings affecting all the tests) but like I said since I'm using this for a class that has one test in it, it's fine. maybe I should mention it in the answer since the question is about settings for one test, not a test suite. – oussema Mar 12 '20 at 14:25
  • 1
    I tried setting the properties in `@SpringBootTest(properties={...})` which did not work well. The solution above how ever worked just fine good job! – hreinn Aug 05 '20 at 12:51
1

If you're using IntelliJ Idea, you can pass arguments to test from Run/Debug configuration -> VM options. If you're using Maven, you can pass arguments in surefire-plugin configuration:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Xmx1G</argLine>
            </configuration>
        </plugin>

And remember, that Maven options overrides Idea defaults (if set).

Betlista
  • 10,327
  • 13
  • 69
  • 110
stinger
  • 3,790
  • 1
  • 19
  • 30
  • Thank you for the answer, however, I'm looking for a way to have JVM arguments with only one specific test. I think this will add the argument to more than one test. – oussema Mar 11 '20 at 14:25
  • You can specify exact Junit test name in Idea run/debug configuration, see https://www.jetbrains.com/help/idea/creating-and-editing-run-debug-configurations.html – stinger Mar 11 '20 at 14:36
  • What about when the tests are run when the project is being built with maven ? I want this to work not just for local tests with IntelliJ. sorry I forgot to mention this in my last comment. – oussema Mar 11 '20 at 14:43
  • this works for any project based on Maven, independent on whether you have IntelliJ or not – hello_earth Apr 19 '23 at 09:28
  • 1
    @hello_earth please read carefully my answer. It contains information about both Maven and Intellij configuration. – stinger Apr 19 '23 at 10:08