Dev determine groovy datatype (#9235)
* add code files for "How to groovy data types" add code files for "How to groovy data types" * added Tests in example * Update pom.xml change tab into spaces * remove the package determine-datatype as per comments * Update pom.xml add dependency for tests * remvoe the unwanted junit5 dependency and refine the structure of code remvoe the unwanted junit5 dependency and refine the structure of code * add missing "()" * correct the package as per sugesstion
This commit is contained in:
parent
8f20c9cca4
commit
2b928b04ce
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.determinedatatype
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
|
||||||
|
private int ageAsInt
|
||||||
|
private Double ageAsDouble
|
||||||
|
private String ageAsString
|
||||||
|
|
||||||
|
Person() {}
|
||||||
|
Person(int ageAsInt) { this.ageAsInt = ageAsInt}
|
||||||
|
Person(Double ageAsDouble) { this.ageAsDouble = ageAsDouble}
|
||||||
|
Person(String ageAsString) { this.ageAsString = ageAsString}
|
||||||
|
}
|
||||||
|
class Student extends Person {}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.determinedatatype
|
||||||
|
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
import com.baeldung.determinedatatype.Person
|
||||||
|
|
||||||
|
public class PersonTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsInteger_thenReturnTrue() {
|
||||||
|
Person personObj = new Person(10)
|
||||||
|
Assert.assertTrue(personObj.ageAsInt instanceof Integer)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsDouble_thenReturnTrue() {
|
||||||
|
Person personObj = new Person(10.0)
|
||||||
|
Assert.assertTrue((personObj.ageAsDouble).getClass() == Double)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenParameterTypeIsString_thenReturnTrue() {
|
||||||
|
Person personObj = new Person("10 years")
|
||||||
|
Assert.assertTrue(personObj.ageAsString.class == String)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenClassName_WhenParameterIsInteger_thenReturnTrue() {
|
||||||
|
Assert.assertTrue(Person.class.getDeclaredField('ageAsInt').type == int.class)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
|
||||||
|
Person personObj = new Person()
|
||||||
|
Assert.assertTrue(personObj instanceof Person)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
|
||||||
|
Student studentObj = new Student()
|
||||||
|
Assert.assertTrue(studentObj in Person)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGroovyList_WhenFindClassName_thenReturnTrue() {
|
||||||
|
def ageList = ['ageAsString','ageAsDouble', 10]
|
||||||
|
Assert.assertTrue(ageList.class == ArrayList)
|
||||||
|
Assert.assertTrue(ageList.getClass() == ArrayList)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGrooyMap_WhenFindClassName_thenReturnTrue() {
|
||||||
|
def ageMap = [ageAsString: '10 years', ageAsDouble: 10.0]
|
||||||
|
Assert.assertFalse(ageMap.class == LinkedHashMap)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue