2

I want to test my web service/web method not by using SOAP but with Junit or other framework. The first solution that I have founded is to create stub of my project and use a client to call this method; but what I want to do, is not to use a client, but directly call my webmethod inside my project; I am using maven and the project is on Jenkins, so with Jenkins I want to test my test method. I have try this to call a webmethod:

private Report reportBean = new Report();
@Mock
HttpSession mockedSession = mock(HttpSession.class);
@Before
public void injectMockEntityManager(){
    EntityManager entityManager = mock(EntityManager.class);
    reportBean.emReport = entityManager;
    Functions fnct= mock(Functions.class);
    reportBean.fnct=fnct;
}

@Test
public void testReport() throws WSException {
    reportBean.getReport("myReport", new Date(), new Date());
TimeUnit.SECONDS.toMillis(1));
}

The Report and Functions class is @Stateless, and in Report I use @EJB to call the functions class, what I have see is every time I call a method that is in a EJB like functions.getElement return null, so there is an alternative to call the web method without use a client like I have explained at the beginning or not ? thanks

  • Have you written unit tests? – Robbie Dee Apr 18 '17 at 11:48
  • no, integration test – Liz Lamperouge Apr 18 '17 at 12:34
  • Then how can you be sure the web methods even work unless they're a very thin layer? – Robbie Dee Apr 18 '17 at 12:38
  • Sorry @Robbie Dee I don't understand your question,what do you mean with " unless they're a very thin layer?" – Liz Lamperouge Apr 18 '17 at 12:44
  • You should really be writing and executing unit tests before integration testing. The exception might be where you have a very thin layer that itself calls another layer. By "thin", I mean that all that web method does is call a method in another layer. – Robbie Dee Apr 18 '17 at 12:58
1

You can test your "Report" by making it webservice independant through a java interface like this:

public interface ReportApi {
    void getReport(String templateName, Date orderDate, Date printDate);
}

// webservice independant implementation of ReportApi
public ReportImpl implements ReportApi {
    public void getReport(String templateName, Date orderDate, Date printDate) {
        // ... generate report
    }
}   

// thin WebService wrapper around some ReportApi implementation run on the webserver
public ReportWebService extends SomeWebServiceBaseClass implements ReportApi {
    private ReportApi reportImplementation;
    public ReportWebService (String serviceUrl, ReportApi reportImplementation) {
        ...
        this.reportImplementation = reportImplementation;
    }
    public void getReport(String templateName, Date orderDate, Date printDate)  {
        this.reportImplementation.getReport(templateName, orderDate, printDate);
    }
}

// thin wrapper for WebService-client used by the client to call the WebService functionality
public ReportWebServiceWrapper extends SomeWebServiceBaseClass implements ReportApi {
    ...
}

All your report consumer needs to know is the interface ReportApi.

For integration testing you can directly use new ReportImpl() instead of new ReportWebServiceWrapper().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.