简体   繁体   中英

Error: Could not find or load main class junit_runner.JgrRunner

I'm trying to run a JUnit test on some of my files. I'm pretty sure that I have JUnit installed and configured, but something in the configuration is incorrect. I think it has to do with the fact that I'm using a machine running Ubuntu. Here's my main file:

import java.util.ArrayList;

public class ArrayListProblems {


/**
 * reverses the ArrayList in place. What that means is that you do not create a new
 * ArrayList pointer. Everything happens in the al variable
 * So if al starts as {1, 2, 3, 4, 5} when its finished the same memory location
 * holds {5, 4, 3, 2, 1}
 * @param al  the ArrayList to reverse
 */
public static void reverseArrayList(ArrayList<Integer> al)
{
 for(int i = al.size() - 1; i >= 0; i--)
 {
 al.add(al.remove(i));
 }  
}

/**
 * finds and returns the largest integer in the ArrayList al
 * @param al the ArrayList to search for the largest element
 * @return the value of te largest element. If no elements then return Integer.MIN_VALUE.
 */
public static int maximumElement(ArrayList<Integer> al)
{
 int max = al.get(0);
  for(int i = 1; i < al.size(); i++)
  {
   max = Math.max(max, al.get(i));
  }
  return max;
}

/**
 * In place changes all elements of the ArrayList to their absolute value
 * so {1,-2,3,-4} become {1,2,3,4}
 * @param al the ArrayList to make he changes in.
 */
public static void changeAllNegatives(ArrayList<Integer> al)
{
 for(int i = 0; i < al.size(); i++)
 {
 al.set(i, Math.abs(al.get(i)));
 }  
}

/**
 * Creates and returns an ArrayList with all the elements of the original ArrayList al that begin with the
 * begin substring.
 * So ({"bob","boob","ralph", "bop", "bing"}, "bo") would return the ArrayList {"bob","boob","bop"}
 * @param al The ArrayList to search
 * @param begin the substring to check the beginning
 * @return a new ArrayList containing only the elements that begin with begin
 */
public static ArrayList<String> getAllFirstLetters(ArrayList<String> al, String begin)
{
    ArrayList<String> list = new ArrayList<String>();
  for(int i = 0; i < al.size(); i++)
  {
   if(al.get(i).length() >= begin.length() && al.get(i).substring(0, begin.length()).equals(begin))
   {
    list.add(al.get(i));
   }
  }
  return list;
}

/**
 * returns the mode of the ArrayList. Note these are Strings not integers. 
 * Note that the mode is simply the element that appears most often. 
 * There are multiple ways to solve this problem.
 * @param al  The ArrayList to search
 * @return the element that appears most often in al
 */
public static String findMode(ArrayList<String> al)
{
 String currentMode = null;
 int currentModeCounter = 0;
 for(int i = 0; i < al.size(); i++)
 {
  int modeCounter = 0;
  String compare = al.get(i);
  for(int j = 0; j < al.size(); j++)
  {
   if(al.get(j).equals(compare))
   {
    modeCounter++;
   } 
  }
  if(modeCounter > currentModeCounter)
  {
   currentMode = compare;
   currentModeCounter = modeCounter;
  } 
 }
return currentMode;
}

public static void main (String args[])
 {
ArrayList<String> a1 = new ArrayList<String>();
a1.add("bob");
a1.add("boob");
a1.add("bob");
a1.add("bop");
a1.add("bing");
ArrayList<String> a2 = getAllFirstLetters(a1, "bo");
System.out.println(findMode(a1));
 }
}

Here is the test file:

 import java.util.ArrayList;
 import junit.framework.TestCase;
 import static org.junit.Assert.*;

 import org.junit.Test;

 public class ArrayListProblemsTest{

@Test
public void reverseArrayListTest1() {
    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(5);
    al.add(2);
    al.add(4);
    al.add(10);
    al.add(21);
    al.add(12);
    al.add(13);
    al.add(15);
    al.add(-1);
    al.add(6);
    ArrayListProblems.reverseArrayList(al);
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    al2.add(6);
    al2.add(-1);
    al2.add(15);
    al2.add(13);
    al2.add(12);
    al2.add(21);
    al2.add(10);
    al2.add(4);
    al2.add(2);
    al2.add(5);
    assertEquals(al2, al);

}


@Test
public void reverseArrayListTest2() {
    ArrayList<Integer> al = new ArrayList<Integer>();

    al.add(2);
    al.add(4);
    al.add(10);
    al.add(21);
    al.add(12);
    al.add(13);
    al.add(15);
    al.add(-1);
    al.add(6);
    ArrayListProblems.reverseArrayList(al);
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    al2.add(6);
    al2.add(-1);
    al2.add(15);
    al2.add(13);
    al2.add(12);
    al2.add(21);
    al2.add(10);
    al2.add(4);
    al2.add(2);
    assertEquals(al2, al);

}

@Test
public void reverseArrayListTest3(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    assertEquals(al,al2);
}

@Test
public void maximumElementTest1(){
    ArrayList<Integer> al = new ArrayList<Integer>();

    al.add(2);
    al.add(4);
    al.add(10);
    al.add(21);
    al.add(12);
    al.add(13);
    al.add(15);
    al.add(-1);
    al.add(6);
    int max = ArrayListProblems.maximumElement(al);
    assertEquals(21, max);
}
@Test
public void maximumElementTest2(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    int max = ArrayListProblems.maximumElement(al);
    assertEquals(Integer.MIN_VALUE, max);
}

@Test
public void maximumElementTest3(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(-2);
    al.add(-4);
    al.add(-10);
    al.add(-21);
    al.add(-12);
    al.add(-13);
    al.add(-15);
    al.add(-1);
    al.add(-6);
    int max = ArrayListProblems.maximumElement(al);
    assertEquals(-1, max);
}

@Test
public void changeAllNegativesTest1(){
    ArrayList<Integer> al = new ArrayList<Integer>();

    al.add(2);
    al.add(4);
    al.add(-10);
    al.add(21);
    al.add(-12);
    al.add(13);
    al.add(15);
    al.add(-1);
    al.add(6);
    ArrayListProblems.changeAllNegatives(al);
    ArrayList<Integer> al2 = new ArrayList<Integer>();

    al2.add(2);
    al2.add(4);
    al2.add(10);
    al2.add(21);
    al2.add(12);
    al2.add(13);
    al2.add(15);
    al2.add(1);
    al2.add(6);
    assertEquals(al2,al);
}

@Test
public void changeAllNegativesTest2(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(-2);
    al.add(-4);
    al.add(-10);
    al.add(-21);
    al.add(-12);
    al.add(-13);
    al.add(-15);
    al.add(-1);
    al.add(-6);
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    al2.add(2);
    al2.add(4);
    al2.add(10);
    al2.add(21);
    al2.add(12);
    al2.add(13);
    al2.add(15);
    al2.add(1);
    al2.add(6);
    ArrayListProblems.changeAllNegatives(al);

    assertEquals(al2,al);
}

@Test
public void changeAllNegativesTest3(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    ArrayListProblems.changeAllNegatives(al);
    assertEquals(al2,al);   
}

@Test
public void changeAllNegativesTest4(){
    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(5);
    al.add(2);
    al.add(4);
    al.add(10);
    al.add(21);
    al.add(12);
    al.add(13);
    al.add(15);
    ArrayListProblems.changeAllNegatives(al);
    ArrayList<Integer> al2 = new ArrayList<Integer>();
    al2.add(5);
    al2.add(2);
    al2.add(4);
    al2.add(10);
    al2.add(21);
    al2.add(12);
    al2.add(13);
    al2.add(15);
    assertEquals(al2, al);
}

@Test
public void getAllFirstLettersTest1(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("bob");
    al.add("boob");
    al.add("ralph");
    al.add("bop");
    al.add("bing");
    ArrayList <String> al3 = ArrayListProblems.getAllFirstLetters(al, "bo");
    ArrayList<String> al2 = new ArrayList<String>();
    al2.add("bob");
    al2.add("boob");
    al2.add("bop");
    assertEquals(al2,al3);
}

@Test
public void getAllFirstLettersTest2(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("bob");
    al.add("boob");
    al.add("ralph");
    al.add("bop");
    al.add("bing");
    ArrayList<String> al3 = ArrayListProblems.getAllFirstLetters(al, "b");
    ArrayList<String> al2 = new ArrayList<String>();
    al2.add("bob");
    al2.add("boob");
    al2.add("bop");
    al2.add("bing");

    assertEquals(al2,al3);

}

@Test
public void getAllFirstLettersTest3(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("bob");
    al.add("boob");
    al.add("ralph");
    al.add("bop");
    al.add("bing");
    ArrayList<String> al3 = ArrayListProblems.getAllFirstLetters(al, "ra");
    ArrayList<String> al2 = new ArrayList<String>();
    al2.add("ralph");
    assertEquals(al2,al3);

}
@Test
public void getAllFirstLettersTest4(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("bob");
    al.add("boob");
    al.add("ralph");
    al.add("bop");
    al.add("bing");
    ArrayList <String> al3 = ArrayListProblems.getAllFirstLetters(al, "sam");
    ArrayList<String> al2 = new ArrayList<String>();
    assertEquals(al2,al3);
}

@Test
public void findModeTest1(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("Sam");
    al.add("Ralph");
    al.add("Sam");
    al.add("Bob");
    al.add("Nikki");
    al.add("Bob");
    al.add("Nikki");
    al.add("Sam");
    al.add("Frank");
    String mode = ArrayListProblems.findMode(al);
    assertEquals("Sam",mode);
}
@Test
public void findModeTest2(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("Ralph");
    al.add("Bob");
    al.add("Nikki");
    al.add("Bob");
    al.add("Nikki");
    al.add("Sam");
    al.add("Sam");
    al.add("Sam");
    al.add("Frank");
    String mode = ArrayListProblems.findMode(al);
    assertEquals("Sam",mode);
}

@Test
public void findModeTest3(){
    ArrayList<String> al = new ArrayList<String>();
    al.add("Ralph");
    al.add("Bob");
    al.add("Nikki");
    al.add("Bob");
    al.add("Nikki");
    al.add("Sam");
    al.add("Sam");
    al.add("Sam");
    al.add("Frank");
    al.add("Bob");
    al.add("Bob");
    String mode = ArrayListProblems.findMode(al);
    assertEquals("Bob",mode);
}

@Test
public void findModeTest4(){
    ArrayList<String> al = new ArrayList<String>();
    String mode = ArrayListProblems.findMode(al);
    assertNull(mode);
 }
}

When I hit compile and run using JUnit Tests, it says Error: Could not find or load main class junit_runner.JgrRunner, and after a couple seconds, it says Unable to connect to JUnit Runner after 8 seconds.

Appreciate any help

To use JUnit in jGRASP with a single source file, with the source file in focus hit the "Create JUnit Test File" icon (red/white/green gradient file icon on toolbar). If that icon isn't there, use "Tools" > "JUnit" > "Configure" to tell jGRASP where JUnit is installed. For a single file (no project), your test class must be named the same as the main class with "Test" added, so for class "MyClass" it would be "MyClassTest". If you already have that file, "Create JUnit Test File" will ask if you want to use the existing file. Once the file is created, a JUnit run icon will show up on the toolbar (red/white/green gradient running man icon) and you use that to compile and run the tests.

If you do have a project in jGRASP, add the test files to the project as test files, not source files. If they are already added as source, you can drag them to "Test" in the projects pane, or right click and select "Mark as Test". With a project, jGRASP will keep track of which tests need to be re-run (if source has been modified) and whether the last run was successful, and this will be shown in the project pane. Test files in projects can have any names, but those identical to classes in the project with "Test" appended will be assumed to be associated with those source files, and the results will be shown for both the test file in the project pane and the associated source, which can be handy.

If you're doing all that and still have the problem, use "Help" > "Report a Bug" to let the developers (me) know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM