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 9 of 9

Thread: AssertionFailedError: Expected <1> but was <0>

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default AssertionFailedError: Expected <1> but was <0>

    Hey guys, I've been having this trouble with the error from my code, debugging doesn't work, but debugs keeps telling me it's because of a ClassNotFoundException which I don't know where to handle it. Anyone know how to fix the error and handle the exception here?

    My code:

    import junit.framework.TestCase;
    import java.util.*;


    public class PushPopTest extends TestCase {

    public void testExecute() throws Exception {


    Stack<Integer> s = new Stack<Integer>();
    SymbolTable st = new SymbolTable();
    int count = 0;
    Push pp = new Push("5");
    pp.execute(count,s,st);
    assertEquals(1,count); --> AssertionFailedError here ( Expected <1> but was <0>)
    assertEquals(5, (int)s.peek());


    Pop po = new Pop("x");
    po.execute(count, s, st);
    assertEquals(2, count);
    assertTrue(s.empty());
    assertEquals(5, st.getValue("x"));
    }

    }

    import java.util.Stack;


    public class Push implements Operation {

    String val;

    public Push(String value)
    {
    //this.com = command;
    this.val = value;

    }


    @Override
    public int execute(int programCounter, Stack<Integer> stack,
    SymbolTable symbolTable) {

    if (!(val == null) && !(val.equals("")))
    {

    if (val.matches("-?\\d+")) //if the value is an integer
    {
    stack.push(Integer.parseInt(val));
    programCounter++;

    }
    else //if the value is not an integer
    {
    stack.push(symbolTable.getValue(val)); //use symboltable to check the value
    programCounter++;

    }

    }
    else
    {
    //throw new Exception();
    }

    return programCounter;
    }

    }

    public class SymbolTable
    {
    /**
    * Creates a SymbolTable.
    */
    String na;
    int val;
    public SymbolTable()
    {
    //na = "";
    //val = 0;
    }

    /**
    * Sets the value of the given variable.
    * @param name the name
    * @param value the value
    */
    public void setValue(String name, int value)
    {
    na = name;
    val = value;
    }

    /**
    * Returns the value of the given variable.
    *
    * @param name the name
    * @return the value
    * @throws RuntimeException if the variable is not defined
    */
    public int getValue(String name)
    {
    if (!(na.equals(name)))
    {
    throw new RuntimeException();
    }
    return val;
    }
    }


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    http://www.javaprogrammingforums.com...ease-read.html <--- please read on how to correctly post your code.

    Use "try catch" blocks to handle the exception.

    The assertion failed error comes from here; Assert (JUnit API)
    Quote Originally Posted by http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/Assert.html
    assertEquals

    public static void assertEquals(int expected,
    int actual)
    Asserts that two ints are equal.
    You initialise count to 0, then you never change that value. You assert that it is expected to be 1. The method is behaving correctly.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    Yes, I intialized count as 0, but I passed it into execute() from Push class as a programcounter, programcounter increments everytime stach push a value. But I don't know why count is not incremented after I push a value onto a stack...

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    Java is pass-by-value. You increment the variable in the execute method. This has zero effect on the count variable in the calling method.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    Hi Junky, could you explain more about this? I am still confused. Thanks!

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    class Foo {
        public static void main(String[] args) {
            int num = 10;
            doubleUp(num);
            System.out.println(num);
        }
     
        public stataic void doubleUp(int num) {
            num = num * 2;
        }
    }
    The output is 10. Even though the variable in the main method and doubleUp method are both called num they are not the same and have no relationship to each other. When the num variable in the doubleUp method is changed this has NO affect on the num variable in the main method.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    Thanks Junky! That explains it perfectly.
    How should I increment count in the right way? Could you give me a hint? Much appreciate it!

  8. #8
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    class Foo {
        public static void main(String[] args) {
            int num = 10;
            num = doubleUp(num);
            System.out.println(num);
        }
     
        public static int doubleUp(int num) {
            return = num * 2;
        }
    }

    Can you see the difference between this one and what Junky posted?

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: AssertionFailedError: Expected <1> but was <0>

    Why are you keeping count yourself? Why not use the size method of Stack?
    Improving the world one idiot at a time!

Similar Threads

  1. ']' expected
    By MagicMojoSoftware in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 15th, 2012, 10:42 AM
  2. '{' expected error please help
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 9th, 2012, 10:01 AM
  3. un-expected error
    By arvindbis in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2011, 09:02 AM
  4. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:33 PM
  5. ';' expected?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2009, 11:55 AM