Showing posts with label environment variables. Show all posts
Showing posts with label environment variables. Show all posts

Monday, 5 July 2021

Junit5: @EnabledIfEnvironmentVariable, @DisabledIfEnvironmentVariable: Enable or disable based on environment variable

@EnabledIfEnvironmentVariable  is used to signal that the annotated test class or test method is only enabled if the value of the specified environment variable matches the specified regular expression.

 

Example

@Test
@EnabledIfEnvironmentVariable(named = "env", matches = "staging")
void onlyOnStagingEnvt() {
  assertTrue(true);
}

 

@DisabledIfEnvironmentVariable annotation is used to signal that the annotated test class or test method is disabled if the value of the specified environment variable matches the specified regular expression.

 

Example

@Test
@DisabledIfEnvironmentVariable(named = "env", matches = "staging")
void notOnStagingEnvt() {
  assertTrue(true);
}

 

EnvironmentVariableTest.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

public class EnvironmentVariableTest {

	@Test
	@EnabledIfEnvironmentVariable(named = "env", matches = "staging")
	void onlyOnStagingEnvt() {
		assertTrue(true);
	}

	@Test
	@DisabledIfEnvironmentVariable(named = "env", matches = "staging")
	void notOnStagingEnvt() {
		assertTrue(true);
	}
	
}

  Run above test class, you will observe that ‘onlyOnStagingEnvt’ test method gets skipped.

 


 

Let’s pass the environment variable ‘env’ by setting to ‘staging’ and re run the test.

 

 


Right click on 'EnvironmentVariableTest' -> Run As -> Run Configurations…

 


Go to ‘Environment’ tab, and add the environment variable ‘env’ and set it to the value ‘staging’.

 

Click on Run button.


You can observe that ‘notOnStagingEnvt()’ test method is disabled.

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Saturday, 9 May 2020

Docker: ENV statement: Set environment variables

ENV statement is used to set environment variables to a value.

Syntax
ENV <key> <value>
ENV <key>=<value> ...

ENV <key> <value>
This syntax is used to set value to a single variable.

Example
ENV product_name Versioning Control

ENV <key>=<value> ...
This syntax is used to define multiple variables.

Example
ENV product_name="Versioning Control" version=1.2.3

Step 1: Create a directory 'myDockerImage'.

Step 2: Create ‘Dockerfile’ in the directory 'myDockerImage'.
Dockerfile
FROM ubuntu

CMD echo "Welcome to Docker Application Development"

ENV product_name="Versioning Control" version=1.2.3

Step 3: Go to the directory where Dockerfile is located and execute below command.

docker image build -t welcomedocker .
$docker image build -t welcomedocker .

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/3 : CMD echo "Welcome to Docker Application Development"
 ---> Running in eeb2e9fe74ad
Removing intermediate container eeb2e9fe74ad
 ---> cb04a6a6b66b
Step 3/3 : ENV product_name="Versioning Control" version=1.2.3
 ---> Running in a617d9f9fb31
Removing intermediate container a617d9f9fb31
 ---> 0e9cc7d92630
Successfully built 0e9cc7d92630
Successfully tagged welcomedocker:latest

Step 4: Execute the command 'docker image ls', you can see all the images.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
welcomedocker       latest              0e9cc7d92630        32 seconds ago      88.9MB
busybox             latest              e4db68de4ff2        4 days ago          1.22MB
jboss/wildfly       latest              4e440230e01d        8 days ago          748MB

Step 5: Execute below command to get the environment variable product_name details.

docker run welcomedocker env | grep product_name

$docker run welcomedocker env | grep product_name
product_name=Versioning Control





Previous                                                    Next                                                    Home