When trying to use the method getFontOfFormattingRun() from ApachePOI 3.13, a NullPointerException in some cases. Which would be the cause?
Related
val signedTx = rpc.startTrackedFlow(
::CreateDeviceFlow,
deviceState,
DeviceContract.Commands.Create())
.returnValue.getOrThrow()
After an upgrade to 4.3 I ran into this issue currently, the call returnValue.getOrThrow throws a Kotlin NPE. The flow is not called at all.
Everything starting up without problems, Contracts are validated as well.
Any reasons I am missing why an NPE can be thrown?
Stupid mistake, changed the Notary's particulars, wasn't found did not cater for it, just used !! instead of ?: and throwing a proper exception.
When using Mockito with Kotlin, if I try to verify Mock calls, it work fine like (this is in a Spring test):
#MockBean
lateinit var fragmentProcessor: FragmentProcessor
verify(fragmentProcessor, timeout(20000)).processFragment(expectedFragment)
that gives the expected behaviour... but just doing something like:
verify(fragmentProcessor, timeout(20000)).processFragment(Mockito.eq(expectedFragment))
will give the following error:
Missing method call for verify(mock) here:
-> at uk.co.argos.productapi.services.kafka.KafkaConsumerServiceTest.testFragmentProcessorReceivesMessages(KafkaConsumerServiceTest.kt:47)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
the same happens with ArgumentCaptor or other matchers
Are you sure that you call fragmentProcessor.processFragment(expectedFragment) somewhere in your code before verify times out?
Error message says that you don't, so verify throws an exception (as it should do).
In this line:
verify(fragmentProcessor, timeout(20000)).processFragment(expectedFragment)
You don't use verify correctly (you have to use Mockito.eq): it doesn't verify anything so doesn't throw, but it doesn't mean that it works as you suppose.
A basic use case would be calling MyEventListener::class without having imported use MyNamespace\MyEventListener. The result would be a broken piece of code that's relatively hard to debug.
Does PHP 7 provide a directive to crash instead of returning the class name if no class exists? For example:
After calling use Foo\Bar;, Bar::class would return 'Foo\Bar'.
But if no import statement, PHP returns 'Bar', even though the class doesn't exist, not even in the global namespace.
Can I make it crash somehow?
The thing you need to keep in mind is that use Foo\Bar; is not "importing" anything. It is telling the compiler: when I say "Bar" I mean Bar from the namespace Foo.
Bar::class is substituted blindly with the string "Foo\Bar". It isn't checking anything.
Until you attempt to instantiate or interact with a class it will not check to see if it exists. That said, it does not throw an Exception, it throws an Error:
// this doesn't exist!
use Foo/Bar;
try {
$instanceOfBar = new Bar();
}
catch (Error $e) {
// catching an Exception will not work
// Throwable or Error will work
}
You can trap and check for non-existent classes at run time, but until you do it will happily toss around strings referring to classes that don't exist.
This is a blessing in the case of Laravel's IoC container and autoloader that abuses this to alias classes as convenient top-level objects. A curse, if you were expecting PHP to throw a fuss on ::class not existing.
Update:
My suggestion for anyone worried about this problem is to use PHPStan in your testing pipeline. It prevents a lot of mistakes, and unlike php -l it will catch if you were to try and interact with a non-existent class.
As far as I know you're going to get a nice error message when you try to instantiate a class that cannot be found through autoloading or explicitly added.
If you want to check if the class exists, first, try this:
$classOutsideNamespaceExists = class_exists('Bar');
$classInsideNameSpaceExists = class_exists('\\Foo\\Bar'));
Or you could try this syntax available since PHP 5.5:
class_exists(MyClass::class)
Finally, you can always use the tried and true method of a try-catch block.
try {
$instanceOfMyClass = new MyClass();
}
catch (Exception $e) {
// conclude the class does not exist and handle accordingly
}
PhpStorm proposes and generates hints like ArrayShape, Pure, etc.
But automatically it is adding
php use JetBrains\PhpStorm\ArrayShape;
or another.
Is not that dangerous that on some production server I will get error
'Class JetBrains\PhpStorm\ArrayShape not found'?
(c)LazyOne:
Well, just use composer require --dev jetbrains/phpstorm-attributes to add such classes to your project. See github.com/JetBrains/phpstorm-attributes
As long as instance of such a class is not actually gets instantiated (created) you should have no error because use statement is just a declaration.
How do you clear the excepted exception after setting the expectedException like so:
PHPUnit_Framework_TestCase::setExpectedException('Acme\Services\Forms\FormValidationException');
What I want to say to PHPUnit is: don't expect an exception anymore, please fail the test if you encounter one.
A unit test should only verify one aspect of the unit of code under test. So if you first want to expect some exception to be raised and then you don't that just means that you should implement these verifications in two separate tests.
Just call the same method again passing null as Exception argument:
\PHPUnit_Framework_TestCase::setExpectedException(null);
For example, I have a code like:
TEST_F(Testmyexception, testthrownexception)
{
EXPECT_THROW(throw new myexception(), myexception);
}
After compiling and running, it gives an error: Actual: it throws a different type.
Does anynone know the answer?
Thanks,
You're throwing a pointer to a myexception, so you have to expect a pointer in the check:
EXPECT_THROW(throw new myexception(), myexception*);
I think you want:
EXPECT_THROW(throw std::underflow_error(""), std::underflow_error);
BTW the syntax to throw an instance of myexception is:
throw myexception();
the "new" keyword should not be used here.