Skip to content

Instantly share code, notes, and snippets.

@liuzy163
Created August 19, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzy163/b6a545ac44af338313881d733ad97c48 to your computer and use it in GitHub Desktop.
Save liuzy163/b6a545ac44af338313881d733ad97c48 to your computer and use it in GitHub Desktop.
package zl.ca.test;
public class Fighter extends Person {
private int power;
public Fighter(String name, int age) {
super(name, age);
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
}
package zl.ca.test;
public class GameController {
public void twoPersonFight(String name1, String name2, int age1, int age2){
Person p1 = PersonFactory.createPerson(name1, age1);
Person p2 = PersonFactory.createPerson(name2, age2);
Fighter f1 = PersonFactory.createFighter(p1);
Fighter f2= PersonFactory.createFighter(p2);
fight(f1, f2);
}
public void fight(Fighter... fighters){
//todo
}
}
package zl.ca.test;
import org.hamcrest.Matchers;
import org.junit.Test;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;
import mockit.Verifications;
public class GameControllerTest {
@Tested
GameController gameController;
@Mocked
PersonFactory factoryNotInUse;
@Test
public void testCreatPerson() {
String name1 = "John";
int age1 = 20;
String name2 = "Dan";
int age2 = 30;
new Expectations() {
{
Person person1 = new Person(name1, age1);
Person person2 = new Person(name2, age2);
PersonFactory.createPerson(name1, age1);
result = person1;
times = 1;
PersonFactory.createPerson(name2, age2);
result = person2;
times = 1;
PersonFactory.createFighter(person1);
times = 1;
PersonFactory.createFighter(person2);
times = 1;
}
};
gameController.twoPersonFight(name1, name2, age1, age2);
new Verifications() {
{
PersonFactory.createFighter(withArgThat(
Matchers.allOf(Matchers.<Person>hasProperty("name", Matchers.equalTo(name1)),
Matchers.hasProperty("age", Matchers.equalTo(age1)))));
times = 1;
PersonFactory.createFighter(withArgThat(
Matchers.allOf(Matchers.<Person>hasProperty("name", Matchers.equalTo(name2)),
Matchers.hasProperty("age", Matchers.equalTo(age2)))));
times = 1;
}
};
}
}
package zl.ca.test;
public class Person {
private String name;
private int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package zl.ca.test;
public class PersonFactory {
public static Person createPerson(String name, int age){
Person p = new Person(name, age);
return p;
}
public static Fighter createFighter(Person person ){
Fighter fighter = new Fighter(person.getName(), person.getAge());
fighter.setPower(100);
return fighter;
}
}
@liuzy163
Copy link
Author

This gist is to demonstrate how to use Hamcrest matchers with JMockit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment