<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manuel Vargas</title>
    <description>The latest articles on DEV Community by Manuel Vargas (@manuelvargastapia).</description>
    <link>https://dev.to/manuelvargastapia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F561598%2F9a697a99-81fb-4194-8d0c-c7f6dc77fdb9.png</url>
      <title>DEV Community: Manuel Vargas</title>
      <link>https://dev.to/manuelvargastapia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manuelvargastapia"/>
    <language>en</language>
    <item>
      <title>How to test static members with Mockito in Flutter</title>
      <dc:creator>Manuel Vargas</dc:creator>
      <pubDate>Sun, 17 Jan 2021 17:49:06 +0000</pubDate>
      <link>https://dev.to/manuelvargastapia/how-to-test-static-members-with-mockito-in-flutter-2139</link>
      <guid>https://dev.to/manuelvargastapia/how-to-test-static-members-with-mockito-in-flutter-2139</guid>
      <description>&lt;p&gt;I'm starting my journey with unit testing in Flutter. Early in the process, while I was writing tests for my Firebase sign in workflow, I faced a situation that I struggled to surpass and want to share. I needed it to test a static member call.&lt;/p&gt;

&lt;p&gt;Concretely, I wanted to test a call to the &lt;code&gt;GoogleAuthProvider.credential()&lt;/code&gt; static method. I wanted to do this to get the OAuth credentials and pass them to &lt;code&gt;FirebaseAuth.signInWithCredential()&lt;/code&gt;. This way I could use Google Sign In through Firebase Authentication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F44if06juxtjp6siu7283.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F44if06juxtjp6siu7283.png" alt="call to GoogleAuthProvider.credential()"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In my test, I couldn't mock this static method call. All my attempts failed. My only hope was to refactor my code to make it testable for this particular case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdgj3xbeoft99t4p5i8so.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdgj3xbeoft99t4p5i8so.png" alt="failed test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After several failed attempts -such as creating a mock to override the static method, doing the same with Fake instead of Mock, etc-, I realized that the solution implies wrapping the call in a class, so I'll be able to test it indirectly. However, the test structure requires this new wrapper class to be passed as a dependency for the class where the relevant call takes place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feye0nbidfw7lxc9tmwgp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feye0nbidfw7lxc9tmwgp.png" alt="first attempt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, the test will pass if I mock this new wrapper class and use it in &lt;code&gt;when()&lt;/code&gt;. But I didn't want to depend on this class and make my overall architecture ugly and &lt;em&gt;ad-hoc&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Finally, I decided to make this little modification:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkwwmjj2du8h8xzdwvfs0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkwwmjj2du8h8xzdwvfs0.png" alt="little modification"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, making the dependency optional, I could mock my wrapper class, call it on &lt;code&gt;when()&lt;/code&gt;, and pass the test, without compromising my overall structure.&lt;/p&gt;

&lt;p&gt;Also, this new wrapper eventually could serve as a place to implement additional forms of authentication, besides GoogleOAuthCredentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkhb80q1hmn54z4qm3td4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkhb80q1hmn54z4qm3td4.png" alt="pass test"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;I'm learning a lot of unit testing with these kinds of situations and enjoying testing my apps. I hope eventually implement Widget and integration testing too for this same project.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>mockito</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
