Java Mockito测试框架
晚上好,第一次面对模拟对象,我试图测试我的应用程序,但什么也没发生。我应该测试自动取款机的工作。我有两个未实现的接口(帐户和卡)和一个类。 ATM(具有空方法)。我的任务是在ATM类中实现方法。但不要改变方法 这是我的代码,无法测试 1)接口卡:Java Mockito测试框架,java,unit-testing,junit,mocking,mockito,Java,Unit Testing,Junit,Mocking,Mockito,晚上好,第一次面对模拟对象,我试图测试我的应用程序,但什么也没发生。我应该测试自动取款机的工作。我有两个未实现的接口(帐户和卡)和一个类。 ATM(具有空方法)。我的任务是在ATM类中实现方法。但不要改变方法 这是我的代码,无法测试 1)接口卡: package myatm; public interface Card { public boolean isBlocked(); // checks whther card is blocked or not pub
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
2)接口账号:
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
3)等级ATM:
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
这是我的测试,我试着为一种方法编写,但它不起作用。请试着让我知道我做错了什么
5)包含Mockito的类:
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
我已使您的代码正常工作,但进行了以下更新:
- 已从测试方法中删除参数(
)李>@test 公共无效设置平衡(双x)
- 在ATM类中添加了getter/setter,因为它们用于测试和良好实践
- 添加了Junit runner注释
@RunWith(PowerMockRunner.class)
- 以及以下工作代码中显示的其他更新李>
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
MyATM.java:
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
ATM.java:
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM) { // we can set up the number of money in ATM
money = moneyInATM;
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode) {
boolean ret = false;
if ((card.checkPin(pinCode) == false) && (card.isBlocked() == false)) {
ret = false;
} else {
if ((card.checkPin(pinCode) == true) && (card.isBlocked() == true))
ret = true;
}
return ret;
}
Account acc = null;
// returns the total ammount of money
public double checkBalance() {
return acc.getBalance();
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Account getAcc() {
return acc;
}
public void setAcc(Account acc) {
this.acc = acc;
}
// method which is taking money from the account.
// Should check if sum is less then money in atm
public double getCash(double amount) {
double sum = amount;
if (this.checkBalance() > acc.getBalance()) {
sum = (acc.getBalance() - sum);
} else if (this.checkBalance() < acc.getBalance()) {
throw new IllegalArgumentException("Not enough money in ATM");
} else if (sum > acc.getBalance()) {
throw new UnsupportedOperationException(
"Not enought money on your account");
}
return sum;
}
}
测试结果:
package myatm;
public interface Card {
public boolean isBlocked(); // checks whther card is blocked or not
public Account getAccount(); // returns the balance connected with this card
public boolean checkPin(int pinCode); // checks the property of password
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
package myatm;
public class ATM {
public double money;
ATM(double moneyInATM){ //we can set up the number of money in ATM
money=moneyInATM;
}
public void setATM (ATM atm){
this.atm =atm;
}
public double getMoneyInATM() { //
return atm.checkBalance();
}
// checks pin code and card status(blocked or not)
// if blocked should send exception
// if pin is not correct should send exception too
public boolean validateCard(Card card, int pinCode){
boolean ret = false;
if ((card.checkPin(pinCode)==false) && (card.isBlocked()==false)){
ret=false;
}
else {
if((card.checkPin(pinCode)==true) && (card.isBlocked()==true))
ret = true;
}
return ret; }
Account acc = null;
//returns the total ammount of money
public double checkBalance(){
return acc.getBalance();
}
ATM atm = new ATM(10500);
// method which is taking money from the account.
//Should check if sum is less then money in atm
public double getCash(double amount){
double sum=amount;
if(atm.checkBalance()>acc.getBalance()){
sum=(acc.getBalance()-sum);
}
else if(atm.checkBalance()<acc.getBalance()){
throw new IllegalArgumentException("Not enough money in ATM");
}
else if (sum>acc.getBalance()){
throw new UnsupportedOperationException("Not enought money on your account");
}
return sum;
}}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
import static java.util.jar.Pack200.Packer.TRUE;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ATMtest {
ATM atm;
@Before
public void setup(){
Card card =mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
atm.setATM(atm);
atm = new ATM(1500);
}
@Test
public void setBalance (double x){
Assert.assertEquals(x, atm.checkBalance());
}
}
package myatm;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class ATMtest {
// Class Under Test
ATM atm;
@Before
public void setup() {
Card card = mock(Card.class);
Account acc = mock(Account.class);
when(card.isBlocked()).thenReturn(Boolean.FALSE);
when(card.checkPin(1234)).thenReturn(Boolean.TRUE);
when(acc.getBalance()).thenReturn((double) 1);
atm = new ATM(1500);
atm.setAcc(acc);
}
@Test
public void testCheckBalance() {
/* Test */
double result = atm.checkBalance();
/* Asserts */
Assert.assertEquals((double) 1, result, .001);
}
}
package myatm;
public class MyATM {
public static void main(String[] args) {
double moneyInATM = 1000;
ATM atm = new ATM(moneyInATM);
Card card = null;
atm.validateCard(card, 1234);
atm.checkBalance();
atm.getCash(999.99);
}
}
package myatm;
public interface Account {
public double getBalance(); // returns current balance
public double withdrow(double amount); // returns the sum which was taken.
}
您忘记了verify()-statementshow以获得测试通过的绿线?因为,我获得的所有内容都是assembly successfull。ATMtest.testCheckBalance()测试方法在我的代码中是绿色的。要执行ATMtest.testCheckBalance(),请在Eclipse中突出显示该测试,然后右键单击并选择RunAs->Junit test。今天晚些时候,我将在绿色JUnit视图中发布成功测试的屏幕截图@Alex Ferdison如果我正在使用netbeans,我也可以这样做吗?因为我试着搜索,但找不到:(我能找到的一切这只是sreen右角的一条小线,在那里我可以阅读ATM(测试),仅此而已:(