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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:55:50+00:00 2026-06-04T10:55:50+00:00

I am creating a JUnit which is supposed to make each time an array

  • 0

I am creating a JUnit which is supposed to make each time an array of random size with random integers in it. Afterwards a copy of the array is made and the original array is inputed as a parameter in my mergesort array and gets sorted while the copy of the array gets sorted with the Arrays.sort() method. Then the two arrays are compared to see if they are the same.

Here is the code:

import java.util.Arrays;
import java.util.Random;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Before;
import org.junit.Test;

public class Mergesort_3wayTest {

    private int[] toSort;
    private static int SIZE;
    private static int MAX;
    private static int tests = 20000;

    @Before
    public void setUp() throws Exception {
        Random generator = new Random();
        SIZE = generator.nextInt(30) + 1;
        MAX = generator.nextInt(30) + 1;
        toSort = new int[SIZE];
        for (int i = 0; i < toSort.length; i++) {
            toSort[i] = generator.nextInt(MAX);
        }
    }

    @Test
    public void itWorksRepeatably() {
        for (int i = 0; i < tests; i++) {
            System.out.println("1 - " + Arrays.toString(toSort));
            int[] sorted = new int[toSort.length];
            System.arraycopy(toSort, 0, sorted, 0, toSort.length);
            Arrays.sort(sorted);

            Mergesort_3way.mergesort(toSort);

            System.out.println("2 - " + Arrays.toString(toSort));

            assertArrayEquals(toSort, sorted);
        }
    }
}

I could clearly see from the println‘s I inserted that only one array was created and the test was running constantly checking that one array but I thought it would create each time a random one which is clearly not the case.

So my question is why is only one array created which I guessed each time the assertArrayEquals(toSort, sorted) run it would the start over again.

I believe it has something to do with the global variables but I’m not quite sure…

  • 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-04T10:55:51+00:00Added an answer on June 4, 2026 at 10:55 am

    If you want this code to function the way you anticipate take the code in your setup function and make it into a new private method that is invoked at the start of each loop like so:

    private void magic()
    {
    Random generator = new Random();
            SIZE = generator.nextInt(30) + 1;
            MAX = generator.nextInt(30) + 1;
            toSort = new int[SIZE];
            for (int i = 0; i < toSort.length; i++) {
                toSort[i] = generator.nextInt(MAX);
            }
    
    }
    

    Followed by:

    @Test
        public void itWorksRepeatably() {
            for (int i = 0; i < tests; i++) {
               magic();  
               //rest of code  
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating Junit test cases in Spring. private static BeanFactory servicefactory = null;
I am creating a custom test runner for JUnit test cases using JUnitCore's run(junit.framework.Test
I'm often creating custom assertion methods for my JUnit tests. eg: public void assertArrays(String[]
Creating a simple RPG game, first time using XNA. Trying to get my character
I'm creating a TemporaryFolder using the @Rule annotation in JUnit 4.7. I've tried to
Creating an iOS app in Flash Builder 4.5 which is a small Hello World
So i am creating a class for MiniString which is just full of instance
I'm creating a test which will perform some stress testing of the app which
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a google map with store locations within 50 miles of user entered address.

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.