Syed
Syed

Reputation: 2597

Not able to disable Spring Vault during unit tests in Java

I've disabled the spring vault in my unit tests. But however, its still enabling it and running in the back ground. This is my code. Is there any issue in the below code

@RunWith(SpringRunner.class)

@SpringBootTest(properties = "spring.cloud.vault.enabled=false", classes = 
 DummyTest.class)
public class DummyTest{
    
    @Test
    public void getDummyTest() throws Exception{
        assertTrue(true);
    }
    
}

Following is the exception that its printing in the console

Caused by: java.lang.IllegalArgumentException: Token (spring.cloud.vault.token) must not be empty
at org.springframework.util.Assert.hasText(Assert.java:287)
at org.springframework.cloud.vault.config.ClientAuthenticationFactory.createClientAuthentication(ClientAuthenticationFactory.java:108)
at org.springframework.cloud.vault.config.VaultBootstrapConfiguration.clientAuthentication(VaultBootstrapConfiguration.java:206)
at org.springframework.cloud.vault.config.VaultBootstrapConfiguration$$EnhancerBySpringCGLIB$$1d0bfc2.CGLIB$clientAuthentication$3(<generated>)
at org.springframework.cloud.vault.config.VaultBootstrapConfiguration$$EnhancerBySpringCGLIB$$1d0bfc2$$FastClassBySpringCGLIB$$7f75c921.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at org.springframework.cloud.vault.config.VaultBootstrapConfiguration$$EnhancerBySpringCGLIB$$1d0bfc2.clientAuthentication(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 99 more

Upvotes: 4

Views: 7343

Answers (4)

TheLittleChimp
TheLittleChimp

Reputation: 31

Another solution is to exclude the VaultAutoConfiguration.class

Like so:

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.config.VaultAutoConfiguration;

@SpringBootTest
@EnableAutoConfiguration(exclude = VaultAutoConfiguration.class)
class ApplicationTest {

    @Test
    void contextLoads() {
    }

}

Upvotes: 1

Abhishek Kotalwar
Abhishek Kotalwar

Reputation: 518

I'm able to fix this issue using

azure.keyvault.enabled=false

Upvotes: 0

Alexander Tilkin
Alexander Tilkin

Reputation: 319

Spring Cloud Vault doesn't support bootstrap.yml anymore. Hence, you have to write the configuration in application.yml.

When you use application context: WebFluxTest, SpringBootTest, Autowire, etc. the test loads application context that results in reading the application.yml file

To avoid loading Vault in UTs you need to create an application.yml file in the resources folder in the Test folder and add the following line:

spring.cloud.vault.enabled: false

Note: yml = properties (whatever format you use)

enter image description here

Upvotes: 2

fbiberger
fbiberger

Reputation: 61

I had the exact same problem yesterday. The issue had been solved in this question: Caused by: java.lang.IllegalArgumentException: Token (spring.cloud.vault.token) must not be empty - Hashicorp Vault

Your test class is loaded in application context, but the error you get is thrown in bootstrap context. spring.cloud-properties are always loaded in that context. The solution is to set spring.cloud.vault.enabled=false in bootstrap context, e.g. you can put it in a bootstrap.yml file in your test-ressources.

Upvotes: 6

Related Questions