What is wrong with my Java import statement? - java

Hello I am new to Java and new to trying out Unit Testing for the first time on a simple program but my import.main.BalancedQuestionMarks; statement is not getting recognized in my test file. I don't understand why my test file isn't recognizing my main file.
Here is my test file
import main.BalancedQuestionMarks;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class BalancedQuestionMarksTest {
#Test
public void onlyQuestionMarkReturnsTrue() {
assertTrue(BalancedQuestionMarksTest.hasBalancedMarks("??"), true);
}
}```
Here is the error when I try to run:
java: cannot find symbol
symbol: method hasBalancedMarks(java.lang.String)
location: class test.BalancedQuestionMarksTest

Related

How can I solve that type of issue when I compile using `javac`: " error: cannot find symbol [...]"?

For starters, I have to say that I am using IntelliJ IDEA Community Edition 2020.3.1 and running java 15.0.1 2020-10-20, also when I run my program after enabling assertions and clicking on the run button, it works as expected. That being said here is my file structure:
Here is the code in my TestRunner.java file:
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public final class TestRunner {
private static final List<Class<?>> TESTS = List.of(CalculatorTest.class);
public static void main(String[] args) throws Exception {
List<String> passed = new ArrayList<>();
List<String> failed = new ArrayList<>();
for (Class<?> klass : TESTS) {
if(!UnitTest.class.isAssignableFrom(klass)){
throw new IllegalArgumentException("Class "+ klass + " must implement UnitTest");
}
for(Method method : klass.getDeclaredMethods()){
if(method.getAnnotation(Test.class) != null){
try{
UnitTest test = (UnitTest) klass.getConstructor().newInstance();
test.beforeEachTest();
method.invoke(test);
System.out.println(method.invoke(test));
test.afterEachTest();
passed.add(getTestName(klass, method));
}catch(Throwable throwable){
failed.add(getTestName(klass, method));
}
}
}
}
System.out.println("Passed tests: " + passed);
System.out.println("FAILED tests: " + failed);
}
private static String getTestName(Class<?> klass, Method method) {
return klass.getName() + "#" + method.getName();
}
}
Here are my issues:
When I compile my main class TestRunner.java using javac TestRunner.java, it fails to find those symbols CalculatorTest.class, UnitTest.class, Test.class, UnitTest. Here is the error message:
When I use javac *.java though, my files compile and my .class files are generated, here is a screenshot:
but when I try to run my file using java TestRunner it says: "Error could not find or load class TestRunner, here is a screenshot:
If anyone can help me solve those issues I'd would be very happy. So far, I have found no solutions when I googled about them. Thank you!
After reading many answers, I found that the solution was simple.
First compiling TestRunner.java:
javac -cp . TestRunner.java
Then running TestRunner (containing my main function):
java -cp . -ea TestRunner
It turns out I was missing on the dot "."!
Here is the final result:

Compile Error: Package sqlj.runtime does not exist

I am trying to do a homework assignment in a database course that requires me to use sqlj. It is supposed to make a connection to a SQL database running on my computer. When copying and pasting the example code into Netbeans I get a compile error:
package sqlj.runtime does not exist: import sqlj.runtime.*;
import java.sql.*;
import java.io.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
import oracle.sqlj.runtime.*;
public class A10Q1 {
public static void main(String[] args) {
DefaultContext cntxt = oracle.getConnection("url", "user", "pswd", true);
}
}
I changed the inputs on the getConnection function.

How can I execute Taurus junit?

I made a taurus test spesifcation:
execution:
- executor: junit
iterations: 5 # loop over test suite for 5 times
concurrency: 20 # number of virtual users
ramp-up: 1m # time of load growing
steps: 5 # number of steps of growing
scenario:
script: src/test
modules:
junit:
junit-version: 5
working-dir: src/main/java
My Unit Test are:
package org.steinko.springtutorial;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.steinko.springtutorial.Main;
public class MainTest {
#Test
void shouldReturnANumber(){
Main main = new Main();
String[] arg = new String[1];
Main.main(arg);
int number = main.getNumber();
assertTrue(0 < number);
assertTrue(number < 100);
}
}
My source code place is:
package org.steinko.springtutorial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.steinko.springtutorial.NumberGenerator;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private static int number;
private static final String CONFIG_LOCATION = "beans.xml";
public static void main(String[] args )
{
log.info("Guess the number game");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATION);
NumberGenerator numberGenerator = context.getBean("numberGenerator", NumberGenerator.class);
number = numberGenerator.next();
log.info("number = {}", number);
context.close();
}
public int getNumber() {
log.info("getNumber",number);
return number;
}
}
When I run bzt ./performanctests/unittests.yaml
I get a error:
[2019-03-17 15:34:25,556 ERROR root] Child Process Error: Javac exited
with code: 1
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:7:
error: cannot find symbol
import org.steinko.springtutorial.Main;
^
symbol: class Main
location: package org.steinko.springtutorial
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:15:
error: cannot find symbol
Main main = new Main();
^
symbol: class Main
location: class MainTest
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:15:
error: cannot find symbol
Main main = new Main();
^
symbol: class Main
location: class MainTest
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:17:
error: cannot find symbol
Main.main(arg);
^
symbol: variable Main
location: class MainTest
4 errors
How do I fix this error?
This seems like you're trying to use an external dependency in your java class. Taurus doesn't exactly build the module before it tries to compile. So any dependencies that your project has in the classpath would not be recognized. The solution to this is to upload the dependencies as .jar files along with the .java file in the BZ test. Basically, any class/interface that you import in your test script should be supplied as a jar. So your config file would look something like this:
- executor: junit
iterations: 5 # loop over test suite for 5 times
concurrency: 20 # number of virtual users
ramp-up: 1m # time of load growing
steps: 5 # number of steps of growing
scenario:
script: src/test
additional-classpath: # just an example - you can include your own .jar files
- rest-assured-2.9.0.jar
- log4j-1.2.17.jar
- groovy-2.4.4.jar
- rest-assured-common-2.9.0.jar
- json-path-2.9.0.jar
modules:
junit:
junit-version: 5
working-dir: src/main/java
More details at https://gettaurus.org/docs/JUnit/

Java package and directories

It may be a similar to this question : package does not exist error!
but I don't understand how to manage it.
I try to follow this lesson (in French sorry) https://openclassrooms.com/courses/les-tests-unitaires-en-java
and so I have the following tree :
Garage/test/XXXTest.java, Garage/main/impl/XXX.java, Garage/main/inter/XXX.java
In test I have this code (GPSTest.Java)
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import main.impl.GPS;
public class GPSTest
{
#Test
public final void GPSTest() {
GPS gps = new GPS();
double prix = gps.getPrix();
assertTrue("Test prix GPS", prix == 113.5);
}
}
and in main/impl I have this one (GPS.java)
package main.impl;
import main.inter.Option;
public class GPS implements Option
{
public double getPrix()
{
return 113.5;
}
}
and in main/inter I have (Option.java)
package main.inter;
public interface Option
{
public double getPrix();
}
When I try to compile (I'm in Garage)
javac -cp "C:\Program Files (x86)\Java\junit-4.10.jar" test\GPSTest.java
I have this error
test\GPSTest.java:6: error: package main.impl does not exist
import main.impl.GPS;
Do I need to add Garage in the package name ? In the lesson (linked above) it's the same architecture and the same package name... But they use Eclipse, so maybe there are some differences (I use the command line)
EDIT
If I remove the test part it works :
test\TestGPS.java
package test;
/*import static org.junit.Assert.*;
import org.junit.Test;*/
import main.impl.GPS;
public class GPSTest
{
// #Test
public final void GPSTest() {
GPS gps = new GPS();
double prix = gps.getPrix();
//assertTrue("Test prix GPS", prix == 113.5);
System.out.println(prix);
}
}
With the following command doesn't give error... So I suppose the problem is with the classpath, but how can I fix it ?
javac test\GPSTest.java
Do I need to add Garage in the package name
No, but you need to be in the directory Garage when you compile, such that you are at the head of the following directory tree:
main
main/impl
main/impl/GPS.java
main/inter
main/inter/Option.java
test
test/GPSTest.java
The problem was with the clathpass. I had to add the current file to the path with .; before the rest of the path:
javac -cp .;"C:\Program Files (x86)\Java\junit-4.10.jar" test\GPSTest.java

Could not find class QWebElement

I'm using QtWebkit to develop a webapplication framework. Now I've begun to ude the DOM and I tryed to use the QWebElement. But when I tried to compile it brought the following error:
symbol : class QWebElement
location: class test
QWebElement elem = new QWebElement();
Here is an example that doesn't work:
import com.trolltech.qt.gui.*;
import com.trolltech.qt.webkit.*;
import com.trolltech.qt.core.*;
import com.trolltech.qt.xml.*;
public class test {
public test() {
QWebElement elem = new QWebElement();
}
public static void main(String[] args) {
QApplication.initialize(args);
new test();
QApplication.exec();
}
}
Could anyone please tell me what I'm doing wrong?
Edit:
I forgot:
When I look in the javadoc, I cant find it! everything says that it doesn't exist. But I've seen other people using it.
Does it exist or not?

Categories