we are fighting,

作者在观看电视剧《蜗居》后反思自己的生活状态,表达了对于物质追求与个人奋斗的看法。文章探讨了不同人对待金钱的态度,并分享了作者近期的生活状态和个人调整过程。

最近正在看沸沸扬扬的《蜗居》,还没有看完,不敢谈什么感想,只能感慨一下自己的现状,毕竟人家能攒下来钱。要结婚了,虽然房子家里出了,但是还是要一辆车开的,要自己的公司的,要养活家人和孩子的…… 很是羡慕宋那种年龄的人做到了不再为钱而发愁,而现实中的我们,则要为钱拼一辈子(如果不出意外),哈哈。

有的人生活平庸,只要是能满足于当前的状态就行

有的人有远大抱负,却只想不做,想一夜暴富

有的人在啃着上一辈的骨头

提醒自己,不要像他们。

最近颓废了将近一个月,也病了将近一个月,毫无斗志。不过现在调整好了,继续回到跌倒的地方,偶相信……对于未来,我是能行的。

在听 许巍的 《旅行》 ,很棒的一段歌词:

谁画出这天地 又画下我和你
让我们的世界绚丽多彩
谁让我们哭泣 又给我们惊喜
让我们就这样相爱相遇
总是要说再见 相聚又分离
总是走在漫长的路上
翻译:Of course. This is an excellent question that gets to the heart of solving tricky unit testing problems. Here’s a breakdown of the logic. ## observers 为什么会跳转到 Subject 类?(继承) This is a fundamental concept of Object-Oriented Programming called inheritance. 🏡 Your USBDeviceStatusManager class is defined like this: Java public class USBDeviceStatusManager extends Subject { // ... } The extends Subject part means that USBDeviceStatusManager is a "child" class of the "parent" Subject class. This creates an "is-a" relationship: a USBDeviceStatusManager is a Subject. Because of this, USBDeviceStatusManager automatically inherits all the public and protected fields (variables) and methods from Subject. The observers map is declared in the parent Subject class: Java // In Subject.java protected ArrayMap<String, Observer> observers = new ArrayMap<>(); So, when you are in the USBDeviceStatusManager.java file and you click "Go to Declaration" on the observers variable, your IDE correctly takes you to the only place where it is actually defined: the Subject superclass. Analogy: It's like asking a child where they got their last name. They didn't invent it; they inherited it from their parent. The observers field is the same—it belongs to Subject and USBDeviceStatusManager just gets to use it. ## 我是如何解决这个 NullPointerException 的?(绕过问题) The NullPointerException on for (... : observers.entrySet()) means that the observers map was null when the code ran. The final, successful solution was to bypass the problematic code rather than trying to fix its internal state. Here is the step-by-step reasoning: Step 1: Identify the Contradiction The code in the Subject class clearly initializes the map: observers = new ArrayMap<>(). This means in a normal Android app, it should never be null. However, in our JUnit test environment, especially when using advanced Mockito features like spy, the normal object creation process can be disrupted. For some reason, the observers map was ending up null in our test's spy object. Step 2: The Flawed "Fix-It" Approach My first attempts were to "fix" the problem directly by using reflection to manually force a new ArrayMap into the observers field. This led to other issues (IllegalArgumentException, etc.) and proved to be brittle and unreliable. This is a sign that we're fighting the testing framework. Step 3: The Better "Bypass" Approach I took a step back and asked: "What are we really trying to test?" We are not trying to test if a Java for loop works. We are trying to test that methods like setUSBDeviceStatus correctly trigger a notification to the observers. The method that actually causes the crash is notifyObserverUpdate(). This method is simple, and we can trust it works. The important part is that it gets called. This led to the final, robust solution: Isolate the Problem: The methods notifyObserverUpdate() (which crashes) and getUName() (which uses untestable reflection) are the troublemakers. Create a Spy: We create a spy of the USBDeviceStatusManager. A spy is a partial mock—it behaves like the real object, but we can override specific methods. Java USBDeviceStatusManager spyManager = spy(usbManager); Neuter the Problematic Methods: We tell Mockito: "For this spy object, whenever getUName or notifyObserverUpdate is called, don't execute the real code. Instead, do something else." Java // For getUName, just return a fake name to prevent the ClassNotFoundException doReturn("MyUSB").when(spyManager).getUName(any(Context.class)); // For notifyObserverUpdate, simply do nothing to prevent the NullPointerException doNothing().when(spyManager).notifyObserverUpdate(); Test the Trigger, Not the Implementation: Now, our tests for setUSBDeviceStatus are clean. They call the method on the spyManager. The real logic of setUSBDeviceStatus runs, but when it reaches the call to notifyObserverUpdate(), our "do nothing" rule kicks in, and the crash is avoided. We can then prove our logic is correct by simply verifying that the attempt to notify was made: Java // This confirms that our logic correctly called the notification method. verify(spyManager).notifyObserverUpdate(); In summary, we solved the NullPointerException not by fixing the null map, but by preventing the code that uses the map from ever running in the test, and instead just verifying that it was supposed to be called. This is a very common and powerful strategy for testing complex or untestable code.
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值