Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Mocking objects ,testing my class and interfaces

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Ukraine
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Mocking objects ,testing my class and interfaces

    Good evening, first time faced with mock objects, I tryed to test my application but nothing happened. I should test the work of ATM. I have got 2 interfaces which are not implemented (Account and Card) and one class. ATM (which has got empty methods). My task was too implement methods in class ATM, and to test all methods using mock objects. when i am trying to run this in netbeans i am getting just assembley succesfully, but i need green line which indicates whether my test passed or not.
    something like this:
    unitTestsResultsInNetBeans73WithHamcrestBeforeJUnitClasspath.jpg
    Here is my code i guess everything i did properly.
    1. Card inerface
    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
    }
    2.Account interface
    package myatm;
    public interface Account  {
    public double getBalance(); // returns current balance
    public double withdrow(double amount);   // returns the sum which was taken.
    }
    3.ATM class which i implemented
    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;
        }
    }
    4.ATM test (Here i wrote some test)
    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);
        }
     
    }
    6.MyATM class where i am creating objects and calling methods.
    [CODE]
    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);
    }
    }
    [CODE]


  2. #2
    Junior Member
    Join Date
    Nov 2013
    Location
    Ukraine
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Mocking objects ,testing my class and interfaces

    Good evening, first time faced with mock objects, I tryed to test my application but nothing happened. I should test the work of ATM. I have got 2 interfaces which are not implemented (Account and Card) and one class. ATM (which has got empty methods). My task was too implement methods in class ATM, and to test all methods using mock objects. when i am trying to run this in netbeans i am getting just assembley succesfully, but i need green line which indicates whether my test passed or not.
    something like this:
    unitTestsResultsInNetBeans73WithHamcrestBeforeJUnitClasspath.jpg
    Here is my code i guess everything i did properly.
    1. Card inerface
    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
    }
    2.Account interface
    package myatm;
    public interface Account  {
    public double getBalance(); // returns current balance
    public double withdrow(double amount);   // returns the sum which was taken.
    }
    3.ATM class which i implemented
    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;
        }
    }
    4.ATM test (Here i wrote some test)
    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);
        }
     
    }
    6.MyATM class where i am creating objects and calling methods.
    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);
        }
    }
    Do not judge strictly i'm just learning

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    How are you executing the program? What happens when you execute it?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Location
    Ukraine
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    i am using NetBeans ALT+F6 i am pressing all i can see in the right corner (bottom) , is just ATM(testing) and that's all and the result assembly is completed.

    --- Update ---

    i know that in Eclipse i can test using JUnit but how to do this with NetBeans?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    Sorry, I don't know how your IDE works. I use the java command to execute my java programs.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Location
    Ukraine
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    It's Okay, i will whait other people if they can ge me a suggestion by they way can you execute it using command line, i will be very appreciate to you.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    What happens when you execute the code using the java command from the commandline?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    You will not be able to run a JUnit class with the normal java convention (there is no main). You have to run with the JUnit tool.
    I'm not 100% sure, but I think you have to run it with this command (change paths when appropriate):
    java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Location
    Ukraine
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    there is no main? you mean main public static void main(String[] args)??

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Mocking objects ,testing my class and interfaces

    No... Well, technically not. The main is already implemented in junit's runner class. You don't create your own. You run the test class with JUnit (either via the command prompt or through your IDE). Runtime behavior is determined by your annotations. For example, the @Test annotation indicates that the method is a test method, the @Before annotation indicates that the method should be ran before every test method, the @After annotation indicates that the method should be ran after every test method, ect.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] Help with objects, methods, and class inheritance.
    By Gingerbread Fetus in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2013, 09:02 AM
  2. Help Testing Objects
    By ChocolateThunder in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 4th, 2013, 07:54 PM
  3. How to use constructors and class objects together.
    By IkeIII in forum Object Oriented Programming
    Replies: 5
    Last Post: April 1st, 2013, 11:16 AM
  4. Java automated testing tools for Unit testing
    By rameezraja in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2012, 08:51 AM
  5. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:40 PM

Tags for this Thread