Merge pull request #503 from igeligel/master

Object Mother pattern #498
This commit is contained in:
Ilkka Seppälä 2016-11-26 20:22:48 +02:00 committed by GitHub
commit cc02ff472b
11 changed files with 522 additions and 1 deletions

31
object-mother/README.md Normal file
View File

@ -0,0 +1,31 @@
---
layout: pattern
title: Object Mother
folder: object-mother
permalink: /patterns/object-mother/
pumlid:
categories: Creational
tags:
- Java
- Difficulty-Beginner
---
## Object Mother
Define a factory of immutable content with separated builder and factory interfaces.
![alt text](./etc/object-mother.png "Object Mother")
## Applicability
Use the Object Mother pattern when
* You want consistent objects over several tests
* you want to reduce code for creation of objects in tests
* every test should run with fresh data
## Credits
* [Answer by David Brown](http://stackoverflow.com/questions/923319/what-is-an-objectmother) to the stackoverflow question: [What is an ObjectMother?](http://stackoverflow.com/questions/923319/what-is-an-objectmother)
* [c2wiki - Object Mother](http://c2.com/cgi/wiki?ObjectMother)
* [Nat Pryce - Test Data Builders: an alternative to the Object Mother pattern](http://www.natpryce.com/articles/000714.html)

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
router="FAN">
<class id="1" language="java" name="com.iluwatar.objectmother.RoyaltyObjectMother" project="object-mother"
file="/object-mother/src/main/java/com/iluwatar/objectmother/RoyaltyObjectMother.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="535" y="366"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.objectmother.King" project="object-mother"
file="/object-mother/src/main/java/com/iluwatar/objectmother/King.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="285" y="141"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.objectmother.Queen" project="object-mother"
file="/object-mother/src/main/java/com/iluwatar/objectmother/Queen.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="783" y="143"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="4" language="java" name="com.iluwatar.objectmother.Royalty" project="object-mother"
file="/object-mother/src/main/java/com/iluwatar/objectmother/Royalty.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="536" y="142"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<realization id="5">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="4"/>
</realization>
<realization id="6">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@ -0,0 +1,45 @@
@startuml
package com.iluwatar.objectmother {
class RoyaltyObjectMother {
+ RoyaltyObjectMother()
+ createDrunkKing() : King {static}
+ createFlirtyQueen() : Queen {static}
+ createHappyDrunkKing() : King {static}
+ createHappyKing() : King {static}
+ createNotFlirtyQueen() : Queen {static}
+ createSoberUnhappyKing() : King {static}
}
class Queen {
- isDrunk : boolean
- isFlirty : boolean
- isHappy : boolean
+ Queen()
+ getFlirted(king : King) : boolean
+ isFlirty() : boolean
+ makeDrunk()
+ makeHappy()
+ makeSober()
+ makeUnhappy()
+ setFlirtiness(flirtiness : boolean)
}
interface Royalty {
+ makeDrunk() {abstract}
+ makeHappy() {abstract}
+ makeSober() {abstract}
+ makeUnhappy() {abstract}
}
class King {
~ isDrunk : boolean
~ isHappy : boolean
+ King()
+ flirt(queen : Queen)
+ isHappy() : boolean
+ makeDrunk()
+ makeHappy()
+ makeSober()
+ makeUnhappy()
}
}
Queen ..|> Royalty
King ..|> Royalty
@enduml

48
object-mother/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<?xml version="1.0"?>
<!--
The MIT License
Copyright (c) 2016 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.14.0-SNAPSHOT</version>
</parent>
<artifactId>object-mother</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,66 @@
/**
* The MIT License
* Copyright (c) 2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.objectmother;
public class King implements Royalty {
boolean isDrunk = false;
boolean isHappy = false;
@Override
public void makeDrunk() {
isDrunk = true;
}
@Override
public void makeSober() {
isDrunk = false;
}
@Override
public void makeHappy() {
isHappy = true;
}
@Override
public void makeUnhappy() {
isHappy = false;
}
public boolean isHappy() {
return isHappy;
}
/**
* Method to flirt to a queen.
* @param queen Queen which should be flirted.
*/
public void flirt(Queen queen) {
boolean flirtStatus = queen.getFlirted(this);
if (flirtStatus == false) {
this.makeUnhappy();
} else {
this.makeHappy();
}
}
}

View File

@ -0,0 +1,69 @@
/**
* The MIT License
* Copyright (c) 2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.objectmother;
public class Queen implements Royalty {
private boolean isDrunk = false;
private boolean isHappy = false;
private boolean isFlirty = false;
@Override
public void makeDrunk() {
isDrunk = true;
}
@Override
public void makeSober() {
isDrunk = false;
}
@Override
public void makeHappy() {
isHappy = true;
}
@Override
public void makeUnhappy() {
isHappy = false;
}
public boolean isFlirty() {
return isFlirty;
}
public void setFlirtiness(boolean flirtiness) {
this.isFlirty = flirtiness;
}
/**
* Method which is called when the king is flirting to a queen.
* @param king King who initialized the flirt.
* @return A value which describes if the flirt was successful or not.
*/
public boolean getFlirted(King king) {
if (this.isFlirty && king.isHappy && !king.isDrunk) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,33 @@
/**
* The MIT License
* Copyright (c) 2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.objectmother;
public interface Royalty {
void makeDrunk();
void makeSober();
void makeHappy();
void makeUnhappy();
}

View File

@ -0,0 +1,83 @@
/**
* The MIT License
* Copyright (c) 2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.objectmother;
public final class RoyaltyObjectMother {
/**
* Method to create a sober and unhappy king. The standard paramters are set.
* @return An instance of {@link com.iluwatar.objectmother.King} with the standard properties.
*/
public static King createSoberUnhappyKing() {
return new King();
}
/**
* Method of the object mother to create a drunk king.
* @return A drunk {@link com.iluwatar.objectmother.King}.
*/
public static King createDrunkKing() {
King king = new King();
king.makeDrunk();
return king;
}
/**
* Method to create a happy king.
* @return A happy {@link com.iluwatar.objectmother.King}.
*/
public static King createHappyKing() {
King king = new King();
king.makeHappy();
return king;
}
/**
* Method to create a happy and drunk king.
* @return A drunk and happy {@link com.iluwatar.objectmother.King}.
*/
public static King createHappyDrunkKing() {
King king = new King();
king.makeHappy();
king.makeDrunk();
return king;
}
/**
* Method to create a flirty queen.
* @return A flirty {@link com.iluwatar.objectmother.Queen}.
*/
public static Queen createFlirtyQueen() {
Queen queen = new Queen();
queen.setFlirtiness(true);
return queen;
}
/**
* Method to create a not flirty queen.
* @return A not flirty {@link com.iluwatar.objectmother.Queen}.
*/
public static Queen createNotFlirtyQueen() {
return new Queen();
}
}

View File

@ -0,0 +1,89 @@
/**
* The MIT License
* Copyright (c) 2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.objectmother.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.iluwatar.objectmother.King;
import com.iluwatar.objectmother.Queen;
import com.iluwatar.objectmother.Royalty;
import com.iluwatar.objectmother.RoyaltyObjectMother;
public class RoyaltyObjectMotherTest {
@Test
public void unsuccessfulKingFlirt() {
King soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
Queen flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
soberUnhappyKing.flirt(flirtyQueen);
assertFalse(soberUnhappyKing.isHappy());
}
@Test
public void queenIsBlockingFlirtCauseDrunkKing() {
King drunkUnhappyKing = RoyaltyObjectMother.createDrunkKing();
Queen notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
drunkUnhappyKing.flirt(notFlirtyQueen);
assertFalse(drunkUnhappyKing.isHappy());
}
@Test
public void queenIsBlockingFlirt() {
King soberHappyKing = RoyaltyObjectMother.createHappyKing();
Queen notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
soberHappyKing.flirt(notFlirtyQueen);
assertFalse(soberHappyKing.isHappy());
}
@Test
public void successfullKingFlirt() {
King soberHappyKing = RoyaltyObjectMother.createHappyKing();
Queen flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
soberHappyKing.flirt(flirtyQueen);
assertTrue(soberHappyKing.isHappy());
}
@Test
public void testQueenType() {
Royalty flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
Royalty notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
assertEquals(flirtyQueen.getClass(), Queen.class);
assertEquals(notFlirtyQueen.getClass(), Queen.class);
}
@Test
public void testKingType() {
Royalty drunkKing = RoyaltyObjectMother.createDrunkKing();
Royalty happyDrunkKing = RoyaltyObjectMother.createHappyDrunkKing();
Royalty happyKing = RoyaltyObjectMother.createHappyKing();
Royalty soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
assertEquals(drunkKing.getClass(), King.class);
assertEquals(happyDrunkKing.getClass(), King.class);
assertEquals(happyKing.getClass(), King.class);
assertEquals(soberUnhappyKing.getClass(), King.class);
}
}

View File

@ -133,7 +133,8 @@
<module>promise</module>
<module>page-object</module>
<module>event-asynchronous</module>
</modules>
<module>object-mother</module>
</modules>
<dependencyManagement>
<dependencies>