本文介绍了为什么我的JSONObject相关单元测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用运行我的测试gradle testFlavorType JSONObject jsonObject1 = new JSONObject(); JSONObject jsonObject2 = new JSONObject(); jsonObject1.put(test,test); jsonObject2.put(test,test); assertEquals(jsonObject1.get(test),jsonObject2.get(test)); 上述测试成功。 jsonObject = new SlackMessageRequest(channel,message).buildBody(); String channelAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_CHANNEL); String messageAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_TEXT); assertEquals(channel,channelAssertion); assertEquals(message,messageAssertion); 但上述两个请求失败。堆栈跟踪表明channelAssertion和messageAssertion为null,但不确定原因。我的问题是:为什么以上两个断言失败? 以下是 SlackMessageRequest 。 公共类SlackMessageRequest 扩展BaseRequest { // region变量 public static final String JSON_KEY_TEXT =text; public static final String JSON_KEY_CHANNEL =channel; private String mChannel; private String mMessage; // endregion // region构造函数 public SlackMessageRequest(String channel,String message){ mChannel = channel; mMessage = message; } // endregion // region方法 @Override public MethodType getMethodType(){返回MethodType.POST; } @Override public JSONObject buildBody()抛出JSONException { JSONObject body = new JSONObject(); body.put(JSON_KEY_TEXT,getMessage()); body.put(JSON_KEY_CHANNEL,getChannel()); 返回机构; } @Override public String getUrl(){ returnhttp:// localhost:1337; } public String getMessage(){ return mMessage; } public String getChannel(){ return mChannel; } // endregion } 下面是堆栈跟踪: junit.framework.ComparisonFailure:expected:< @ tk>但是:< null> at junit.framework.Assert.assertEquals(Assert.java:100) at junit.framework.Assert.assertEquals(Assert.java:107) at junit.framework.TestCase.assertEquals (TestCase.java:269) at com.example.app.http.request.SlackMessageRequestTest.testBuildBody(SlackMessageRequestTest.java:30) 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:483) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141) at junit.framework.TestResult $ 1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java) :125)junit.framework.TestC的 ase.run(TestCase.java:129) at junit.framework.TestSuite.runTest(TestSuite.java:252) at junit.framework.TestSuite.run(TestSuite.java:247) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor。 processTestClass(JUnitTestClassProcessor.java:64) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50) 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:483) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle。 messaging.dispatch.ProxyDispatchAdapter $ DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at com.sun.proxy。$ Proxy2.processTestClass(Unknown Source) at org.gradle.api.internal.tasks。 testing.worker.TestWorker.processTestClass(TestWorker.java:106) 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:483) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle .messaging.remote.internal.hub.MessageHub $ Handler.run(MessageHub.java:360) at org.gradle.internal.concurrent.DefaultExecutorFactory $ StoppableExecutorImpl $ 1.run(DefaultExecutorFactory.java:64) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) at java.lang。 Thread.run(Thread.java:745) 编辑美国东部时间下午5:55 我发现我可以使用 System.out.println()然后通过运行 gradle testFlavorType --debug 查看结果,通过反复试验,我发现了以下奇怪的情况: @Ove rride public JSONObject buildBody()抛出JSONException { System.out.println(buildBody mChannel =+ mChannel); System.out.println(buildBody mMessage =+ mMessage); JSONObject body = new JSONObject(); body.put(JSON_KEY_TEXT,getMessage()); body.put(JSON_KEY_CHANNEL,getChannel()); if(body.length()!= 0){ Iterator< String> keys = body.keys(); if(keys.hasNext()){ do { String key = keys.next(); System.out.println(keys:+ key); } while(keys.hasNext()); } } else { System.out.println(没有键????); } 返回机构; } 出于某种原因,没有钥匙????打印出来了?!?!?!?!为什么?! 编辑美国东部时间下午6:20 null。我不知道这意味着什么(见下文)。由于我认为这是相关的,我的gradle文件包括以下内容: testOptions { unitTests.returnDefaultValues = true } 特别奇怪,因为如果我在测试中构造一个JSONObject,那么一切正常。但如果它是原始应用程序代码的一部分,则它不起作用并执行上述操作。 解决方案 JSONObject类是android SDK的一部分。这意味着默认情况下不能用于单元测试。 来自 http://tools.android.com/tech-docs/unit-testing-support 当您将测试选项设置为 testOptions { unitTests.returnDefaultValues = true } 你正在修理方法......不要嘲笑。问题,但结果是当你的代码使用 new JSONObject()你没有使用真正的方法时,你使用的是一个不做任何事情的模拟方法,它只返回一个默认值。这就是对象 null 的原因。 您可以在这个问题中找到解决问题的不同方法:使用Mockito时不会嘲笑Android方法 I'm running my tests using gradle testFlavorTypeJSONObject jsonObject1 = new JSONObject();JSONObject jsonObject2 = new JSONObject();jsonObject1.put("test", "test");jsonObject2.put("test", "test");assertEquals(jsonObject1.get("test"), jsonObject2.get("test"));The above test succeeds.jsonObject = new SlackMessageRequest(channel, message).buildBody();String channelAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_CHANNEL);String messageAssertion = jsonObject.getString(SlackMessageRequest.JSON_KEY_TEXT);assertEquals(channel, channelAssertion);assertEquals(message, messageAssertion);But the above two requests fail. The stack trace says that channelAssertion and messageAssertion are null, but not sure why. My question is: Why are the above two asserts failing?Below is the SlackMessageRequest.public class SlackMessageRequest extends BaseRequest { // region Variables public static final String JSON_KEY_TEXT = "text"; public static final String JSON_KEY_CHANNEL = "channel"; private String mChannel; private String mMessage; // endregion // region Constructors public SlackMessageRequest(String channel, String message) { mChannel = channel; mMessage = message; } // endregion // region Methods @Override public MethodType getMethodType() { return MethodType.POST; } @Override public JSONObject buildBody() throws JSONException { JSONObject body = new JSONObject(); body.put(JSON_KEY_TEXT, getMessage()); body.put(JSON_KEY_CHANNEL, getChannel()); return body; } @Override public String getUrl() { return "http://localhost:1337"; } public String getMessage() { return mMessage; } public String getChannel() { return mChannel; }// endregion}Below is the stacktrace:junit.framework.ComparisonFailure: expected:<@tk> but was:<null> at junit.framework.Assert.assertEquals(Assert.java:100) at junit.framework.Assert.assertEquals(Assert.java:107) at junit.framework.TestCase.assertEquals(TestCase.java:269) at com.example.app.http.request.SlackMessageRequestTest.testBuildBody(SlackMessageRequestTest.java:30) 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:483) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141) at junit.framework.TestResult$1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java:125) at junit.framework.TestCase.run(TestCase.java:129) at junit.framework.TestSuite.runTest(TestSuite.java:252) at junit.framework.TestSuite.run(TestSuite.java:247) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50) 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:483) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106) 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:483) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360) at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)EDIT 5:55PM ESTI've figured out that I can log with System.out.println("") and then see the results by running gradle testFlavorType --debug and by trial and error I've discovered the following weird situation:@Overridepublic JSONObject buildBody() throws JSONException { System.out.println("buildBody mChannel = " + mChannel); System.out.println("buildBody mMessage = " + mMessage); JSONObject body = new JSONObject(); body.put(JSON_KEY_TEXT, getMessage()); body.put(JSON_KEY_CHANNEL, getChannel()); if (body.length() != 0) { Iterator<String> keys = body.keys(); if (keys.hasNext()) { do { String key = keys.next(); System.out.println("keys: " + key); } while (keys.hasNext()); } } else { System.out.println("There are no keys????"); } return body;}For some reason, "There are no keys????" is printing out?!?!?!?! Why?!EDIT 6:20PM ESTI've figured out how to debug unit tests. According to the debugger, the assigned JSONObject is returning "null". I have no clue what this means (see below). Since I think this is relevant, my gradle file includes the following:testOptions { unitTests.returnDefaultValues = true}It's especially strange because if I construct a JSONObject inside the test, then everything works fine. But if it is part of the original application's code, then it doesn't work and does the above. 解决方案 The class JSONObject is part of the android SDK. That means that is not available for unit testing by default.From http://tools.android.com/tech-docs/unit-testing-supportWhen you set the test options totestOptions { unitTests.returnDefaultValues = true}you are fixing the "Method ... not mocked." problem, but the outcome is that when your code uses new JSONObject() you are not using the real method, you are using a mock method that doesn't do anything, it just returns a default value. That's the reason the object is null.You can find different ways of solving the problem in this question: Android methods are not mocked when using Mockito 这篇关于为什么我的JSONObject相关单元测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 20:44