alwaysRun parameter in TestNG - java

Can someone please explain me when #AfterMethod(alwaysRun = true) should execute. Will it execute when #test method is skipped. In documentation is written so, but I have observed different behavior.
Example:
Code:
public class testing{
#Test
public void testCase2(){
System.out.println("in test case 2");
Assert.assertEquals(1,2);
}
#Test(dependsOnMethods = { "testCase2" })
public void testcase3(){
System.out.println("OK");
}
#AfterMethod(alwaysRun = true)
public void afterMethod2() {
System.out.println("in afterMethod");
}
}
Output:
in test case 2
in afterMethod
FAILED: testCase2
java.lang.AssertionError: expected [2] but found [1]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:494)
at org.testng.Assert.assertEquals(Assert.java:123)
at org.testng.Assert.assertEquals(Assert.java:370)
at org.testng.Assert.assertEquals(Assert.java:380)
at app.testing.testCase2(testing.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
SKIPPED: testcase3
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 1
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 1
===============================================
Shouldn't #After method be executed twice, also after skipped #Test?
Thanks for answer in advance :)

The documentation says
For after methods (afterSuite, afterClass, ...): If set to true, this
configuration method will be run even if one or more methods invoked
previously failed or was skipped.
So I think it should execute after skipped test method.

Even though the documentation has stated that
For after methods (afterSuite, afterClass, ...): If set to true, this
configuration method will be run even if one or more methods invoked
previously failed or was skipped.
However, in configfailurepolicy annotations, the following is stated when there's any test failures
configfailurepolicy: Whether TestNG should continue to execute the
remaining tests in the suite or skip them if an #before* method fails.
Default behavior is skip.
BTW, #AfterMethod is not a test method but a configuration one.

alwaysRun on #Test: A method (say method2) having this testng attribute willalways run even if the method on which it depends (dependsOnMethods="Method1") failed and it is referred as soft dependency in testNG terminology.
alwaysRun on #Before or #After annotated methods: if #BeforeSuite fails and #BeforeClass has alwaysRun = true then it won't be skipped. (although test case may get skipped)
public class TestAlwaysRun{
#BeforeSuite
public void toFailBeforeSuite() {
System.out.println("I'm beforeSuite, I will fail");
throw new RuntimeException();
}
#BeforeTest(alwaysRun = true)
public void toFailBeforeTest() {
System.out.println("BUG: I'm beforeTest, I will fail");
throw new RuntimeException();
}
#BeforeClass
public void toFailBeforeClass() {
System.out.println("BUG: I'm beforeClass, I will fail");
throw new RuntimeException();
}
#BeforeMethod(alwaysRun = true)
public void toFailBeforeMethod() {
System.out.println("BUG: I'm beforeMethod, I will fail");
//throw new RuntimeException();
}
#Test
public void tetsCase() {
System.out.println("I'm a Test Case");
}
}
[RemoteTestNG] detected TestNG version 7.4.0
I'm beforeSuite, I will fail
BUG: I'm beforeTest, I will fail
BUG: I'm beforeMethod, I will fail
FAILED CONFIGURATION: #BeforeTest toFailBeforeTest
java.lang.RuntimeException
at testngTutorial.ABC.toFailBeforeTest(ABC.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:637)
at org.testng.TestRunner.beforeRun(TestRunner.java:627)
at org.testng.TestRunner.run(TestRunner.java:589)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
FAILED CONFIGURATION: #BeforeSuite toFailBeforeSuite
java.lang.RuntimeException
at testngTutorial.ABC.toFailBeforeSuite(ABC.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:317)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED: tetsCase
java.lang.RuntimeException
at testngTutorial.ABC.toFailBeforeTest(ABC.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:637)
at org.testng.TestRunner.beforeRun(TestRunner.java:627)
at org.testng.TestRunner.run(TestRunner.java:589)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 2, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 0, Skips: 1
Configuration Failures: 2, Skips: 0
===============================================

I think here you have given dependsOnMethods = { "testCase2" }
That is why your second test itself isn't triggered and so aftermethod doesn't execute.
Please remove that depends and check ..it will execute aftermethod twice.

Related

Selenium java I have issue with two method that are failed, there is no any error shown in code but Two cases are failed

I have issue with title verification & close browser method both are failed, there is no any error shown in code but 2 case are failed..
This is my code
package com.google;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Titlevarification {
#Test(priority=1)
public void Launchbrowser() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\Admin\\Downloads\\New folder
(3)\\geckodriver\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
Thread.sleep(1000);
}
#Test(priority=2)
public static void titlevarification(WebDriver driver)
{
String ExpectedTitle = "Google";
String ActualTitle = driver.getTitle();
if (ActualTitle.equals(ExpectedTitle))
{
System.out.println("Test Passed!");
}
else
{
System.out.println("Test Failed");
}
}
#Test(priority=3)
public static void closebrowser(WebDriver driver)
{
driver.quit();
System.out.println("Browser Closed");
}
}
these are errors
PASSED: Launchbrowser FAILED: titlevarification
org.testng.TestNGException: Cannot inject #Test annotated Method
[titlevarification] with [interface org.openqa.selenium.WebDriver].
For more information on native dependency injection please refer to
https://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:439)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:353)
at org.testng.internal.Parameters.createParameters(Parameters.java:708)
at org.testng.internal.Parameters.handleParameters(Parameters.java:884)
at org.testng.internal.Parameters.handleParameters(Parameters.java:740)
at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:59)
at
org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:38)
at
org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:791)
at
org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
FAILED: closebrowser
org.testng.TestNGException: Cannot inject #Test
annotated Method [closebrowser] with [interface
org.openqa.selenium.WebDriver]. For more information on native
dependency injection please refer to
https://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:439)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:353)
at org.testng.internal.Parameters.createParameters(Parameters.java:708)
at org.testng.internal.Parameters.handleParameters(Parameters.java:884)
at org.testng.internal.Parameters.handleParameters(Parameters.java:740)
at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:59)
at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:38)
at
org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:791)
at
org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Default test
Tests run: 3, Failures: 2, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 1, Failures: 2, Skips: 0
===============================================
Passing arguments to methods annotated with #Test should be done in a special way. I suggest that you remove the WebDriver argument from both methods and make it an instance variable instead.

FAILED CONFIGURATION: #BeforeMethod setUp : java.lang.NullPointerException

I have written the below code which is one of the program in designing a framework and it is giving me this below error everytime. Can some one pls help me in fixing this issue.
"[RemoteTestNG] detected TestNG version 7.0.1 FAILED CONFIGURATION: #BeforeMethod setUp java.lang.NullPointerException"
package com.mercury.qa.testcases;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.mercury.qa.base.TestBase;
import com.mercury.qa.pages.HomePage;
import com.mercury.qa.pages.LoginPage;
public class LoginPageTest extends TestBase {
LoginPage loginPage;
HomePage homePage;
public LoginPageTest() {
// This super() keyword will call the constructor of the base class which is 'TestBase'
super();
}
#BeforeMethod
public void setUp() {
// This will call initialization() method from the base class but initialization() method
//in the base class contains getProperty() method so to avoid the compiler to throw null pointer
//exception we have #Nullabledesigned the above constructor and called the base class constructor
//through super() keyword.
initialization();
loginPage = new LoginPage();
}
#Test(priority = 1)
public void loginPageTitleTest() {
String title = loginPage.validateLoginPageTitle();
Assert.assertEquals(title, "Welcome: Mercury Tours");
}
#Test(priority = 2)
public void mercuryImageLogoTest() {
boolean flag = loginPage.validateMercuryLogo();
Assert.assertTrue(flag);
}
#Test(priority = 3)
public void loginTest() {
//HomePage homePage = new HomePage(); // same as return New HomePage() which is written in LoginPage.java class
homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
}
#AfterMethod
public void tearDown() {
driver.quit();
}
}
and below is the test base class which the above program is extending
package com.mercury.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.mercury.qa.util.TestUtil;
public class TestBase {
public static WebDriver driver;
public static Properties prop;
public static EventFiringWebDriver e_driver;
public TestBase(){
try {
prop = new Properties();
FileInputStream ip = new FileInputStream("F:\\Vishal_Offc Work\\Workspace\\"
+ "FreeMercuryTest\\src\\main\\java\\com\\mercury"
+ "\\qa\\config\\config.properties");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization(){
String browserName = prop.getProperty("browser");
if(browserName.equals("chrome")){
System.setProperty("webdriver.chrome.driver","F:\\Drivers\\chromedriver_win32\\chromedriver.exe");
}
else if(browserName.equals("FF")){
System.setProperty("webdriver.gecko.driver","F:\\Drivers\\geckodriver-v0.26.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
}
below is the console error which I am getting
[RemoteTestNG] detected TestNG version 7.0.1
FAILED CONFIGURATION: #BeforeMethod setUp
java.lang.NullPointerException
at com.mercury.qa.base.TestBase.initialization(TestBase.java:50)
at com.mercury.qa.testcases.LoginPageTest.setUp(LoginPageTest.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED CONFIGURATION: #AfterMethod tearDown
SKIPPED CONFIGURATION: #BeforeMethod setUp
SKIPPED CONFIGURATION: #AfterMethod tearDown
SKIPPED CONFIGURATION: #BeforeMethod setUp
SKIPPED CONFIGURATION: #AfterMethod tearDown
SKIPPED: loginPageTitleTest
java.lang.NullPointerException
at com.mercury.qa.base.TestBase.initialization(TestBase.java:50)
at com.mercury.qa.testcases.LoginPageTest.setUp(LoginPageTest.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED: mercuryImageLogoTest
java.lang.NullPointerException
at com.mercury.qa.base.TestBase.initialization(TestBase.java:50)
at com.mercury.qa.testcases.LoginPageTest.setUp(LoginPageTest.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED: loginTest
java.lang.NullPointerException
at com.mercury.qa.base.TestBase.initialization(TestBase.java:50)
at com.mercury.qa.testcases.LoginPageTest.setUp(LoginPageTest.java:31)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 3
Configuration Failures: 1, Skips: 5
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 0, Failures: 0, Skips: 3
Configuration Failures: 1, Skips: 5
===============================================
In the TestBase() class in line no 41. I have missed the driver initialization for chrome although it was added for firefox but not for chrome. I have added the below statement in the if statement and it worked for me.
driver = new ChromeDriver();

java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:3000

I build a http server with http://sparkjava.com/ that validate files, if it is valid or not.
The code is very simple:
post("/pdf", (req, res) -> {
if (req.headers("application/pdf") == null) {
res.status(400);
PdfState state = new PdfState();
state.SetIsValid(false);
return JsonUtil.toJson(state);
}
InputStream stream = new ByteArrayInputStream(req.bodyAsBytes());
PdfState state = pdf.validate(stream);
res.type("application/json");
return JsonUtil.toJson(state);
});
Then I want to write a unit test and choose the http client library http://square.github.io/okhttp/.
The test function looks as follow:
public void IsPdfContentRequestValid_StreamValidPdfContent_ExpectJsonSuccess() throws Exception {
File file = new File("/Volumes/Dev/java/inspector/files/pass.pdf");
Request request = new Request.Builder()
.url("http://127.0.0.1:3000/pdf")
.addHeader("Content-type", "application/pdf")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
When I run the test, it complains:
java.net.ConnectException: Failed to connect to /127.0.0.1:3000
What is wrong with OKClient?
Update
That is the whole error:
[TestNG] Running:
/Users/developer/Library/Caches/IntelliJIdea2016.2/temp-testng-customsuite.xml
0 [Thread-1] INFO org.eclipse.jetty.util.log - Logging initialized #1554ms
java.net.ConnectException: Failed to connect to /127.0.0.1:3000
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:189)
at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:173)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60)
at com.cloudfirmation.inspector.controller.PdfControllerTest.IsPdfContentRequestValid_StreamValidPdfContent_ExpectJsonSuccess(PdfControllerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:746)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
at org.testng.TestNG.runSuites(TestNG.java:1104)
at org.testng.TestNG.run(TestNG.java:1076)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
56 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >>> Spark shutting down ...
56 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - done
===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
I think the server is running when start the test.

TestNG and Powermockito - cannot mock static void

I can't get Powermock work with TestNG. The same code adjusted to JUnit works fine, but somehow in TestNG it fails.
TestNG version is 6.8.21
Powermockito version is 1.6.1
package p;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
#PrepareForTest({FooTest.class})
public class FooTest {
#BeforeTest
public void setup() {
mockStatic(FooTest.class);
MockitoAnnotations.initMocks(this);
}
#Test
public void test() throws Exception {
PowerMockito.doNothing().when(FooTest.class,"foo");
FooTest.foo();
Assert.assertEquals(1, 1);
}
public static void foo() throws Exception {
throw new Exception("huh?");
}
}
The exception I get is on the when method:
java.lang.Exception: huh?
at p.FooTest.foo(FooTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:753)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)
at p.FooTest.test(FooTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:696)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:882)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1189)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
at org.testng.SuiteRunner.run(SuiteRunner.java:254)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
===============================================
Custom suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
So what am I missing here?
I suspect this may be the same issue and solution as this question. In other words make sure you are using the org.powermock.modules.testng.PowerMockObjectFactory. But as #fge says, you should #PrepareForTest the class containing the static method(s) to mock, which should not be the test class itself...

Powemockito constructor testing problems

Hej I have some problems with TestNG and powerMockito 1.5.x I tried with 1.5.5 and 1.5.6. The problem is when I try to verify a constructor that is not empty.
I've check quite some examples around about extending from PowerMockTestCase and so on but they do not seem to work for me.
package com.cdev.common;
public class TestSubject {
String data;
String context;
public TestSubject()
{
this.data = "Mock!!";
this.context = "PowerMockito";
}
public TestSubject(String data)
{
this.data = data;
this.context = "PowerMockito";
}
public TestSubject(String data,String context)
{
this.data = data;
this.context = context;
}
public String getData()
{
throw new UnsupportedOperationException();
}
public String getContext()
{
throw new UnsupportedOperationException();
}
public boolean isNew()
{
throw new UnsupportedOperationException();
}
}
This is the testNG test:
package com.cdev.common.test;
import com.cdev.common.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.Test;
#PrepareForTest({TestSubject.class})
public class TestSubjectTest{
#Test
public void TestSubject() throws Exception
{
System.out.println("======== Test ==================");
TestSubject mockTestSubject = PowerMockito.mock(TestSubject.class);
PowerMockito.whenNew(TestSubject.class).withArguments("A", "B").thenReturn(mockTestSubject);
PowerMockito.when(mockTestSubject.getData()).thenReturn("A");
PowerMockito.when(mockTestSubject.getContext()).thenReturn("B");
PowerMockito.when(mockTestSubject.isNew()).thenReturn(true);
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");
TestSubject lala = new TestSubject("A","B");
System.out.println(lala.getData());
System.out.println(lala.isNew());
Assert.assertTrue(lala.getData().equals("A"), "Not equal");
}
}
This gives me this output:
[TestNG] Running:
/tmp/testng-eclipse--2099806353/testng-customsuite.xml
======== Test ==================
FAILED: TestSubject
java.lang.AssertionError: Wanted but not invoked com.cdev.common.TestSubject(
"A",
"B"
);
Actually, there were zero interactions with this mock.
at org.powermock.api.mockito.internal.invocation.InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(InvocationControlAssertionError.java:93)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.invokeSubstitute(DefaultConstructorArgumentsVerfication.java:51)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.withArguments(DefaultConstructorArgumentsVerfication.java:44)
at com.cdev.common.test.TestSubjectTest.TestSubject(TestSubjectTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#6d9c638: 18 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#2401f4c3: 6 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#5d22bbb7: 3 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#3d012ddd: 31 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 2 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#1175e2db: 5 ms
You need to move the verifyNew call to after the invocation has been made:
TestSubject lala = new TestSubject("A","B");
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");
Following your instructions:
#Test
public void TestSubject() throws Exception
{
System.out.println("======== Test ==================");
TestSubject mockTestSubject = PowerMockito.mock(TestSubject.class);
PowerMockito.whenNew(TestSubject.class).withArguments("A", "B").thenReturn(mockTestSubject);
PowerMockito.when(mockTestSubject.getData()).thenReturn("A");
PowerMockito.when(mockTestSubject.getContext()).thenReturn("B");
PowerMockito.when(mockTestSubject.isNew()).thenReturn(true);
**TestSubject lala = new TestSubject("A","B");
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");**
System.out.println(lala.getData());
System.out.println(lala.isNew());
Assert.assertTrue(lala.getData().equals("A"), "Not equal");
}
The output:
[TestNG] Running:
/tmp/testng-eclipse--349458877/testng-customsuite.xml
======== Test ==================
FAILED: TestSubject
java.lang.AssertionError: Wanted but not invoked com.cdev.common.TestSubject(
"A",
"B"
);
Actually, there were zero interactions with this mock.
at org.powermock.api.mockito.internal.invocation.InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(InvocationControlAssertionError.java:93)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.invokeSubstitute(DefaultConstructorArgumentsVerfication.java:51)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.withArguments(DefaultConstructorArgumentsVerfication.java:44)
at com.cdev.common.test.TestSubjectTest.TestSubject(TestSubjectTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#6d9c638: 36 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#2401f4c3: 8 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#5d22bbb7: 13 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#3d012ddd: 45 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#1175e2db: 5 ms

Categories