You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Glen Mazza <gl...@gmail.com> on 2008/08/11 15:11:04 UTC

Need code check -- JUnit test cases using endpoint.publish()

Hello,

I'm trying to create a JUnit test case for my infamous "DoubleIt" web
service (http://www.jroller.com/gmazza/date/20080417), and would like to:

(1) create the endpoint, 
(2) make several test SOAP requests against it, and 
(3) close the endpoint.  

My test case is below--I want to make sure I'm doing this correctly (it
seems to work--and correctly returns errors when I intentionally place wrong
values in for the expected results), in particular the setUp() and
tearDown()--is it really as simple as I have it below?

Thanks,
Glen


package com.mycompany.webservice.service;

import static org.junit.Assert.assertEquals; 

import java.math.BigInteger;
import javax.xml.ws.Endpoint;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.mycompany.webservice.service.DoubleItPortTypeImpl;
import org.example.doubleit.DoubleItPortType;

public class DoubleItPortTypeImplTest {

    protected static Endpoint ep;
    protected static DoubleItPortTypeImpl port;
    
    @BeforeClass
    public static void setUp() throws Exception {
        port = new DoubleItPortTypeImpl();
        String address =
"http://localhost:8080/cxf/services/DoubleItPortType";
        Endpoint ep = Endpoint.publish(address, port);
    }

    @AfterClass
    public static void tearDown() {
        try {
            ep.stop();
        } catch (Throwable t) {
            System.out.println("Error thrown: " + t.getMessage());
        }
    }

    @Test
    public void doubleItWorksForPositiveNumbers() {      
        BigInteger resp = port.doubleIt(new BigInteger("10"));
        assertEquals("Double-It not doubling positive numbers", 
            resp.toString(), "20");
    }

    @Test
    public void doubleItWorksForNegativeNumbers() {
        BigInteger resp = port.doubleIt(new BigInteger("-10"));
        assertEquals("Double-It not doubling negative numbers", 
            resp.toString(), "-20");
    }   
}


-- 
View this message in context: http://www.nabble.com/Need-code-check----JUnit-test-cases-using-endpoint.publish%28%29-tp18925599p18925599.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Need code check -- JUnit test cases using endpoint.publish()

Posted by Glen Mazza <gl...@gmail.com>.

ianroberts wrote:
> 
>> Hello,
>> 
>> I'm trying to create a JUnit test case for my infamous "DoubleIt" web
>> service (http://www.jroller.com/gmazza/date/20080417), and would like to:
>> 
>> (1) create the endpoint, 
>> (2) make several test SOAP requests against it, and 
>> (3) close the endpoint.  
>> 
>> My test case is below--I want to make sure I'm doing this correctly (it
>> seems to work--and correctly returns errors when I intentionally place
>> wrong
>> values in for the expected results), in particular the setUp() and
>> tearDown()--is it really as simple as I have it below?
> 
> Your tests are just making normal Java method calls to the impl object, 
> they're not going through the web service stack at all.  If you really 
> want to test it properly you'd need to create a client proxy talking to 
> the same address as the Endpoint.publish, and make your test calls 
> through that proxy rather than straight to the impl.
> 
> Ian
> 

Thanks--that's something obvious I should have noticed.

Glen

-- 
View this message in context: http://www.nabble.com/Need-code-check----JUnit-test-cases-using-endpoint.publish%28%29-tp18925599p18936726.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Need code check -- JUnit test cases using endpoint.publish()

Posted by Ian Roberts <i....@dcs.shef.ac.uk>.
Glen Mazza wrote:
> Hello,
> 
> I'm trying to create a JUnit test case for my infamous "DoubleIt" web
> service (http://www.jroller.com/gmazza/date/20080417), and would like to:
> 
> (1) create the endpoint, 
> (2) make several test SOAP requests against it, and 
> (3) close the endpoint.  
> 
> My test case is below--I want to make sure I'm doing this correctly (it
> seems to work--and correctly returns errors when I intentionally place wrong
> values in for the expected results), in particular the setUp() and
> tearDown()--is it really as simple as I have it below?

Your tests are just making normal Java method calls to the impl object, 
they're not going through the web service stack at all.  If you really 
want to test it properly you'd need to create a client proxy talking to 
the same address as the Endpoint.publish, and make your test calls 
through that proxy rather than straight to the impl.

Ian

> Thanks,
> Glen
> 
> 
> package com.mycompany.webservice.service;
> 
> import static org.junit.Assert.assertEquals; 
> 
> import java.math.BigInteger;
> import javax.xml.ws.Endpoint;
> import org.junit.AfterClass;
> import org.junit.BeforeClass;
> import org.junit.Test;
> import com.mycompany.webservice.service.DoubleItPortTypeImpl;
> import org.example.doubleit.DoubleItPortType;
> 
> public class DoubleItPortTypeImplTest {
> 
>     protected static Endpoint ep;
>     protected static DoubleItPortTypeImpl port;
>     
>     @BeforeClass
>     public static void setUp() throws Exception {
>         port = new DoubleItPortTypeImpl();
>         String address =
> "http://localhost:8080/cxf/services/DoubleItPortType";
>         Endpoint ep = Endpoint.publish(address, port);
>     }
> 
>     @AfterClass
>     public static void tearDown() {
>         try {
>             ep.stop();
>         } catch (Throwable t) {
>             System.out.println("Error thrown: " + t.getMessage());
>         }
>     }
> 
>     @Test
>     public void doubleItWorksForPositiveNumbers() {      
>         BigInteger resp = port.doubleIt(new BigInteger("10"));
>         assertEquals("Double-It not doubling positive numbers", 
>             resp.toString(), "20");
>     }
> 
>     @Test
>     public void doubleItWorksForNegativeNumbers() {
>         BigInteger resp = port.doubleIt(new BigInteger("-10"));
>         assertEquals("Double-It not doubling negative numbers", 
>             resp.toString(), "-20");
>     }   
> }
> 
> 


-- 
Ian Roberts               | Department of Computer Science
i.roberts@dcs.shef.ac.uk  | University of Sheffield, UK