Discuss / Java / 关于测试的问题

关于测试的问题

Topic source

Pyrojewel

#1 Created at ... [Delete] [Delete and Lock User]

按照当前的写法,通不过测试,想要通过测试必须要改成

```java

private Class<T> getParameterizedType() {

        Type type = getClass().getSuperclass().getGenericSuperclass();

```

java.lang.IllegalArgumentException: Class com.itranswarp.learnjava.service.UserServiceTest$1 does not have parameterized type.

不然会出现上述exception。但如果改成上面这样,运行的时候就会报错啦。请问有没有一种写法使得运行和测试都不报错呢?

numeiso

#2 Created at ... [Delete] [Delete and Lock User]

这样写:

private Class<T> getParameterizedType() {
    Class<?> cls = getClass();
    while (cls.getSuperclass() != AbstractDao.class) {
      cls = cls.getSuperclass();
    }
    Type type = cls.getGenericSuperclass();
    ...
}

这样不管几级继承都能正确找到直接继承 AbstractDao 的 Class


  • 1

Reply