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 8097415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:45:19+00:00 2026-06-05T21:45:19+00:00

I am very new to JUnit testing, I am trying to test the following

  • 0

I am very new to JUnit testing, I am trying to test the following very simple class

public interface IItemsService extends IService {

    public final String NAME = "IItemsService";


    /** Places items into the database
     * @return 
     * @throws ItemNotStoredException 
    */
    public boolean storeItem(Items items) throws ItemNotStoredException;




    /** Retrieves items from the database
     * 
     * @param category
     * @param amount
     * @param color
     * @param type
     * @return
     * @throws ItemNotFoundException 
     */
    public Items getItems (String category, float amount, String color, String type) throws ItemNotFoundException;

}

This is what I have for the test but I keep getting null pointer, and another error about it not being applicable for the argument… obviously I am doing something stupid but I am not seeing it. Could someone point me in the right direction?

public class ItemsServiceTest extends TestCase {



    /**
     * @throws java.lang.Exception
     */

    private Items items;

    private IItemsService itemSrvc;

    protected void setUp() throws Exception {
        super.setUp();

        items = new Items ("red", 15, "pens", "gel");

    }

    IItemsService itemsService;
    @Test
    public void testStore() throws ItemNotStoredException {
            try {
                Assert.assertTrue(itemSrvc.storeItem(items));
            } catch (ItemNotStoredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println ("Item not stored");
            }
    }

    @Test
    public void testGet() throws ItemNotStoredException {
            try {
                Assert.assertFalse(itemSrvc.getItems(getName(), 0, getName(), getName()));
            } catch (ItemNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
    }

}
  • 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-05T21:45:21+00:00Added an answer on June 5, 2026 at 9:45 pm

    You’re not creating an instance of the class under test, you’re only declaring it as the interface. In each of your tests you should create an instance of the class under test and test it’s implementation of the method. Note also, your tests should not be dependent on one another. You shouldn’t rely on them running in particular order; any set up for a test should be done in the test set up method, not by another test.

    Generally you want to use the AAA (Arrange, Act, Assert) pattern in your tests. The setUp (arrange) and tearDown (assert) can be part of this, but the pattern should also be reflected in each test method.

    @Test
    public void testStore() throws ItemNotStoredException {
        // Arrange
        ISomeDependency serviceDependency = // create a mock dependency
        IItemsService itemSvc = new ItemsService(someDependency);
    
        // Act
        bool result = itemSrvc.storeItem(items);
    
        // Assert
        Assert.assertTrue(result);
        // assert that your dependency was used properly if appropriate
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to persist a very simple class using Hibernate. I'm not using Spring,
I'm trying to fix our messy failing test runs, and, unfortunately, I'm very new
I am very new using JUnit. I want to test some classes that implements
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Very new mobile developer here... I am trying to retrieve a list of tweets
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
This is a very basic question for JUnit testing. I wrote a program which
I am testing a site written in Javascript, by writing (very simple) Java tests
I'm very new to JUnit, but I want to set up some tests which
I'm trying to compile my antlr grammar with gradle. I'm very new to gradle,

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.