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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:40:24+00:00 2026-05-23T16:40:24+00:00

You can use e.g. JUnit to test the functionality of your library, but how

  • 0

You can use e.g. JUnit to test the functionality of your library, but how do you test its type-safetiness with regards to generics and wildcards?

Only testing against codes that compile is a “happy path” testing; shouldn’t you also test your API against non-type-safe usage and confirm that those codes do NOT compile?

   // how do you write and verify these kinds of "tests"?

    List<Number> numbers = new ArrayList<Number>();
    List<Object> objects = new ArrayList<Object>();

    objects.addAll(numbers); // expect: this compiles

    numbers.addAll(objects); // expect: this does not compile

So how do you verify that your genericized API raises the proper errors at compile time? Do you just build a suite a non-compiling code to test your library against, and consider a compilation error as a test success and vice versa? (Of course you have to confirm that the errors are generics-related).

Are there frameworks that facilitate such testing?

  • 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-05-23T16:40:25+00:00Added an answer on May 23, 2026 at 4:40 pm

    Since this is not testing in the traditional sense (that is – you can’t “run” the test), and I don’t think such a tool exists, here’s what I can suggest:

    1. Make a regular unit-test
    2. Generate code in it – both the right code and the wrong code
    3. Use the Java compiler API to try to compile it and inspect the result

    You can make an easy-to-use wrapper for that functionality and contribute it for anyone with your requirements.

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

Sidebar

Related Questions

How can I use JUnit idiomatically to test that some code throws an exception?
You can use App.config; but it only supports key/value pairs. You can use .Net
I use new Eclipse. Create demo test with jUnit (I added default jUnit library
In JUnit you can use @Ignore before methods to tell the test runner to
How can I use the Groovy Console to kick off junit tests? (Currently using
I can use FlashWindowEx to make a window flash in the taskbar, but what
How can I mark a test as an expected failure in JUnit 4? In
I'm using IntelliJ-Idea to write Java daemons. We use JUnit to unit test our
We are using org.mule.tck.FunctionalTestCase for test cases. Its an abstract JUnit test case. This
I'm attempting to use JUnit to test some database code. I'm pretty new to

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.