我有这样的方法
public QueryResult<?> queryForObject(String queryKey, Class<?> clazz, String... queryParams) {
LOGGER.debug("Attempt to get query by key {}.", queryKey);
QueryResult<?> result = null;
try {
if (null != queryKey) {
String query = queries.get(queryKey);
LOGGER.debug("Retrieve query {} from the my using key {}.", query, queryKey);
query = String.format(query, (Object[]) queryParams); //line 47
LOGGER.debug("Formatted query {}.", query);
result = forceApi.query(query, clazz);
LOGGER.debug("Retrieved object {} using ForceApi", result);
}
return result;
} catch (ApiTokenException exc) {
throw exc;
} catch (ApiException exc) {
throw exc;
} catch (Exception exc) {
throw new SpecialRuntimeException(exc); // there NullPointerException line 59
}
}
我想测试整个方法 - 阻止try
和所有catch
阻止。
我为块尝试写了一个测试
@SuppressWarnings("unchecked")
@Test
public void testQueryForObjectReturn() {
Map<String, String> queries = createMock(Map.class);
QueryResult<?> result = createMock(QueryResult.class);
mockStatic(String.class);
expect(queries.get(QUERY_KEY)).andReturn(StringUtils.EMPTY);
expect(String.format(StringUtils.EMPTY, QUERY_PARAMS)).andReturn(StringUtils.EMPTY);
expect(forceApi.query(StringUtils.EMPTY, Account.class)).andReturn((QueryResult<Account>) result);
replayAll();
queryTemplate.setQueries(queries); // line 61
queryTemplate.queryForObject(QUERY_KEY, Account.class, QUERY_PARAMS);
verifyAll();
}
抛出NullPointerException。
我的类声明和setUp
方法看起来像这样。
public class SalesforceQueryTemplateTest {
private static final String QUERY_KEY = "QUERY";
private static final String QUERY_PARAMS = "1234";
private ForceApi forceApi;
private SalesforceQueryTemplate queryTemplate;
@Before
public void setUp() {
forceApi = createStrictMock(ForceApi.class);
queryTemplate = new SalesforceQueryTemplate();
queryTemplate.setForceApi(forceApi);
}
有什么问题? 附:每次我在测试时遇到麻烦......
堆栈跟踪
java.lang.NullPointerException: null
at java.util.regex.Matcher.getTextLength(Unknown Source) ~[na:1.7.0_67]
at java.util.regex.Matcher.reset(Unknown Source) ~[na:1.7.0_67]
at java.util.regex.Matcher.<init>(Unknown Source) ~[na:1.7.0_67]
at java.util.regex.Pattern.matcher(Unknown Source) ~[na:1.7.0_67]
at java.util.Formatter.parse(Unknown Source) ~[na:1.7.0_67]
at java.util.Formatter.format(Unknown Source) ~[na:1.7.0_67]
at java.util.Formatter.format(Unknown Source) ~[na:1.7.0_67]
at java.lang.String.format(Unknown Source) ~[na:1.7.0_67]
at xxx.xxxxxxx.xxxadapters.salesforce.repository.query.SalesforceQueryTemplate.queryForObject(SalesforceQueryTemplate.java:47) ~[bin/:na]
at xxx.xxxxxxx.xxxsalesforce.repository.query.SalesforceQueryTemplateTest.testQueryForObjectReturn(SalesforceQueryTemplateTest.java:61) [bin/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_67]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_67]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_67]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_67]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit-4.11.jar:na]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit-4.11.jar:na]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.11.jar:na]
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) [junit-4.11.jar:na]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) [junit-4.11.jar:na]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
xxx.xxxxxxx.xxxcommon.exception.SpecialRuntimeException: java.lang.NullPointerException
at xxx.xxxxxxx.xxxadapters.salesforce.repository.query.SalesforceQueryTemplate.queryForObject(SalesforceQueryTemplate.java:59)
at xxx.xxxxxxx.xxxsalesforce.repository.query.SalesforceQueryTemplateTest.testQueryForObjectReturn(SalesforceQueryTemplateTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Unknown Source)
at java.util.regex.Matcher.reset(Unknown Source)
at java.util.regex.Matcher.<init>(Unknown Source)
at java.util.regex.Pattern.matcher(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at xxx.xxxxxxx.xxxadapters.salesforce.repository.query.SalesforceQueryTemplate.queryForObject(SalesforceQueryTemplate.java:47)
... 25 more
答案 0 :(得分:0)
我的问题在这里:
forceApi = createStrictMock(ForceApi.class);
queryTemplate = new SalesforceQueryTemplate();
在创建对象时初始化非静态变量。在代码中,您在创建对象之前已分配了一个值。
请改为尝试:
queryTemplate = new SalesforceQueryTemplate();
forceApi = createStrictMock(ForceApi.class);