0

I'm quite new to Java and I'm working on a project that has a web-based UI and it then makes various calls to Java code. The calls to the code seem to be done via URL's and Spring @RequestMapping similar to this:

public class myClass()
{
    @RequestMapping(value="aUrlHere/SomeOtherPieceofUrl", method.request.GET)
    Public ResponseEntity doStuff()
    {
        //Code here that does stuff
        return;
    }
}

The problem is that I've never worked on @RequestMapping or ResponseEntity things and I don't really understand how it works. I think what happens is when the appropriate URL is navigated to in the UI, the corresponding method is called from the code, and the @RequestMapping is used to map those URL calls to the various methods.

What I would like to be able to do is call these methods without having to use the web-based UI, e.g. like in the form of some sort of unit test.

Can I call these methods using something like this?

myCLass testClass = new myClass();
testClass.doStuff();

I think I tried something like the above but it didn't work. However I don't really know how to use these methods. I didn't write the original code and the person who did is no longer available to speak to.

I tried using something like a HttpWebRequest in Java to just make a call to the URL but I was running into issues with security certificate exceptions. Normally you would have to be logged in via the web UI to use this thing and due to the security setup, it seems to be hard to navigate around.

I was just wondering if I could write some sort of unit test to fire off those methods and get a response?

Thanks.

irldev
  • 409
  • 2
  • 8
  • 21
  • 1
    You can simply call the method from a unit test, but usually there are some parameters that are passed in that would require some kind of mocking framework. Without you being able to provide an example that does not seem to work, the question is just to broad to answer. – hotzst Dec 05 '15 at 14:55
  • 1
    If you want to test your web layer, you can simulate running your code in a servlet container using [Spring MVC Test Framework](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-framework). Doing "plain" JUnit tests will exercise your code as "plain" Java, ignoring all Spring annotations etc. – kryger Dec 05 '15 at 14:58
  • Thanks, I will look into that although I'm not really sure what it does. I could use a certain amount of mocking however I need to be able to get back data from the real systems so I guess the mocking will be limited. – irldev Dec 05 '15 at 15:49
  • “The problem is that I've never worked on @RequestMapping or ResponseEntity things and I don't really understand how it works.” – I’d recommend you to read [the `@RequestMapping` docs](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html); I think they are crucial to understand what’s going on. – Chriki Dec 05 '15 at 16:01

0 Answers0