如何用Mockito检查类中是否存在AndroidSupportInjection.inject(this)语句?
Great question! Let's start by clarifying a key point: Mockito on its own can't directly check if a specific line of code exists in a class. Mockito's sweet spot is mocking dependencies and verifying interactions with those mocks at runtime. But we can absolutely achieve your goal with a combination of PowerMockito (since we're dealing with a static method here) and runtime verification, or even static code analysis if you need to check for the literal presence of the line.
Option 1: Verify the Method is Called at Runtime (Most Practical for Tests)
This is the approach you'll want for unit tests, because it doesn't just check if the line exists—it confirms that the code actually executes AndroidSupportInjection.inject(this) when your target class runs. Here's how to do it with PowerMockito:
Step-by-Step Implementation
- First, set up your test class with PowerMockito's runner and prepare the static class for mocking:
@RunWith(PowerMockRunner.class) @PrepareForTest(AndroidSupportInjection.class) public class YourTargetClassTest { @Test public void testAndroidSupportInjectionIsInvoked() { // 1. Static mock the AndroidSupportInjection class PowerMockito.mockStatic(AndroidSupportInjection.class); // 2. Create an instance of your target class YourTargetClass targetInstance = new YourTargetClass(); // 3. Trigger the method where you expect inject() to be called // (e.g., onCreate() for an Activity/Fragment) targetInstance.onCreate(null); // 4. Verify that inject() was called with the target instance as the parameter PowerMockito.verifyStatic(AndroidSupportInjection.class); AndroidSupportInjection.inject(targetInstance); } }
Why This Works
- PowerMockito lets us mock static methods (something regular Mockito can't do), so we can track calls to
AndroidSupportInjection.inject(). - The
verifyStaticcall confirms that the method was invoked exactly as expected—this ensures your code isn't just containing the line, but actually executing it when it should.
Option 2: Check for the Literal Line of Code (Static Analysis)
If you truly need to verify that the line AndroidSupportInjection.inject(this) exists in the class (rather than being executed), you'll need to use bytecode analysis. Tools like ASM can parse the class file and look for the method call. This is less common in unit tests, but here's a high-level approach:
- Add the ASM dependency to your project.
- Write code to read your target class's bytecode, traverse its methods, and check for instructions that invoke
AndroidSupportInjection.inject().
A simplified snippet might look like this (you'd expand this for full functionality):
ClassReader cr = new ClassReader(YourTargetClass.class.getName()); cr.accept(new ClassVisitor(Opcodes.ASM9) { @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new MethodVisitor(Opcodes.ASM9, mv) { @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { // Check if this is the call we're looking for if (owner.equals("dagger/android/support/AndroidSupportInjection") && name.equals("inject") && desc.equals("(Ljava/lang/Object;)V")) { // Found the method call! You can assert this in your test assertTrue("AndroidSupportInjection.inject(this) is present", true); } super.visitMethodInsn(opcode, owner, name, desc, itf); } }; } }, 0);
Note
Static analysis checks for the presence of the code, but doesn't confirm it's ever executed. For most testing scenarios, the runtime verification (Option 1) is more valuable.
Key Notes
- Make sure you have the correct PowerMockito dependencies in your build file (e.g.,
powermock-module-junit4andpowermock-api-mockito2for Mockito 2.x). - If you're using a newer JUnit version (JUnit 5), you'll need to use PowerMock's JUnit 5 support.
内容的提问来源于stack exchange,提问作者Harmeet Kaur

