I know that Eclipse has powerful debugging capabilities. Are there any hooks that allow plug-ins to keep track of the code path followed when an open project is run?
For example, suppose I had the following program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
if(in.nextInt() == 1) {
num += 2;
} else {
num += 3;
}
System.out.println(num);
}
}
Is there some API that Eclipse exposes that would let me make a plug-in that determines which branch of the if statement this program took after it's executed once?
You might be interested in looking into code coverage. It's a metric used to determine what parts of your code have been run after execution. Typically, it's used for testing purposes, to see what parts of your code aren't run.
That said, you could use it to determine what branch was chosen by only running the program once. EclEmma, an eclipse plugin, will show you what branches were used by coloring the lines on the editor itself.
Related
I'd like to list what classes are touched during the execution of each unit test. I want to discover which tests have an overly large scope and should use a smaller unit instead. Measuring the code coverage via IntelliJ or JaCoCo doesn't help me as I cannot drill down to a single test. Has anyone managed to do something similar? I found a similar question which was asked ten years ago, but never answered. I currently use Java 8 with Gradle and TestNG. I feel like I have to build some Gradle plugin or modify TestNG in some way, but I have no idea where to start.
Any help is greatly appreciated.
OK, I've discovered that Openclover can do this. I've setup a dummy maven project and when I execute the tests via this command chain:
mvn clean clover:setup test clover:aggregate clover:clover
Then clover generates a report in the "target\site\clover" subdirectory of my project. In that directory is an index.html which you can use to browse through the results. You need to navigate to a test and to a certain test method to see the breakdown. It looks like this:
We can see that the method testCalculateBalance of BalanceCalculatorTest covers BalanceCalculator, PriorServiceBalanceProvider and TimeAccount, but we don't see TimeAccountProvider, because it is mocked in the test. I'll paste the code snippets, so you get the full picture. Bottom line is, it works, but it is not very user friendly.
public class BalanceCalculatorTest {
#Test
public void testCalculateBalance() {
TimeAccountProvider timeAccountProvider = Mockito.mock(TimeAccountProvider.class);
Mockito.doReturn(Collections.singleton(new TimeAccount())).when(timeAccountProvider).getTimeAccounts();
BalanceCalculator balanceCalculator = new BalanceCalculator(timeAccountProvider);
Assert.assertEquals(balanceCalculator.calculateBalance(), 2);
}
}
public class BalanceCalculator {
private final TimeAccountProvider timeAccountProvider;
public BalanceCalculator(TimeAccountProvider timeAccountProvider) {
this.timeAccountProvider = timeAccountProvider;
}
public int calculateBalance() {
Set<TimeAccount> timeAccounts = timeAccountProvider.getTimeAccounts();
int sum = 0;
for (TimeAccount timeAccount : timeAccounts) {
Set<TimeAccountDetail> bookings = timeAccount.getTimeAccountDetails();
sum += bookings.stream()
.mapToInt(TimeAccountDetail::getAmount)
.sum();
}
sum += new PriorServiceBalanceProvider().getPriorBalance();
return sum;
}
}
I am new to java and I know this isn't for homework but I need help. I've typed a program in java and I need to have the values passed in the command line arguments and that is where I am getting stuck I quite understand how to do that. Here is the question that I needed to answer.
(Write an application that uses an enhanced for a statement to sum the double values passed by the command-line arguments. [Hint: Use the static method parseDouble of class Double to convert a String to a double value .])
here is my code
public class Enhanced {
public static void main(String[] args) {
Double total = 0.0;
for (String number : args) {
total += Double.parseDouble(number);
}
//print total
System.out.printf("Total of array elements: %f%n", total);
}
}
First thing you would need to compile your code using below command.
javac Enhanced.java
It will compile your program and prepare a class file for it, then you need to run that class as specified in other answers.
java Enhanced 3.2 2.12 5.83
Here's how to specify command-line arguments when running your app under NetBeans:
Under your Projects tab, right-click on your project and choose Properties
In the Project Properties dialog, under Categories, select Run;
Enter your command-line arguments in the Arguments field;
Click OK;
The arguments you entered will be used every time you run the project after that.
sorry if this question was repeated earlier, but I couldnt seem to find the answer. I have a code:
import java.util.Scanner;
public class PracticeProblems {
public static void main(String[] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
x= input.nextInt();
if(x%2 == 0) {
System.out.println(x%2==0);
}
else {
System.out.println(x%2==0);
}
}
}
Now I am not too worried if the code actually works or not (because this same problem has occurred when the code was perfectly functional) I am just as confused why it doesnt run.
Try running by right clicking on PracticeProblems.java, and choosing "Run As Application". It looks like it is trying to run ComputeArea, and can't find that class.
I had a similar problems too.
Eclipse > Projects > Clean
if its not working try to copy the class. Delete the Project and recreate it
I have faced similar problems many times.
To solve this problem save the program, close Eclipse, reopen it and run the program without any loss of the program.
It works fine.
I am fairly new java and programming as a whole. Currently i follow a guide to java programming but a few of the programs don't execute accordingly in eclipse mars but execute without problems on ideone.com. An example:
class Vehicle{
int Passengers, mpg, Fuelcap, Size;
boolean running, full, Fueltankempty;
void range(){
System.out.println("Range is " + Fuelcap*mpg);
}
}
class AddMeth{
public static void main (String args[]){
Vehicle minivan = new Vehicle();
Vehicle sportscar =new Vehicle();
minivan.Passengers=7;
minivan.Fuelcap=16;
minivan.mpg=21;
sportscar.Passengers=2;
sportscar.Fuelcap=14;
sportscar.mpg=12;
System.out.println("minivan can carry "+minivan.Passengers+" with a range of " );
minivan.range();
System.out.println("sportscar can carry "+sportscar.Passengers+" with a range of ");
sportscar.range();
}
}
When executing I get the following error message:
minivan can carry 7 with a range of
Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:26)
Does anybody know why I get the message?
You have to recompile your code, each time you made a change to your source code and want to run your program. If you are usingan IDE like Eclipse or Netbeans, it will (by default) automatically build/recompile.
In Eclipse, check, whether you have activated build automatically, which is in the Project menu.
Here is my code
import java.util.Scanner;
public class Range {
public static void main(String[] args)
{
System.out.println("Greetings.");
int min,max;
System.out.println("Enter a minimum and maximum value.");
Scanner keyboard = new Scanner(System.in);
min = keyboard.nextInt();
max = keyboard.nextInt();
System.out.println("The number of values in the range from " + min + "to " + max + " is");
for (int i = min; i <= max; i++){
System.out.println(i);
}
}
}
Exception:
run:
Error: Could not find or load main class project.Project
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
could someone tell me what I am doing wrong?
That's not a compiler error - that's when you're trying to run the code.
And the answer is simple - you're trying to run project.Project, but your class is actually just Range (in the default package, by the looks of it).
You are told that Java Runtime can't find the main class to run the app, and it looks like it's set to project.Project whereas it should be set to Range since that's the full name of the class containing your main method.
Right click the project, select Properties, then go to the Run tab. Then set Main Class to Range. Assuming you're launching the correct project, you'll be fine. If not, then check if it is set as main project (right click it and select the corresponding menu item) or right click it and select Run to launch it.
Set your main project. This is not a compile error.
You need to learn using NetBeans, the different steps to write, compile and run the programs. Once you know all this such errors will never bug you. Here is the tutorial to help you with that:
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html