I am writing a test for which having a Jenkins instance . so I need to mock the Jenkins class and set the instance to Jenkins. What is the method to set the instance?
Import jenkins.model.Jenkins
Related
I'm using JUnit 5 in my Spring project and would like to do some mocking using Mockito. So I've I added a couple Mockito dependencies: mockito-core 2.21.0 and mockito-junit-jupiter 4.0.0.
Then based on some guidance I found somewhere I added this to my very simple test class:
#ExtendWith(MockitoExtension.class)
But when I run the test I get this confounding error:
java.lang.NoSuchMethodError: org.mockito.internal.configuration.plugins.Plugins.
getMockitoLogger()Lorg/mockito/plugins/MockitoLogger;
...
But I'm not using the MockitoLogger class anywhere, or at least not explicitly. So what could cause this strangeness?
If you want to mock a method and test a method in the same class you have to use #Spy instead of #Mock. Then you should remove the #BeforeEach code block. Additionally you have to call the method you want to test.
Plz share more code, thanks
While executing the script using TestNG, it is skipping the main. Below is a sample code, which is similar to mine. If i remove String[] args, then the code is working with TestNG, but running as java application option is removed. How can i overcome this?
Package tests;
import org.testng.annotations.Test;
#Test
public class SearchText {
public void createzoo(String[] args) {
String[] elems = {"lion", "tiger", "duck"};
System.out.println(elems[0]);
System.out.println(elems[1]);
System.out.println(elems[2]);
}
}
Here is the Answer to your Question:
When you want to execute a Java Project you have 2 ways to do so:
Execute the Java file which contains the main method as:
public static void main(String[] args)
Your Java program will start executing from Line 1 within main method.
If you have integrated TestNG libraries to your Java Project you have got a new approach to execute your Java Project. Once you integrate TestNG there is absolutely no need of writing main method as previous case. In this case you need to execute the Class file which contains the #Test Annotation of TestNG as a TestNG Test. TestNG will execute your methods marked with #Test Annotations one by one as per the logic you specify. Once you execute your Java Project as a TestNG Test a xml file is generated by the name testng-customsuite.xml.
Next you can use your IDE feature Convert to TestNG to convert your Java Project to a TestNG specific Project. On doing that you will observe an xml file by the name testng.xml being created at your Project Level. Now using testng.xml you can also be able to execute the Project as a TestNG Suite
Let me know if this Answers your Question.
If you set #Test on a class, then it means that all methods of the class are test methods, included main.
If a test method has params, then TestNG must know how to inject them.
Currently, there is no way to exclude some method via annotations and an issue related issue already exists: https://github.com/cbeust/testng/issues/1405
As an alternative, you can only annotate methods instead of class and it should work.
This error "XYZ method requires 1 parameters but 0 were supplied in the #Test annotation" is encountered when there is misuse of #Test annotation in the code.
In your case, you can either run the application as java application or as TestNG application. When using TestNG , main is not required and you can use run as TestNG. When running as java application, main method would be required.
If i remove String[] args, then the code is working with TestNG, but running as java application option is removed.
When you remove String[] args, it will not be considered main method required for running java application. It is just any regular method. Hence it is working with TestNG but run as java application option is not applicable.
I am building a framework that is able to execute Selenium instructions and Cucumber instructions. I have created the runner for Cucumber in a seperate empty class with the following code
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
I also will have a processor for Selenium instructions in a separate class.
prop.load(ClassLoader.getSystemResourceAsStream("src/main/resources/runConfig.properties"));
if(prop.getProperty("instructionsheet").contains("InstructionSheets")){
KeywordProcessor kp = new KeywordProcessor();
}else if(prop.getProperty("instructionsheet").contains("FeatureFiles")){
RunCukesTest runCukes = new RunCukesTest();
}
How can I execute the Cucumber runner class if it's an empty class?, do I just create an instance of the runner class for cucumber? or do I have to spacify something else
#RunWith(Cucumber.class) is used to integrate cucumber with the existing infrastructure for running JUnit tests. If you start to roll out your own framework, it is worth to look other places. cucumber.api.cli.Main could be a good place to start your journey.
The class RunCukesTest is a class that will be executed by the JUnit runner Cucumber.class since it is annotated with #RunWith(Cucumber.class).
In other words, it will be executed when you run the tests the same way as any other JUnit class.
The class is empty, and has to be empty, to force a separation of the execution of Cucumber and the steps supporting the execution.
If you want to execute Cucumber from another context, say your own framework, look at the command line interface. You are able to run its main() from any Java class, just call the static method and wait for the result.
Was woundering if there was a way to have a JUnit class that runs other JUnit testclasses.
I have got a few testclasses, and i wan't to make a JUnit class that runs all the other test classes so i can just run the one JUnit class to run all the tests.
You probably need a TestSuite.
http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/TestSuite.html
How can I use the Groovy Console to kick off junit tests?
(Currently using Groovy 1.6.0)
I currently use:
junit.textui.TestRunner.run(MyTest)
(Where my junit test class is MyTest)
I've tried running:
MyTest
within the Groovy Console, but that just returns the class instance. Is there an easier way?