Showing posts with label junit. Show all posts
Showing posts with label junit. Show all posts

Thursday, 29 September 2022

How to execute tests in a maven project?

In this post, I am going to explain various scenarios to execute tests in a maven project.

 

Demo application has three test classes, where each class has one or more test cases.

 


 

Scenario 1: Execute all the tests.

mvn test

Scenario 2: Execute single test class.

mvn -Dtest=TestClass1 test

Above command execute all the tests in TestClass1.

Scenario 3: Execute tests in multiple test classes.

mvn -Dtest=TestClass1,TestClass3 test

Above command execute all the tests in TestClass1, TestClass3.

 

Scenario 4: Run single test case from a test class.

mvn -Dtest=TestClass1#testCase1 test

Scenario 5: Run all the test cases that match to a pattern in a test class.

mvn -Dtest=TestClass1#*2 test

Above snippet run all the test cases where the test case name ends with 2 in TestClass1.

 

You can download this application from this link.



Previous                                                 Next                                                 Home

Tuesday, 12 July 2022

How to test the ordering of collection or list elements in junit?

Sometimes you may want to check whether given collection has elements in the specific order or not. Using IsIterableContainingInOrder class, you can address this use case.

 


CollectionElementsOrderTest.java

package com.sample.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Assert;
import org.junit.Test;

public class CollectionElementsOrderTest {
	
	@Test
	public void collectionTest() {
		final List<Integer> actual = Arrays.asList(11, 3,7, 5, 2);
		Collections.sort(actual);
		
		Assert.assertThat(actual , IsIterableContainingInOrder.contains(2, 3, 5, 7, 11));
	}

}

 

 

 

Previous                                                 Next                                                 Home

Friday, 26 February 2021

EnvironmentVariables: Set and clear Environment Variables

'EnvironmentVariables' rule allows you to set environment variables for your test. All changes to environment variables are reverted after the test.

 

Example

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

environmentVariables.set(COUNTRY_KEY, "India");
environmentVariables.set(STATE_KEY, "Andhra Pradesh");

assertEquals("India", System.getenv(COUNTRY_KEY));
assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

Find the below working application.

 

EnvironmentVariablesTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class EnvironmentVariablesTest {
	@Rule
	public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	private void printEnvProps() {
		System.out.println(COUNTRY_KEY + "-> " + System.getenv(COUNTRY_KEY));
		System.out.println(STATE_KEY + "-> " + System.getenv(STATE_KEY));
	}

	@Test
	public void test() {
		printEnvProps();

		System.out.println("\nSetting Environment Variables\n");
		environmentVariables.set(COUNTRY_KEY, "India");
		environmentVariables.set(STATE_KEY, "Andhra Pradesh");

		assertEquals("India", System.getenv(COUNTRY_KEY));
		assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

		printEnvProps();

		System.out.println("\nClearning the key 'country'\n");
		environmentVariables.clear(COUNTRY_KEY);

		assertNull(System.getenv(COUNTRY_KEY));
		assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

		printEnvProps();
	}

}


Output

country-> null
state-> null

Setting Environment Variables

country-> India
state-> Andhra Pradesh

Clearning the key 'country'

country-> null
state-> Andhra Pradesh


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules





 

 

Previous                                                    Next                                                    Home

ExpectedSystemExit: Test System exits

Follow below steps to test system exit codes.

 

Step 1: Create a rule of type ExpectedSystemExit.

@Rule

public final ExpectedSystemExit exit = ExpectedSystemExit.none();

 

Step 2: Use 'expectSystemExitWithStatus' method to test system exit with status code.

exit.expectSystemExitWithStatus(0);

 

SystemExitApp.java

package com.sample.app;

public class SystemExitApp {

	public void exitApp() {
		System.exit(0);
	}

}

 

SystemExitAppTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

import com.sample.app.SystemExitApp;

public class SystemExitAppTest {
	@Rule
	public final ExpectedSystemExit exit = ExpectedSystemExit.none();

	@Test
	public void exitApp() {
		exit.expectSystemExitWithStatus(0);

		SystemExitApp app = new SystemExitApp();

		app.exitApp();
	}

}

 

You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 

 

 

  

Previous                                                    Next                                                    Home

DisallowWriteToSystemErr: Disable System error messages

‘DisallowWriteToSystemErr’ class make the test fail, if the test tries to write some data to System.err.

 

DisallowWriteToSystemErrTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.DisallowWriteToSystemErr;

public class DisallowWriteToSystemErrTest {
	@Rule
	public final DisallowWriteToSystemErr disallowWriteToSystemErr = new DisallowWriteToSystemErr();

	@Test
	public void thisTestFail() {
		System.err.println("Hello World");
	}
}

 

You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 

 

 

  

Previous                                                    Next                                                    Home

DisallowWriteToSystemOut: Disable System output messages

‘DisallowWriteToSystemOut’ class make the test fail, if the test tries to write some data to System.out.

 

DisallowWriteToSystemOutTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.DisallowWriteToSystemOut;

public class DisallowWriteToSystemOutTest {
	@Rule
	public final DisallowWriteToSystemOut disallowWriteToSystemOut = new DisallowWriteToSystemOut();

	@Test
	public void thisTestFail() {
		System.out.println("Hello World");
	}
}

 

 

 

Previous                                                    Next                                                    Home

TextFromStandardInputStream: Create tests that depends on input (System.in)

‘TextFromStandardInputStream’ class is used to create tests that depends on System input (System.in).

 

'provideLines' method is used to provide input to the program.

 

Arithmetic.java

package com.sample.app;

import java.util.Scanner;

public class Arithmetic {

	public int sum() {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		return a + b;
	}
}

As you see sum() method, it takes two integers a and b, return sum of a and b.

 

We can pass input 1, 2 using provideLines method like below.

@Rule
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();

systemInMock.provideLines("1", "2");


ArithmeticTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;

import com.sample.app.Arithmetic;

public class ArithmeticTest {
	@Rule
	public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();

	private Arithmetic arithemtic = new Arithmetic();

	@Test
	public void sumOf1And2() {
		systemInMock.provideLines("1", "2");
		assertEquals(3, arithemtic.sum());
	}
}


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 





 

  

Previous                                                    Next                                                    Home