Having trouble mocking System.getenv calls in junit

PCR :

I'm trying to write a unit test with junit and mockito (very new to this) for a Spring Boot application. Basically in my code, I have specified an environment variable for a specific URL in a manifest.yml file (for deployment) that I can access through String URL = System.getenv("VARIABLE") in my code. I'm having a lot of trouble with my unit testing however, since the URL variable is obviously undefined. I tried the solution here, but realized this is only for mocking an environment variable if you're calling it from the actual test itself, not if you're relying on the environment variable being accessible from the code.

Is there any way to set it up so that when the test is run, I can set environment variables that can be accessed within the code?

StvnBrkdll :

You can use PowerMockito to mock static methods. This code demonstrates mocking the System class and stubbing getenv()

@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class})
public class Xxx {

    @Test
    public void testThis() throws Exception {
        System.setProperty("test-prop", "test-value");
        PowerMockito.mockStatic(System.class);

        PowerMockito.when(System.getenv(Mockito.eq("name"))).thenReturn("bart");
        // you will need to do this (thenCallRealMethod()) for all the other methods
        PowerMockito.when(System.getProperty(Mockito.any())).thenCallRealMethod();

        Assert.assertEquals("bart", System.getenv("name"));

        String value = System.getProperty("test-prop");

        Assert.assertEquals("test-value", System.getProperty("test-prop"));
    }
}

I believe this illustrates what you are trying to accomplish. There may be a more elegant way to do this using PowerMockito.spy(), I just can't remember it.

You will need to do thenCallRealMethod() for all the other methods in System.class that get called directly or indirectly by your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PowerMockito+Junit - Mocking System.getenv

Mocking System.getenv other than with powermock

Jest having trouble mocking specific function

Having trouble understanding ajax calls

Having trouble creating a wrapper function for api calls

Having trouble with chaining method calls in Java

Having trouble with implementing a system that detects if an input is a integer or not

Having trouble turning mbpfan into a system service

Having trouble creating an App Script email system

Pygame : Having trouble w/ multiple event.get() calls

Mocking os.GetEnv("ENV_VAR")

Having Trouble building a distributed calculator system with C sharp

Having trouble creating a Like system for comments in a Laravel Project

Having trouble with a Javascript adding system (simple but I'm slow)

Mocking Reflection based calls

Mocking Method Calls In Python

Mocking ReentrantReadWriteLock in JUnit with mockito

Junit Mockito not mocking as expected

LoadingCache mocking for JUnit testing

Junit Mocking Private Method

Mocking objects for JUnit test

Error in junit while mocking

Junit ignore method mocking

Testing if a java program calls System.exist - without exiting Junit

Having trouble getting calls to addChild() on nested children in vanilla ActionScript 3 to do anything

I'm having some trouble visualizing the recursive calls of the Combination Sum problem?

Having trouble converting external Windows Crypto API calls from C# to F#

Why does strcat to getenv() changes subsequent getenv() calls?

Mocking a class having @Configuration annotation