Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:21:57+00:00 2026-06-04T20:21:57+00:00

I am very new in Java Unit Testing and I heard that Mockito framework

  • 0

I am very new in Java Unit Testing and I heard that Mockito framework is really good for testing purposes.

I have developed a REST Server (CRUD methods) and now I want to test it, but I don’t know how?

Even more I don’t know how this testing procedure should begin. My server should work on localhost and then make calls on that url(e.g. localhost:8888)?

Here is what I tried so far, but I’m pretty sure that this isn’t the right way.

    @Test
    public void testInitialize() {
        RESTfulGeneric rest = mock(RESTfulGeneric.class);

        ResponseBuilder builder = Response.status(Response.Status.OK);

        builder = Response.status(Response.Status.OK).entity(
                "Your schema was succesfully created!");

        when(rest.initialize(DatabaseSchema)).thenReturn(builder.build());

        String result = rest.initialize(DatabaseSchema).getEntity().toString();

        System.out.println("Here: " + result);

        assertEquals("Your schema was succesfully created!", result);

    }

Here is the code for initialize method.

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/initialize")
    public Response initialize(String DatabaseSchema) {

        /** Set the LogLevel to Info, severe, warning and info will be written */
        LOGGER.setLevel(Level.INFO);

        ResponseBuilder builder = Response.status(Response.Status.OK);

        LOGGER.info("POST/initialize - Initialize the " + user.getUserEmail()
                + " namespace with a database schema.");

        /** Get a handle on the datastore itself */
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();


        datastore.put(dbSchema);

        builder = Response.status(Response.Status.OK).entity(
                "Your schema was succesfully created!");
        /** Send response */
        return builder.build();
    }

In this test case I want to send a Json string to the server(POST). If everything went well then the server should reply with “Your schema was succesfully created!”.

Can someone please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:21:59+00:00Added an answer on June 4, 2026 at 8:21 pm

    OK. So, the contract of the method is the following: Parse the input string as JSON, and send back BAD_REQUEST if it’s invalid. If it’s valid, create an entity in the datastore with various properties (you know them), and send back OK.

    And you need to verify that this contract is fulfilled by the method.

    Where does Mockito help here? Well, if you test this method without Mockito, you need a real DataStoreService, and you need to verify that the entity has been created correctly in this real DataStoreService. This is where your test is not a unit test anymore, and this is also where it’s too complex to test, too long, and too hard to run because it needs a complex environment.

    Mockito can help by mocking the dependency on the DataStoreService: you can create a mock of DataStoreService, and verify that this mock is indeed called with the appropriate entity argument when you call your initialize() method in your test.

    To do that, you need to be able to inject the DataStoreService into your object under test. It can be as easy as refactoring your object in the following way:

    public class MyRestService {
        private DataStoreService dataStoreService;
    
        // constructor used on the server
        public MyRestService() {
            this.dataStoreService = DatastoreServiceFactory.getDatastoreService();
        }
    
        // constructor used by the unit tests
        public MyRestService(DataStoreService dataStoreService) {
            this.dataStoreService = dataStoreService;
        }
    
        public Response initialize(String DatabaseSchema) {
             ...
             // use this.dataStoreService instead of datastore
        }
    }
    

    And now in your test method, you can do:

    @Test
    public void testInitializeWithGoodInput() {
        DataStoreService mockDataStoreService = mock(DataStoreService.class);
        MyRestService service = new MyRestService(mockDataStoreService);
        String goodInput = "...";
        Response response = service.initialize(goodInput);
        assertEquals(Response.Status.OK, response.getStatus());
    
        ArgumentCaptor<Entity> argument = ArgumentCaptor.forClass(Entity.class);
        verify(mock).put(argument.capture());
        assertEquals("the correct kind", argument.getValue().getKind());
        // ... other assertions
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really new to Java and I read that synchronized is very expensive
I am very new to Java generics and have spent an exorbitant amount of
Unit testing is new to me, and I have this error I don't understand.
I am very new to spring and java. I have been using mostly springsource.org
I'm using Grails 1.3.2 in NetBeans. I have a very simple unit test that
I am very new to JAVA. I have written simple program (in Linux -VIM
I am new to Java testing with JUnit. I have to work with Java
Very new to Java and Swing, and I have been playing with a swing
I am very new to glassfish, JPA and so on and I have really
I am a very new java user, and I have always had trouble with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.