Androidx ServiceTestRule cannot find my service - android-service

To be fair, I am not testing the service, but am using a service as part of my test classes to test a Bluetooth library. So my test class needs to create a service that invokes the Bluetooth library. The test class then needs to bind to this service to perform the tests. However, attempting to start the service always gives me the following error which leads to a Nullpointer exception
W/ActivityManager: Unable to start service Intent { cmp=com.example.androidhdpadapter.test/com.lampreynetworks.ahd.oxp.transport.BluetoothTestService } U=0: not found
At this stage, the test class is very simple:
package com.lampreynetworks.ahd.oxp.transport;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ServiceTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.fail;
#RunWith(AndroidJUnit4.class)
public class AndroidServiceBluetoothAdapterTests
{
private final static String TAG =
AndroidServiceBluetoothAdapterTests.class.getName();
#Rule
public final ServiceTestRule serviceRule = new ServiceTestRule();
#Test
public void testAndroidBluetoothAdapter() throws TimeoutException, InterruptedException
{
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Intent intent = new Intent(context, BluetoothTestService.class);
serviceRule.startService(intent);
Thread.sleep(12000);
IBinder binder = serviceRule.bindService(intent);
BluetoothTestService service = ((BluetoothTestService.TestManagerBinder) binder).getService();
serviceRule.unbindService();
}
}
There are NUMEROUS posts regarding problems with testing services and I have tried all of them (messing with gradle.build file dependencies, conflicts between androidx.test and android.support.test, etc.). I have also tried invoking the service in the standard manner without ServiceTestRule, but that likely failed because there is no AndroidManifest for the test that specifies the service (I don't know if that can be done).
Maybe it is not possible to use Androidx's ServiceTestRule unless one is actually testing a service that is part of an application. Here I am testing a Bluetooth Library that is typically invoked by a service, and this test service is NOT part of the library, but exists only in the androidTests directory.
The reason I am doing this is that Android has buggy Bluetooth and on some platforms, the Bluetooth spontaneously shuts down and then restarts. My Bluetooth Library tries to recover from this shutdown. The recovery works BUT for every spontaneous shutdown and recovery, I get a service leak. That is what I am trying to solve in a simplified framework instead of a full-fledged application.

Related

Could not instantiate new WebDriver instance of type class org.openqa.selenium.remote.RemoteWebDriver

Getting the below error while try running in sauce lab. I am not sure what is the issue to apply fix here. Could someone extend help?
Note: Secure remote tunnel is provisioned and running successfully.
Command:
mvn clean verify -Dwebdriver.driver="remote" -Dsaucelabs.browserName="chrome" -Dsaucelabs.browserVersion="102" -Dsaucelabs.platformName="windows 10" -Dsaucelabs.url="https://foo:boo#ondemand.eu-central-1.saucelabs.com:443/wd/hub" -Denv="dev" -Dwebdriver.base.url="https://test.com/login" -Dsaucelabs.accessKey="foo" -Dsaucelabs.username="boo" -Dcucumber.filter.tags="#test" -Dsaucelabs.tunnelName="koo" -Dsaucelabs.tunnelOwner="boo" -Dsaucelabs.name="POC" -Dwebdriver.remote.url="https://ondemand.us-west-1.saucelabs.com/wd/hub"
TestRunner.java
package xyz.abc;
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(plugin = {"pretty"},
features = "Test",
glue = "fooboo"
)
public class TestRunner {
}
Expected Results:-
Execution should take place in sauce labs.
Actual Results:-
Could not instantiate class org.openqa.selenium.remote.RemoteWebDriver
It seems you are not setting your Sauce Labs user and API key, which ends up as an unauthorized request.

Having problems launching my glassfish server

I followed a starting guide for jax-rs web service development with intellij,glassfish. After i done the tutorial i tried to launch it as shown in tutorial, but server launched and atemmpted to start domain for a while and the website did not load. So I stopped the server and got error message at the bottom on the screen: http://prntscr.com/nsrmeb. Moreover here is how i start my application.
Tried restarting it couple times also searching in web for similar problems, but failed to find anything.
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/")
public class MyApplication extends Application{
#Override
public Set<Class<?>> getClasses(){
HashSet h = new HashSet<Class<?>>();
h.add(MatchResource.class);
return h;
}
}
This is how the tutorial I followed:
https://www.jetbrains.com/help/idea/creating-and-running-your-first-
restful-web-service.html

REST Annotated classes not part of org.superbiz package are not being deployed by TomEE Plume 7.0.4

On TomEE Plume 7.0.3/4 when I change my REST annotated classes package from org.superbiz then they are no more deployed when I deploy the war file.
Sample class is given below
package com.abc.rest.application;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/rest-prefix")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(SimpleRESTPojo.class, SimpleRESTEJB.class));
}
}
Now ApplicationConfig class is never read by the TomEE Plume. What configuration I need to make so that TomEE recognize my rest application config class and other jax-rs, ejb and jpa annotated classes.
In system.properties set openejb.classloader.forced-load=com.abc
then only my application packages would be loaded. Tricky to understand.

Android Instrumentation Tests Stuck "Running Tests" Forever Android Studio

I use Android Espresso Tests with latest Android Studio 2.1.2 and Tests are running ok but it does not seems like the standalone test app returns back the results to reflect back to Android Studio and it shows Running Tests Forever
I realize this is an old question, but I just ran into this and didn't see any other search results that had the same problem.
In my case, this was caused by the code under test having a stack overflow exception, which it seems that the test runner failed to handle. I was only able to figure it out by looking at the "Android Monitor" and looking in the logcat.
So if you get to the point where AS just sits at "running test" forever, you might want to look in logcat for an exception that the test runner couldn't handle.
You have to try removing testCoverageEnabled option from build.gradle file if it's there.
possible duplicate
Gradle build tool long for android Tests
In case this helps anyone.
In my case, I was setting the idle state wrongly(false instead of true) after doing a background task, so espresso was stuck thinking that was idle.
You can add an exit test class like this and include this in your Test Suite or Executor in the last
import android.os.Looper;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.io.IOException;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
#RunWith(AndroidJUnit4.class)
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExitTests {
#Rule
public ActivityTestRule<MainActivity> myActivityTestRule =
new ActivityTestRule<>(MainActivity.class, false, true);
public void init(){
getInstrumentation().waitForIdleSync();
if(Looper.myLooper() == null)
Looper.prepare();
}
#Test
public void Exit() throws InterruptedException, IOException {
Runtime.getRuntime().exec(new String[] {"am", "force-stop", "com.package.name"});
}
}
Here is the Test Suite
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
#RunWith(Suite.class)
#Suite.SuiteClasses({ABC.class, ExitTests.class})
public class TestExecutionOrder {
}

How to use selenium-server-standalone-2.0rc2 while creating script with RC

package com.html;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class Html5 extends TestCase{`enter code here`
Selenium selenium1;
public void setUp()
{
selenium1=new DefaultSelenium("localhost",4444,"*firefox","http://live.com");
selenium1.start();
}
}
Error appearing in com.thoughtworks.selenium.DefaultSelenium; and DefaultSelenium("localhost",4444,"*firefox","http://live.com"); line.
Please suggest.
First :
What the enter code here string does there ?
Secondly :
If there is an error in the import com.thoughtworks.selenium.DefaultSelenium; and in the new DefaultSelenium, it's certainly that the jars are not in your classpath
selenium-server-standalone contains the Selenium server classes, but not the client ones, where DefaultSelenium is. You'll have to bring the client jars in your classpath, that is selenium2-java for this version I think
I think you need to give Path to firefox.exe in your Constructor..So
selenium1 = new DefaultSelenium("localhost",4444,"*firefox","http://live.com"); Goes like
selenium1 = new DefaultSelenium("localhost",4444,"*firefox C:\Documents and Settings\Mozilla Firefox\firefox.exe","http://live.com");
Try this once.