Either targetObject or targetClass for the field must be specified

報錯: java.lang.IllegalArgumentException: Either targetObject or targetClass for the field must be specified

java.lang.IllegalArgumentException: Either targetObject or targetClass for the field must be specified

	at org.springframework.util.Assert.isTrue(Assert.java:116)
	at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:173)
	at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:105)
	at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:89)
	at com.gnete.sdywbc.trade.tradebean.MZSettRefundQueryTest.init(MZSettRefundQueryTest.java:148)
	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:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

報錯部分代碼及源碼如下:
測試類:

public class MZSettRefundQueryTest {
	@Autowired
    private CallPaymService callPaymService;
    ......
	ReflectionTestUtils.setField(callPaymService, "insurancePayRpcService", paymscInsurancePayRpcService);
	......
}

ReflectionTestUtils類:

public static void setField(@Nullable Object targetObject, @Nullable Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) {
    Assert.isTrue(targetObject != null || targetClass != null, "Either targetObject or targetClass for the field must be specified");
    if (targetObject != null && springAopPresent) {
        targetObject = AopTestUtils.getUltimateTargetObject(targetObject);
    }

    if (targetClass == null) {
        targetClass = targetObject.getClass();
    }

    Field field = ReflectionUtils.findField(targetClass, name, type);
    if (field == null) {
        throw new IllegalArgumentException(String.format("Could not find field '%s' of type [%s] on %s or target class [%s]", name, type, safeToString(targetObject), targetClass));
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Setting field '%s' of type [%s] on %s or target class [%s] to value [%s]", name, type, safeToString(targetObject), targetClass, value));
        }

        ReflectionUtils.makeAccessible(field);
        ReflectionUtils.setField(field, targetObject, value);
    }
}

分析:
可以看出報錯是因爲“targetObject ”或“targetClass ”爲空,但ReflectionTestUtils.setField裏的含有targetObject 參數(callPaymService),那麼可以想到是沒有把CallPaymService類注入進來。

解決辦法:
檢查代碼是否正確注入CallPaymService類,包名,最後別忘了檢查測試類是否繼承BaseTest,或者測試類是否帶有註解:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TradeProviderMain.class)
@Transactional

附:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TradeProviderMain.class)
@Transactional
public abstract class BaseTest   {
	
}

改正後爲:

public class MZSettRefundQueryTest extends BaseTest {
	@Autowired
    private CallPaymService callPaymService;
    ......
	ReflectionTestUtils.setField(callPaymService, "insurancePayRpcService", paymscInsurancePayRpcService);
	......
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章