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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:39:46+00:00 2026-06-01T16:39:46+00:00

I have a method add which adds a generic object (i.e. T) into an

  • 0

I have a method add which adds a generic object (i.e. T) into an List. I want to unit test this method.

public static <T> List<T> add(List<T> list, T t) {
    if(list == null) return null;
    if(t == null) return list;

    List<T> outputList = new ArrayList<T>(list);
    outputList.add(t); // append item into list

    return outputList;
}

I am trying to get the Method and test it. I have written following code snippet to obtain the method (which i know is not correct).

Class myClass = Myclass.class;
Class<List<T>> clazz = (Class<List<T>>) Class.forName("java.util.ArrayList");
Method myObjectMethod = myClass.getMethod("add", ...) // i need to identify T's class

Certain trail-error attempts lead me to NoSuchMethod exceptions.

Can anybody help me in identifying this method? any pointers to rectify the above code?

Thanks!

  • 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-01T16:39:47+00:00Added an answer on June 1, 2026 at 4:39 pm

    clazz should probably be List.class, not ArrayList.class. You have to use the exact raw type used in the method definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In asp.net mvc3 I want to have a generic method which adds a viewmodel
i have written a method in java like this public boolean ADD(String ID,String Name,String
So, I have implemented this method to add a footer to my table view:
Let's say I have a simple ASP MVC3 list controller, with an add method,
Greetings, I have a method to capture packets. After capturing I want to add
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
I have a large static list which is basically a lookup table, so I
I have a method which adds a new vehicle(Honda) to the vehicles array. The
I have the following method which adds a new column to a Telerik RadGridView:
So I have a method in a reservation model called add_equip. This method does

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.