java - How to return type I explicitly passed as parameter? -


i want store type parameter, when return , check in within junit test,

expected: instance of java.lang.string but: <class java.lang.string> java.lang.class 

this minimalized example of class...

public class contextvariableresult<t> {     private class<t> type;      public contextvariableresult(class<t> type) {         this.type = type;     }      //todo doesn't work     public class<t> gettype() {         return type;     } } 

i pass string.class constructor parameter.

my test looks this....

assertthat(result.gettype(), instanceof(string.class)); 

i thought hamcrest matcher wrong, can't use is(string.class) or isa(string.class) due compilation errors:

 method assertthat(t, matcher<? super t>) in type assert not applicable arguments (class<capture#3-of ?>,       matcher<string>) 

i tried return reflection object type, tried cast parameterizedtype, classcastexceptions , on.

i want method result "string". did wrong? better if don't need pass parameter "string.class", think type erasure problems.

you checking return value instance of string, e.g. "hello". method return class string, i.e. string.class.

i guess method returns wanted. in case not have ot use hamecrest validation. regular junit's assert.assertequals(string.class, result.gettype()) work you.


Comments