I'm working on a Java Hands-On Exercise, And I ran into this problem where for some reason, It gives me this error whenever I try to call the ItemTaxCalculator class on my Java Test File
Here is the ItemTaxCalculator class
package com.mycompany.taxcalculator;
public class ItemTaxCalculator {
public double CalculateItemTax(double itemPrice, double taxPercent){
double decrease = taxPercent/10.0;
return itemPrice * decrease;
}
}
And this is the ItemTaxCalculatorTest.java
package taxcalculator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ItemTaxCalculatorTest {
public ItemTaxCalculatorTest() {
}
#Test
public void testCalculateItemTax(){
System.out.println("CalculateItemTax method");
ItemTaxCalculator instance = new ItemTaxCalculator();
double itemPrice = 50.0;
double taxPercent = 1.0;
double expectedResult = 5.0;
double result = instance.CalculateItemTax(itemPrice, taxPercent);
assertEquals(expectedResult, result, 0.0);
}
}
The error occurs on the
ItemTaxCalculator instance - new ItemTaxCalculator(); line.
An error message comes up saying:
cannot find symbol
symbol: class ItemTaxCalculator
location: class ItemTaxCalculatorTest
I can't seem to find an answer for this problem, And this is the only thing that hinders me from running the code. I hope I can find some help, cheers!
Your problem is not netbeans related.
Your original class is in package:
package com.mycompany.taxcalculator;
while your test is in package:
package taxcalculator;
The test would be able to compile and execute as is, but only if both classes were in the same package.
So, there are two ways you can fix your issue.
Put your classes in the same package
Just change one of your package statements so that it matches the other one.
Add an import statement in your test class
import com.mycompany.taxcalculator.ItemTaxCalculator;
Once you do this, the class will be found and can be instantiated.
Related
I have the following Junit test class
package Md5Tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import timebackup.*;
/**
*
* #author jack
*/
public class PathNodeToXmlTest {
public PathNodeToXmlTest() {
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
#Test
public void createXml() {
PathNode root = new PathNode(true, "docs", "0");
root.addPathNode(new PathNode(true, "docs sub folder", "1"));
root.addPathNode(new PathNode(false, "docs sub file", "2"));
PathNodeToXml xmlCreator = new PathNodeToXml(root);
System.out.println(xmlCreator);
}
}
I am now trying to temporarily stop the test createXml() from being run by the test runner. I have tried adding the #Ignore annotation right before the #Test annotation however the compiler throws an error saying that it can't find the symbol for #Ignore. I am using the NetBeans Ide.
Anyone have any ideas either how to prevent the compile error, or another way to temporarily prevent a JUnit test from running?
#Ignore annotation is in a package org.junit so you need to add import statement
import org.junit.Ignore;
or
import org.junit.*;
Next time you have a problem like this you can just google class name (e.g. junit #Ignore) go to the documentation page and check package name.
In NetBeans you can use "Source -> Fix Imports" command (Ctrl + Shift + I) and IDE will try to resolve necessary imports automatically.
I'm new to java, and I've been trying to get my program to compile using Netbeans. HelloWorldApp.java uses the Greeter class in Greeter.java. I keep getting errors and I can't figure it out. I understand that you have to include "packages" or something. I don't have a lot of experience with Netbeans either. But I would love for this to work.
Here is the HelloWorldApp.java:
package helloworldapp;
import Greeter
public class HelloWorldApp
{
public static void main(String[] args)
{
Greeter myGreeterObject = new Greeter();
myGreeterObject.sayHello();
}
}
And here is Greeter.java:
public class Greeter
{
public void sayHello()
{
System.out.println("Hello, World!");
}
}
Change the first line of Greeter to
package helloworldapp;
And then remove
import Greeter
from HelloWorldApp. You only need to import classes that are in other packages. Also, an import line is terminated with a semicolon. Finally, import is always optional and a convenience for the developer; as an example,
import java.util.Calendar;
Allows you to write
Calendar cal = Calendar.getInstance();
But, without the import you could still use
java.util.Calendar cal = java.util.Calendar.getInstance();
Just put the Greeter class in the same folder (i.e. package) as the other file and remove the "import Greeter" statement. You should put every class in a package as you did with the HelloWorldApp class.
If you leave classes without package (i.e. in the root folder) you cannot import them.
As long as both are in the same package (folder) there will be no need for the "import Greeter" statement, this should fix it, hope this helps!
I am new to JUnit.
I just started working on JUnit and i am getting following error.
The method assertEquals(String, String) is undefined for the type TestJunit
and my Javacode:
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class TestJunit {
String message = "Hello World";
MessageUtil messageutil = new MessageUtil(message);
public void testPrintMessage()
{
assertEquals(message,messageutil.printMessage());
}
}
please help me resolve this issue.
You imported
import static org.junit.Assert.assertArrayEquals;
but not
import static org.junit.Assert.assertEquals;
You could also import every static member of Assert with
import static org.junit.Assert.*;
Without these, Java thinks you are calling the method assertEquals defined in the class the code is declared in. Such a method does not exist.
if importing doesn't work try saving before you run the code
I am trying out this code from javapractices dot com and Im seeing a type resolve issue :
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
TextTransfer textTransfer = new TextTransfer(); <- ERROR
// Do other stuff
}
I see the following error. How do I resolve this? Im using Eclipse with Java 1.7.0_45
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
TextTransfer cannot be resolved to a type
TextTransfer cannot be resolved to a type
at JP_Clipboard.main(JP_Clipboard.java:20)
In the example the class name is called TextTransfer. You have renamed it to JP_Clipboard. Either use
JP_Clipboard textTransfer = new JP_Clipboard();
or rename the class back to TextTransfer (preferrable - follows Java naming conventions)
I checked your reference.. You have renamed the TextTransfer class by JP_Clipboard in your case
So your code should be:
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
JP_Clipboard textTransfer = new JP_Clipboard();
}
The issue is with your example Class name JP_Clipboard - this should be renamed to TextTransfer; referring to link you have provided in your post.
You need import for TextTransfer or it must be in same package.
I've two classes under the same package Class names are "TestPlugin" and "Pokemon". The error I get is in the class TestPlugin at line 7 where there's written "New Pokemon". The error is "Cannot be resolved to a variable". I want the TestPlugin to acces the code in Pokemon so it can be used. What should I do to fix this problem? New to bukkit plugin creation so don't make the answer too advanced please. "I don't own this code/plugin. I've it for educational purposes only!". If you wonder what bukkit libary I'm using, it's the recommended build "craftbukkit-1.6.4-R2.0".
TestPlugin's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin{
public void onEnable(){
getServer().getPluginManager().registerEvents(new Pokemon, this);
}
}
Pokemon's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Pokemon implements Listener{
#EventHandler
public void onBlockPlace(BlockPlaceEvent event){
Player p = event.getPlayer();
Block bp = event.getBlockPlaced();
p.sendMessage("You've placed a " + bp.getType().toString());
}
}
Change line 7 to
this.getServer().getPluginManager().registerEvents(new Pokemon(this), this);
also think about adding a on disable
You wanted to call Pokemon's constructor, so use
new Pokemon() with the parentheses.
As #Welsar55 mentioned, use new Pokemon(this) if you are referencing your plugin in the Pokemon constructor (common-practice for Java plugins), i.e. where your Pokemon constructor is:
public Pokemon(TestPlugin myPlugin) {
this.plugin = myPlugin;
}