Showing posts with label assertfalse. Show all posts
Showing posts with label assertfalse. Show all posts

Wednesday, 24 March 2021

Bean Validation: @AssertFalse: Validate falsity of the element

If you annotate any element with @AssertFalse annotation, then the annotated element must be false.

 

Example

@AssertFalse

private boolean isExperienced;

 

Supported Types

a.   boolean

b.   Boolean

 

Where can I apply this annotation?

a.   METHOD,

b.   FIELD,

c.    ANNOTATION_TYPE,

d.   CONSTRUCTOR,

e.   PARAMETER,

f.     TYPE_USE

 

Employee.java

package com.sample.model;

import javax.validation.constraints.AssertFalse;

public class Employee {

	private int id;

	@AssertFalse
	private boolean isExperienced;

	public Employee(int id, boolean isExperienced) {
		this.id = id;
		this.isExperienced = isExperienced;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isExperienced() {
		return isExperienced;
	}

	public void setExperienced(boolean isExperienced) {
		this.isExperienced = isExperienced;
	}

}

Test.java

package com.sample.test;

import java.util.Set;

import javax.validation.*;
import javax.validation.ValidatorFactory;

import com.sample.model.Employee;

public class Test {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {

		Employee emp1 = new Employee(123, false);
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(234, true);
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);
	}
}


Run above application, you can able to see below output in the console.

Validation errors on bean emp1
************************************
No validation errors....

Validation errors on bean emp2
************************************
isExperienced,must be false





 

  

Previous                                                    Next                                                    Home

Thursday, 12 March 2020

TestNG: assertFalse: Assert condition is false

'assertFalse' method is used to assert that a condition is false. It is available in below two forms.

public static void assertFalse(boolean condition)
public static void assertFalse(boolean condition, String message)

When the assertion failed, then the test case fails with given message.

Calculator.java
package com.sample.app.arithmetic;

public class Calculator {
 public int add(int a, int b) {
  return a + b;
 }

 public int subtract(int a, int b) {
  return a - b;
 }

 public int mul(int a, int b) {
  return a * b;
 }

 public int div(int a, int b) {
  return a / b;
 }

 public int remainder(int a, int b) {
  return a % b;
 }

 public boolean isEven(int number) {
  return number % 2 == 0;
 }

 public boolean isOdd(int number) {
  return number % 2 != 0;
 }
}

AssertFalseTest.java
package com.sample.app.assertions;

import static org.testng.Assert.assertFalse;

import org.testng.annotations.Test;

import com.sample.app.arithmetic.Calculator;

public class AssertFalseTest {
 private Calculator calc = new Calculator();

 @Test
 public void isEven_4_true() {
  assertFalse(!calc.isEven(4));
 }

 @Test
 public void isEven_5_false() {
  assertFalse(calc.isEven(5));
 }

 @Test
 public void failMe() {
  assertFalse(true, "Failing the test case");
 }
}

Run ‘AssertFalseTest.java’, you will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
PASSED: isEven_4_true
PASSED: isEven_5_false
FAILED: failMe
java.lang.AssertionError: Failing the test case did not expect to find [false] but found [true]
 at org.testng.Assert.fail(Assert.java:97)
 at org.testng.Assert.failNotEquals(Assert.java:969)
 at org.testng.Assert.assertFalse(Assert.java:65)
 at com.sample.app.assertions.AssertFalseTest.failMe(AssertFalseTest.java:24)
 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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
 at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
 at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
 at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
 at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
 at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
 at java.util.ArrayList.forEach(ArrayList.java:1257)
 at org.testng.TestRunner.privateRun(TestRunner.java:770)
 at org.testng.TestRunner.run(TestRunner.java:591)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
 at org.testng.SuiteRunner.run(SuiteRunner.java:304)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
 at org.testng.TestNG.runSuites(TestNG.java:1032)
 at org.testng.TestNG.run(TestNG.java:1000)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
===============================================



Previous                                                    Next                                                    Home

Sunday, 16 June 2019

Bean Validation: @AssertFalse: Validate falsity of the element

If you annotate any element with @AssertFalse annotation, then the annotated element must be false.

Supported Types
a.   boolean
b.   Boolean

Where can I apply this annotation?
a.   METHOD,
b.   FIELD,
c.    ANNOTATION_TYPE,
d.   CONSTRUCTOR,
e.   PARAMETER,
f.     TYPE_USE

Employee.java
package com.sample.model;

import javax.validation.constraints.AssertFalse;

public class Employee {

 private int id;

 @AssertFalse
 private boolean isExperienced;

 public Employee(int id, boolean isExperienced) {
  this.id = id;
  this.isExperienced = isExperienced;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public boolean isExperienced() {
  return isExperienced;
 }

 public void setExperienced(boolean isExperienced) {
  this.isExperienced = isExperienced;
 }

}


Test.java

package com.sample.test;

import java.util.Set;

import javax.validation.*;
import javax.validation.ValidatorFactory;

import com.sample.model.Employee;

public class Test {
 private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
 private static Validator validator = validatorFactory.getValidator();

 private static void validateBean(Employee emp) {
  System.out.println("************************************");
  Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

  if (validationErrors.size() == 0) {
   System.out.println("No validation errors....");
  }

  for (ConstraintViolation<Employee> violation : validationErrors) {
   System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
  }
  System.out.println("");
 }

 public static void main(String args[]) {

  Employee emp1 = new Employee(123, false);
  System.out.println("Validation errors on bean emp1");
  validateBean(emp1);

  Employee emp2 = new Employee(234, true);
  System.out.println("Validation errors on bean emp2");
  validateBean(emp2);
 }
}


Run above application, you can able to see below output in the console.

Validation errors on bean emp1
************************************
No validation errors....

Validation errors on bean emp2
************************************
isExperienced,must be false



Previous                                                 Next                                                 Home