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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:46:48+00:00 2026-05-16T16:46:48+00:00

I have the following JUnit test. The method that I’m testing is quite simple,

  • 0

I have the following JUnit test. The method that I’m testing is quite simple, it just receives a number and returns a List with its divisors. I don’t want to repeat the test code many times, so I’ve created an auxiliar method, testDivisorsAux:

@Test
public final void testDivisors() {
    testDivisorsAux(1, new int[] { 1 });
    testDivisorsAux(6, new int[] { 1, 2, 3, 6 });
    testDivisorsAux(0, new int[] { });
    ...
}

private final void testDivisorsAux(final int number, final int[] expected) {
    List<Integer> divisors = Util.divisors(number);
    assertSame(divisors.size(), expected.length);
    for (int i : expected) {
        assertTrue(divisors.contains(i));
    }
}

Everything works fine, I’m just wondering… is this a bad practice? Should I write the test in a different way? Maybe keep all the code within the “@Test method”?

PMD is telling me that JUnit tests should include assert() or fail() (for the first method), and JUnit 4 tests that execute tests should use the @Test annotation (for the second one). I know that PMD is using just a regular expression (well, actually it’s XPath) to determine which rules I’m breaking… so I’m inclined to think that it’s simply a “false positive” warning. But anyway, I would like to know if I’m doing something wrong. (Appart from writing tests 4 times longer than the method being tested 🙂

While I was searching for questions similar to this one, I’ve found something called parametrized tests… but it seems it’s something oriented to much bigger scenarios, isn’t it?

  • 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-16T16:46:48+00:00Added an answer on May 16, 2026 at 4:46 pm

    Personally I think it is good practise to do this. There is a small danger of going too far and building masses of code to do your test in which case you should rethink the design of the stuff you’re testing. You don’t really want to have to write tests to test your tests but the example you give seems fine and it means your test cases are a lot simpler.

    I have not used PMD and personally I would try to configure it to ingore these warnings. If they worry you you might be able to change your code to get rid of them. My guess is that the 2nd warning was because your helper method starts with the word test. In junit3 all methods that start with test are tests. Why not rename the method testDivisorsAux to be assertDivisors – maybe having a method that starts with assert will help with the 1st warning also.

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

Sidebar

Related Questions

I am new to junit testing and I have the following test - public
I have a junit test method that takes a CommonsMultipartFile object as a parameter.
I have a test class that I'm trying to run from a main method
I have the following 2 methods I'm testing from a DAO: public List<T> findAll()
One of the methods i have returns a double . When testing this method
I have the following JUnit test: @Test public void testRunLocalhost() throws IOException, InterruptedException {
I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make
I have following methods of my class Test : (Selenium RC, JUnit, Java, Eclipse)
I have a property file with the following junit.version=3.8.1 dbcp.version=5.5.27 oracle.jdbc.version=10.2.0.2.0 I try to
I have following script that executes all the .reg files in the current directory.

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.