NullReferenceException on Received() in NSubstitute

26 views Asked by At

I have the following code I want to verify with NSubsitute:

public PkiBodyBuilder AddExtension(
            DerObjectIdentifier oid,
            bool critical,
            Asn1Encodable value)
{
     this.extensionsGenerator.AddExtension(oid, critical, value);
     return this;
}

I did

[TestMethod]
        public void AddExtension_KeyUsage_OK()
        {
            // arrange
            X509ExtensionsGenerator  mockExtensionsGenerator = Substitute.For<X509ExtensionsGenerator>();
            builderUnderTest.SetExtensionsGenerator(mockExtensionsGenerator );
            KeyUsage keyUsage = new KeyUsage(KeyUsage.KeyCertSign | KeyUsage.CrlSign);
            DerObjectIdentifier oid = X509Extensions.KeyUsage;

            // act
            builderUnderTest.AddExtension(oid, true, keyUsage.ToAsn1Object());

            // assert
            mockExtensionsGenerator.Received().AddExtension(Arg.Is<DerObjectIdentifier>(doi => oid.Equals(doi)),
                                                  Arg.Is<bool>(crit => crit),
                                                  Arg.Is<Asn1Encodable>(a1e => keyUsage.ToAsn1Object().Equals(a1e)));
        }

to verify the AddExtensions()-method of the internal ExteensionsGenerator has been called. However I receive the following error in the assert-Line:

Testname:   AddExtension_KeyUsage_OK
Test FullName:  Telenot.HiXServer.TMP.PKI.AddExtension.AddExtension_KeyUsage_OK
Testquelle: D:\Projekte\hixserver\Telenot.HiXServer.TMP.Unittests\PKI\PkiBodyBuilder\AddExtension.cs Zeile 39
Testergebnis:   Fehlgeschlagene
Testdauer:  0:00:00,0376248
Ergebnis StackTrace:    
bei Org.BouncyCastle.Asn1.X509.X509ExtensionsGenerator.AddExtension(DerObjectIdentifier oid, Boolean critical, Asn1Encodable extValue)
   bei Telenot.HiXServer.TMP.PKI.AddExtension.AddExtension_KeyUsage_OK() in D:\Projekte\hixserver\Telenot.HiXServer.TMP.Unittests\PKI\PkiBodyBuilder\AddExtension.cs:Zeile 52.
Ergebnis Meldung:   
Die Telenot.HiXServer.TMP.PKI.AddExtension.AddExtension_KeyUsage_OK-Testmethode hat eine Ausnahme ausgelöst: 
System.ArgumentException: error encoding value: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei Org.BouncyCastle.Asn1.X509.X509ExtensionsGenerator.AddExtension(DerObjectIdentifier oid, Boolean critical, Asn1Encodable extValue)

I just can't figure out why I get this exception. Could anybody help? Debugging shows that the mockExtensionsGenerator is definitely not null. Also oid and keyUsage are not null.

EDIT

I am well aware of this SO question. However it does not really answer my problem as I checked via debugger that none of the values at the line in question are null. I think it has something to do with my usage of NSubstitute, but as I am quite new to NSubstitute I cannot see what I might have done wrong.

0

There are 0 answers