Unit test: checking for a NPE where exists an error before

I am developing a plugin for PhpStorm, and in some point, it try to method.getContainingClass(). This method should return NULL when the class name is absent. Just to make easier to explain, my code is like that:

final PhpClass methodClass = method.getContainingClass();
assert methodClass != null;

Note that the assert is wrong here, just to make the code flow (my mistake here, because initially I thought that visitPhpMethod() is never called when the getContainingClass() could be null). And my problem is just to make this assert fail programmatically (from unit test). From real PhpStorm application, running the builded plugin, I can do that fails when I just remove the class name like that:

<?php
class { function x(){} }

// ~> java.lang.AssertionError (failed that methodClass != null)

But when I run the same code from unit test, it give that:

<?php
class<error descr="Expected: class name"> </error>{ function x(){} }

And with that, the visitPhpMethod() is not triggered (while in real PhpStorm application it does). In this way, is impossible to do a proof case, which is needly to I check if I can keep this assert or not (for instance).

5 comments
Comment actions Permalink
Official comment

Hi David, sorry for the long response. You've solved your problem, haven't you?

The behavior looks consistent to me, `getContainingClass()` is not equals to null in both real execution and tests while `getNameIdentifier()` is null for the code you've provided.

Comment actions Permalink

I thought on another possibility, but I don't know how make it works. Basically I removed the class name node programmatically (it I know how do), but I don't know how I make the visitor visit the element again (then I could call the visitPhpMethod() programmatically on current circunstances, where class name node doesn't exists). Some tip?

0
Comment actions Permalink

Okay, I found my issue.

On reality, the problem is not on getContaingClass(). It really doesn't returns null when called from visitPhpMethod(). I found my problem on another method that try to getNameIdentifier()from the containing class, in this case, it is really absent and I just try to return it from a @NotNullmethod.

0
Comment actions Permalink

Yes, I do. My problem was that getNameIdentifier() returns null in this case, and I do not checked it (I have asserted). But I already fixed. Thanks a lot!

0

Please sign in to leave a comment.