Solved

junit example issue

Posted on 2014-08-06
6
172 Views
Last Modified: 2014-08-17
package eePackage;

interface Appender
{
    /**
     * Appends an integer value to the end of the string.
     * @param value the integer value to be appended.
     * @return the string with the appended value.
     */
    String append(final Integer value);
}






package eePackage;

interface Calculator
{
    /**
     * Multiplies two integer values and returns the result.
     * @param a the first value.
     * @param b the second value.
     * @return the product of the multiplication.
     */
    Integer multiply(final int a, final int b);
}





package eePackage;

public interface Reverser
{
    /**
     * Reverses a string.
     * @param toBeReversed the string to be reversed.
     * @return the reversed string.
     */
    String reverse(String toBeReversed);
}





package eePackage;

public class Computer
{
    private Calculator calculator;
    private Appender appender;
    private Reverser reverser;
 
    /**
     * Constructor.
     * 
     * @param calculator the calculator class.
     * @param appender the appender class.
     * @param reverser the reverser class.
     */
    public Computer(final Calculator calculator, final Appender appender, final Reverser reverser)
    {
        this.calculator = calculator;
        this.appender = appender;
        this.reverser = reverser;
    }
    
    public String compute(final int a, final int b)
    {
        Integer product = calculator.multiply(a, b);
        return "";
    }
}






package eePackage;

import org.jmock.Expectations;
import org.jmock.Mockery;

import junit.framework.TestCase;

public class ComputerTest extends TestCase
{
    private Mockery mockery;
    private Calculator calculator;
    private Appender appender;
    private Reverser reverser;
    private Computer computer;
 
    /**
     * {@inheritDoc}
     */
    @Override
    public void setUp()
    {
        mockery = new Mockery();
        calculator = mockery.mock(Calculator.class);
        appender = mockery.mock(Appender.class);
        reverser = mockery.mock(Reverser.class);
 
        computer = new Computer(calculator, appender, reverser);
    }
 
    /**
     * Tests the compute method.
     */
    public void testCompute()
    {
        final Integer product = Integer.valueOf(200);
        final int a = 10, b = 20;
        final String someString = "a string ";
     
        mockery.checking(new Expectations(){
            {
                one(calculator).multiply(a, b);
                will(returnValue(product));
     
                one(appender).append(product);
                String appendedString = someString + product.toString();
                will(returnValue(appendedString));
     
                one(reverser).reverse(appendedString);
            }
     
        });
     
        computer.compute(a, b);
        mockery.assertIsSatisfied();
    }
}

Open in new window


I am trying above example on RAD IDE from link
http://thejavablog.wordpress.com/2008/05/27/testing-with-jmock/

I saw option to import junit3 automatically which i did.

I see below import not coming automatically and creating issues

import org.jmock.Mockery;

private Mockery mockery;


Please advise on how to fix it
0
Comment
Question by:gudii9
  • 3
  • 3
6 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 500 total points
ID: 40245531
Are you sure that you did this:
After downloading the zip files you will need to put the junit.jar, jmock-2.4.0.jar, and hamcrest-library-1.1.jar in your classpath.

What error message(s) do you get?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40247927
After downloading the zip files you will need to put the junit.jar, jmock-2.4.0.jar, and hamcrest-library-1.1.jar in your classpath.

Does RAD/eclipse do this automatically for us?
When it does automaticallly when we need to do this step manually.
Please advise
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 40248087
Please, don't forget to answer the questions experts trying to help are asking you.
>> What error messages do you get?



>> Does RAD/eclipse do this automatically for us?
What do you mean exactly? Automatically when you do what? Adding an import statement? Then the answer is: no.
Your IDE can't know what jar's you want to use. You have to tell it.
And you don't tell it by adding an import statement. If that is what you meant.
You have to add that jar as a dependency of your project.
I can't tell you how to do that in Eclipse since I'm using another IDE (IntelliJ's IDEA).
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 37

Expert Comment

by:zzynx
ID: 40248102
0
 
LVL 7

Author Comment

by:gudii9
ID: 40250186
I thought IDEs like eclipse and RAD have default support for junit without necessity of adding copying the junit jars to project lib folder then going to build path and setting the build path with those libraries?

Compilation errors i saw earlier infront of below lines(i see red mark before them )

import org.jmock.Mockery;

private Mockery mockery;



Now those errors are not there which might have been gone as I set the classpath.
JunitJARS.jpg
0
 
LVL 7

Author Comment

by:gudii9
ID: 40253795
please advise
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 113
tomcat startup error 5 107
CSV file parsing thru Java 13 34
IBM TS2900 (3572) Tape Autoloader Java? 12 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question