0

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.

Community
  • 1
  • 1
Frank
  • 2,036
  • 1
  • 20
  • 32
  • What is at line 39? – Patrick Hofman Jan 09 '17 at 12:59
  • At line 39 is the opening bracket of my TestMethod. Line 52 (further down the Stacktrace) is the call to mockExtensionsGenerator.Received(). – Frank Jan 09 '17 at 13:02
  • 1
    Is `X509ExtensionsGenerator.AddExtension` virtual? NSub will only work with interfaces or with virtual members. As an aside, a simpler way to write the assertion is `mockExtensionsGenerator.Received().AddExtension(oid, true, keyUsage.ToAsn1Object())` (don't need the `Arg.Is` stuff in this case). – David Tchepak Jan 09 '17 at 22:18
  • Hey thanks, that was helpful. Indeed, bouncycastle does not provide an interface nor does it declare AddExtension virtual. I resolved it by not using NSubstitute, but the real implementation and then asserting the content of the fields in question. Not so nice but writing a wrapper class just for this case seemed to be an overkill. – Frank Jan 10 '17 at 10:20

0 Answers0