using gradle, error: cannot find symbol #Test - java

I am trying to build the tests using gradle in the project.
On building the tests, I am getting error ->
:compileTestJava/home/vibhcool/Documents/github/loklak_server/test/org/loklak/harvester/TwitterScraperTest.java:28: error: cannot find symbol
#TEST
^
symbol: class TEST
location: class TwitterScraperTest
1 error
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
The file in which I am getting error ->
package org.loklak.harvester;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.loklak.harvester.TwitterScraper;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TwitterScraperTest {
#TEST
public void test_prepareSearchURL() {
String url;
String[] query = {"q1", "q2", "q3", };
String[] out_url = {"url1", "url2", "url3"};
for (int i = 0; i < query.length; i++) {
url = TwitterScraper.prepareSearchURL(query[i]);
assertEquals(url, out_url[i]);
}
}
}
When I comment #TEST in the file, gradle successfully builds the tests.
I have googled a lot but, not able to get the error.
I am using junit 4.10

It should be :
#Test
public void test_prepareSearchURL() {
...
}
and not
#TEST
public void test_prepareSearchURL() {
..
}
You can take a look on how to get started with Junit here.

Related

io.cucumber.junit.UndefinedStepException: The step ''.... are undefined

Been trying to learn/perform some automated testing using Selenium + JUnit + Cucumber and I've spent hours browsing through the internet trying to resolve this with no success. I've written my feature, steps and runner code and the 'glue' is mapped to the correct package too. However, I run into the topic error when I try to run my Runner.java file.
Project Structure
Feature file:
Feature: Admin login
Scenario: Logging into Fleet Portal
Given user navigates to Fleet Portal
When enter credentials
Then user had logged
Runner file:
package Runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.*;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Features"
,glue="Steps"
)
public class TestRunner {}
Step file:
package Steps;
import java.util.ArrayList;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
class App {
private static final WebDriver driver = new ChromeDriver();
public void myMethod(String flag) {
System.out.println(flag);
}
#Given("user navigates to Fleet Portal")
public void user_navigates_to_fleet_portal() {
throw new io.cucumber.java.PendingException();
}
#When("enter credentials")
public void enter_credentials() throws InterruptedException {
throw new io.cucumber.java.PendingException();
}
#Then("user had logged in successfully")
public void user_had_logged_in_successfully() {
myMethod("yes");
throw new io.cucumber.java.PendingException();
}
}
I've tried to take JUnit out of the picture by simply running the feature file as a cucumber test but it still results in the same error, which is weird because the cucumber plugin confirms that the steps are mapped to the features -> feature mapping
POM file
Any and all help is appreciated.

React Native: error: cannot find symbol > new AppCenterReactNativeAnalyticsPackage(MainApplication.this

I was getting a successful build with android in React Native until I added these:
"appcenter-analytics": "2.1.0",
"appcenter-crashes": "2.1.0",
I am on:
"react": "16.8.6",
"react-native": "0.60.4",
The error is:
Error: Command failed: ./gradlew app:installDebug
-PreactNativeDevServerPort=8081 Note: /Users/danale/Projects/NFIBEngage/node_modules/react-native-sentry/android/src/main/java/io/sentry/RNSentryModule.java
uses or overrides a deprecated API. Note: Recompile with
-Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for
details.
/Users/danale/Projects/NFIBEngage/android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java:60:
error: cannot find symbol
new AppCenterReactNativeAnalyticsPackage(MainApplication.this, getResources().getString(R.string.appCenterAnalytics_whenToEnableAnalytics)),
^ symbol: class MainApplication location: class PackageList
/Users/danale/Projects/NFIBEngage/android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java:60:
error: cannot find symbol
new AppCenterReactNativeAnalyticsPackage(MainApplication.this, getResources().getString(R.string.appCenterAnalytics_whenToEnableAnalytics)),
^ symbol: variable appCenterAnalytics_whenToEnableAnalytics
location: class string
/Users/danale/Projects/NFIBEngage/android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java:61:
error: cannot find symbol
new AppCenterReactNativeCrashesPackage(MainApplication.this, getResources().getString(R.string.appCenterCrashes_whenToSendCrashes)),
^ symbol: class MainApplication location: class PackageList
/Users/danale/Projects/NFIBEngage/android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java:61:
error: cannot find symbol
new AppCenterReactNativeCrashesPackage(MainApplication.this, getResources().getString(R.string.appCenterCrashes_whenToSendCrashes)),
^ symbol: variable appCenterCrashes_whenToSendCrashes location:
class string 4 errors
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Apparently there is this android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java file that did not exist with the legacy version of this application:
package com.facebook.react;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.ArrayList;
import com.nfibengage.BuildConfig;
import com.nfibengage.R;
// #react-native-community/async-storage
import com.reactnativecommunity.asyncstorage.AsyncStoragePackage;
// appcenter-analytics
import com.microsoft.appcenter.reactnative.analytics.AppCenterReactNativeAnalyticsPackage;
// appcenter-crashes
import com.microsoft.appcenter.reactnative.crashes.AppCenterReactNativeCrashesPackage;
// react-native-sentry
import io.sentry.RNSentryPackage;
// react-native-splash-screen
import org.devio.rn.splashscreen.SplashScreenReactPackage;
public class PackageList {
private Application application;
private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}
public PackageList(Application application) {
this.reactNativeHost = null;
this.application = application;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
}
private Resources getResources() {
return this.getApplication().getResources();
}
private Application getApplication() {
if (this.reactNativeHost == null) return this.application;
return this.reactNativeHost.getApplication();
}
private Context getApplicationContext() {
return this.getApplication().getApplicationContext();
}
public ArrayList<ReactPackage> getPackages() {
return new ArrayList<>(Arrays.<ReactPackage>asList(
new MainReactPackage(),
new AsyncStoragePackage(),
new AppCenterReactNativeAnalyticsPackage(MainApplication.this, getResources().getString(R.string.appCenterAnalytics_whenToEnableAnalytics)),
new AppCenterReactNativeCrashesPackage(MainApplication.this, getResources().getString(R.string.appCenterCrashes_whenToSendCrashes)),
new RNSentryPackage(),
new SplashScreenReactPackage()
));
}
}
The MainApplication.java file is inside of android/main/java/com/nfibengage/MainApplication.java:
package com.nfibengage;
import android.app.Application;
import android.util.Log;
import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}
#Override
protected String getJSMainModuleName() {
return "index";
}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
I tried importing it into PackageList.java like so:
import com.nfibengage.MainApplication;
but I continue to get the same error. If anyone can walk me through where did this PackageList.java file come from and how do I get this working again short of just completely removing appcenter-analytics and appcenter-crashes.
Please make sure your installation order is the same as the following.
Add the App Center SDK modules
npm install appcenter appcenter-analytics appcenter-crashes --save-exact
or
yarn add appcenter appcenter-analytics appcenter-crashes --exact
Integrate the SDK automatically
react-native link appcenter
react-native link appcenter-analytics
react-native link appcenter-crashes
App Center SDK does not set up mocks automatically for App Center modules during the linking process. If you are using Jest test framework in your application and experience errors caused by the App Center SDK while running tests with Jest, add the following to the jest section of package.json file (include only modules in use):
"setupFiles": [
"<rootDir>/node_modules/appcenter/test/AppCenterMock.js",
"<rootDir>/node_modules/appcenter-analytics/test/AppCenterAnalyticsMock.js",
"<rootDir>/node_modules/appcenter-crashes/test/AppCenterCrashesMock.js",
"<rootDir>/node_modules/appcenter-push/test/AppCenterPushMock.js"
]
Edit the project's android/app/src/main/assets/appcenter-config.json and replace the YOUR_APP_SECRET placeholder value with your App Center project's application secret.
do one think only
goto your folder structure.
2.Add a file yarn-error.log
there will not be any problem your project will start well.

All feature files are not executing

Cucumber not executing both the features with created stepdefinations
I have tried with tag, also given both full path of both the features but still the same
package runners;
import com.cucumber.listener.ExtentProperties;
import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import managers.Common;
import managers.FileReader;
import org.apache.log4j.PropertyConfigurator;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import java.io.File;
#RunWith(Cucumber.class)
#CucumberOptions(
features = ".//src//test//java//FeatureList",glue = "stepDefinations",
plugin = { "com.cucumber.listener.ExtentCucumberFormatter:",
"junit:target/cucumber-results.xml"},
tags={"#API"},
monochrome = true
)
public class TestRunner {
static String ReportName= Common.ReportName();
#BeforeClass
public static void setup() {
ExtentProperties extentProperties = ExtentProperties.INSTANCE;
extentProperties.setReportPath("target/cucumber-reports/"+ReportName+".html");
PropertyConfigurator.configure(".//src//log4j.properties");
}
#AfterClass
public static void writeExtentReport() {
Reporter.loadXMLConfig(new File(FileReader.getInstance().getConfigReader().getReportConfigPath()));
Reporter.setSystemInfo("User Name", System.getProperty("user.name"));
Reporter.setSystemInfo("Time Zone", System.getProperty("user.timezone"));
Reporter.setSystemInfo("Environment", FileReader.getInstance().getConfigReader().getEnvironment());
}
}
Not sure why its always running error codes.feature but never enums.feature
Feature: Enums Codes
#API
Scenario: xxx Enums Codes
Given Run get method "xxxxxxxxxxx" api to get fetch all type of xxx xxx
Then response should be 200
And xxxxxxxxxxxxxx
Feature: Error Codes
#API
Scenario: xxError Codes
Given Run "xxxx" api to get response
Then response should be 200
And Verify xx Error Codes xx error response
"features" is looking for a filesystem path:
features = ".//src//test//java//FeatureList"
Try this -
1 features = "src/test/java/FeatureList"
2 features = "FeatureList"

Selenium grid with Java - user input

I am writing automatic tests using Java with Selenium Grid and JUnit framework and I have encountered a problem with user input. So my code looks like this:
package com.example.tests;
import com.thoughtworks.selenium.DefaultSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Scanner;
import static org.junit.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class test {
private DefaultSelenium selenium;
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 5555, "*googlechrome", "www.google.com");
selenium.start();
}
#Test
public void Test() throws Exception {
// some tests here
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
I would like to add a user input, so when user types for example "Google Chrome", the test will start with Google Chrome, when he types "Firefox", the test will start with Firefox etc. I have tried to put
Scanner in = new Scanner(System.in);
String web_browser = in.next();
somwhere in my code (in setUp method for example), but when the program starts, I can't type anything in the console. Does anyone know the solution for this?
It's tricky dealing with System.in in the test.
I suggest that you rather read your driver preference as a system property?
String driver = System.getProperty("driver");
if (driver != null) {
//use that driver
}
else {
//use default driver
}
You can the launch your test like
mvn test -Ddriver=chrome
or by setting them in your IDE

Workaround to support org.junit.runners.Parameterized in Gradle

Gradle is unable to run JUnit tests annotated with:
import org.junit.runners.Parameterized;
#RunWith(value = Paremetrized.class)
public class UnitTest {
// ...
}
There is an official issue for this problem from June 2014 yet unsolved.
Are there any workarounds for enabling those tests (in Gradle 2.4)?
An alternative for JUnit's Parameterized is the open source third-party library junit-dataprovider. It is supported by Gradle (tested with 2.4).
The usage is similar:
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
#RunWith(DataProviderRunner.class)
public class DataProviderTest {
#DataProvider
public static Object[][] dataProviderAdd() {
// #formatter:off
return new Object[][] {
{ 0, 0, 0 },
{ 1, 1, 2 },
/* ... */
};
// #formatter:on
}
#Test
#UseDataProvider("dataProviderAdd")
public void testAdd(int a, int b, int expected) {
// Given:
// When:
int result = a + b;
// Then:
assertEquals(expected, result);
}
}
The dependency line for Gradle:
testCompile 'com.tngtech.java:junit-dataprovider:1.9.3'

Categories

Resources