How can I execute Taurus junit? - java

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/

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:

What is wrong with my Java import statement?

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

Under one package : compiler fails to read other class

I have three java files in one package : 'Receiver'.
CMReceiverMutant.java
CMReceiverMutantContext.java
TestDriver.java
Here is my TestDriver.java
package Receiver;
public class TestDriver{
public static void main (String[] args){
TestCase1();
// alternateTestCase1();
}
public static void TestCase1(){
CMReceiverMutant obj = new CMReceiverMutant();
obj.INT1SurvFlag();
obj.Exitw0();
System.out.println("Test case 1 reaches state :"+obj._fsm.getState().getName());
if(obj._fsm.getState().getName().equals("CMReceiverMap.Final"))
System.out.println("Test Case 1 passes!");
else
System.out.println("Test Case 1 fails");
}
}
I compiled TestDriver which depends on CMReceiverMutant.java. Eventhough I put them in the same directory. The compiler seems can't read CMReceiverMutant.java and it makes error :
TestDriver.java:11: error: cannot find symbol
CMReceiverMutant obj = new CMReceiverMutant();
^
symbol: class CMReceiverMutant
location: class TestDriver
I use cmd
javac -classpath Receiver\TestDriver.java
and I've tried
javac -classpath Receiver*.java
The errors are the same. Can you tell me whats the problem is?
Thank you
please check "Source" packages in the "Java Build Path" sometimes if the package isn't registered there the compiler fails to load them.
Hopefully it worked for me.

Java compilation / package / namespace error

This is the stupidest, simplest problem ever with basic Java, but what am I doing wrong?
In a directory structure called
com/myname/robos
- Robo.java
- Arena.java
Robo.java :
package com.myname.robos;
public class Robo {
public void fala() {
System.out.println("Gleep Beep Boop!");
}
}
Arena.java :
package com.myname.robos;
import com.myname.robos.Robo;
public class Arena {
public static void main(String[] args) {
Robo r2 = new Robo();
r2.fala();
}
}
When I try to :
javac Robo.java
it compiles.
When I then try to
javac Arena.java
I get
Arena.java:3: error: cannot find symbol
import com.myname.robos.Robo;
^
symbol: class Robo
location: package com.myname.robos
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
3 errors
I KNOW it's an error about incompatibility between directory / package etc. names.
But I still never get this right. What should I be writing?
You need to ensure that your files are in the same structure as your package reference, say:
if your .java files are for instance at ../Desktop you must create folders with names: com, myname and robos and then paste them there, so your files will be inside ../Desktop/com/myname/robos.
Then you just need to compile Arena.java and run it:
$ javac com/myname/robos/Arena.java
$ java com/myname/robos/Arena
Positioning you inside ../Desktop

Java: multiple packages minimum working example

Trying to understand how Java packages work with classpath etc. In ~/java/tmp/test/HelloWorld I created HelloWorld.java:
package test;
import test2.Hello2;
public class HelloWorld {
public static void main(String[] args) {
Hello2 x = new Hello2();
x.blagh(args);
}
}
Then in ~/java/tmp/test2/Hello2 I created Hello2.java:
package test2;
public class Hello2
{
public static void blagh(String[] args) {
System.out.println("Hello, World");
}
}
Working in ~/java/tmp, I try to compile using:
javac -g test/HelloWorld/HelloWorld.java
I get the following errors:
test/HelloWorld/HelloWorld.java:3: package test2 does not exist
import test2.Hello2;
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
I've tried things like setting the classpath (to every possible combination of the above paths I could think of), changing which directory I run the compiler from, etc. Nothing works. Please help.
Your HelloWorld.java belongs to package test, so it should live in a directory named test, not test/HelloWorld. Same with Hello2.java, it should live in test2, not test2/Hello2. Move HelloWorld.java to the test directory and Hello2.java to the test2 directory and give it another try.

Categories