<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Subclass_Mock_Objects</id>
		<title>Subclass Mock Objects - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Subclass_Mock_Objects"/>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Subclass_Mock_Objects&amp;action=history"/>
		<updated>2026-04-29T09:47:43Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Subclass_Mock_Objects&amp;diff=471&amp;oldid=prev</id>
		<title>Dt1nh6: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Subclass_Mock_Objects&amp;diff=471&amp;oldid=prev"/>
				<updated>2016-05-09T13:27:37Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 13:27, 9 May 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;' lang='en'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Dt1nh6</name></author>	</entry>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Subclass_Mock_Objects&amp;diff=470&amp;oldid=prev</id>
		<title>Macrop at 12:25, 26 September 2014</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Subclass_Mock_Objects&amp;diff=470&amp;oldid=prev"/>
				<updated>2014-09-26T12:25:08Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Tests]]&lt;br /&gt;
[[Category:nUnit]]&lt;br /&gt;
[[Category:Extreme Programming]]&lt;br /&gt;
[[Category:Tips]]&lt;br /&gt;
&lt;br /&gt;
== Subclass Mock Objects ==&lt;br /&gt;
Subclass mock objects is a mock object that is created by subclassing the class you want to test, and overriding some of its methods. Let's look at a simple class:&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c-sharp&amp;quot;&amp;gt;&lt;br /&gt;
public class MyUnit {&lt;br /&gt;
&lt;br /&gt;
  protected MyDependency dependency = null;&lt;br /&gt;
&lt;br /&gt;
  public MyUnit(MyDependency dep) {&lt;br /&gt;
    this.dependency = dep;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void doTheThing(String param) {&lt;br /&gt;
&lt;br /&gt;
    if(&amp;quot;one&amp;quot;.equals(param)) {&lt;br /&gt;
      dependency.callOne();&lt;br /&gt;
    } else {&lt;br /&gt;
      dependency.callTwo();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The class MyUnit is the class I am trying to unit test. In this unit test, I want to check if the MyUnit class calls the MyDependency class correctly. There are 3 ways to do this:&lt;br /&gt;
&lt;br /&gt;
# Create a Mock implementation of the MyDependency class, using a proxy object&lt;br /&gt;
# Create a subclass mock of the MyDependency class, and override the callOne() and callTwo() methods&lt;br /&gt;
# Create a subclass of the MyUnit class itself, and use that during testing.&lt;br /&gt;
 &lt;br /&gt;
In this text I will show you how to do nr. 3. From that method it should be fairly easy to figure out how to do number 2, in case you want that.&lt;br /&gt;
&lt;br /&gt;
Using method nr. 3 requires the following steps:&lt;br /&gt;
&lt;br /&gt;
# Encapsulate calls to MyDependency inside MyUnit&lt;br /&gt;
# Create a subclass of MyUnit&lt;br /&gt;
# Override the MyDependency call encapsualation methods.&lt;br /&gt;
I will show you how to perform these three steps, throughout the rest of this text.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Encapsulate calls to MyDepedency ===&lt;br /&gt;
&lt;br /&gt;
The first thing to do is to refactory the MyUnit class, so that all calls to the MyDependency class are encapsulated in their own methods. Here is how that looks:&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c-sharp&amp;quot;&amp;gt;&lt;br /&gt;
public class MyUnit {&lt;br /&gt;
&lt;br /&gt;
  protected MyDependency dependency = null;&lt;br /&gt;
&lt;br /&gt;
  public MyUnit(MyDependency dep) {&lt;br /&gt;
    this.dependency = dep;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void doTheThing(String param) {&lt;br /&gt;
&lt;br /&gt;
    if(&amp;quot;one&amp;quot;.equals(param)) {&lt;br /&gt;
      callOne();&lt;br /&gt;
    } else {&lt;br /&gt;
      callTwo();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  protected void callOne() {&lt;br /&gt;
      dependency.callOne();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  protected void callTwo() {&lt;br /&gt;
      dependency.callTwo();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how the two calls to MyDependency.callOne() and MyDependency.callTwo() are now encapsulated in two protected methods, callOne() and callTwo().&lt;br /&gt;
&lt;br /&gt;
We are now ready to create the MyUnit subclass.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Create a Subclass Mock of MyUnit ===&lt;br /&gt;
&lt;br /&gt;
The second step is to create a subclass mock of the MyUnit class. Here it is:&lt;br /&gt;
&lt;br /&gt;
public MyUnitMock extends MyUnit {&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c-sharp&amp;quot;&amp;gt;&lt;br /&gt;
  protected boolean callOneCalled = false;&lt;br /&gt;
  protected boolean callTwoCalled = false;&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  protected void callOne() {&lt;br /&gt;
    this.callOneCalled = true;&lt;br /&gt;
    super.callOne();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  protected void callTwo() {&lt;br /&gt;
    this.callTwoCalled = true;&lt;br /&gt;
    super.callTwo();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how the two overridden methods records if they are called or not, by setting a boolean to true.&lt;br /&gt;
&lt;br /&gt;
The next step is to use the MyUnitMock in your unit test.&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Using the MyUnitMock Class in Your Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
Here is a unit test method that uses the MyUnitMock class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c-sharp&amp;quot;&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void test() {&lt;br /&gt;
   MyUnitMock myUnitMock = new MyUnitMock();&lt;br /&gt;
&lt;br /&gt;
   myUnitMock.doTheThing(&amp;quot;one&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
   assertTrue (myUnitMock.callOneCalled);&lt;br /&gt;
   assertFalse(myUnitMock.callTwoCalled);&lt;br /&gt;
&lt;br /&gt;
   //reset mock before next call&lt;br /&gt;
   myUnitMock.callOneCalled = false;&lt;br /&gt;
&lt;br /&gt;
   myUnitMock.doTheThing(&amp;quot;two&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
   assertFalse(myUnitMock.callOneCalled);&lt;br /&gt;
   assertTrue (myUnitMock.callTwoCalled);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First an instance of the MyUnitMock class is created. Second, the doTheThing() method is called. Third, assertions are made about whether the callOne() and callTwo() method were invoked.&lt;br /&gt;
&lt;br /&gt;
=== Summary ===&lt;br /&gt;
&lt;br /&gt;
As you can see, it is possible to test almost all of a class by using subclass mocks, as described above. There are, however, situations where it works better to use a completely separate mock dependency object with the original class instead. Exactly when, depends on the concrete application.&lt;br /&gt;
&lt;br /&gt;
Just keep in mind, that the above is just a suggestion - one possible method. Use it when it make sense, and don't use it when it doesn't make sense. Use your judgement!&lt;/div&gt;</summary>
		<author><name>Macrop</name></author>	</entry>

	</feed>