The import com.twilio.Twilio cannot be resolved - java

I want to use the Twilio API to allow users from my web application to make calls or send messages. So far I only wrote this basic code:
import com.twilio.sdk.TwilioRestClient;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Main {
public static final String ACCOUNT_SID = "ACXX";
public static final String AUTH_TOKEN = "XX";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+40742000000"), new PhoneNumber("+40742000000),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
This is the place where my JAR is stored now(the end of Referenced Libraries)
It just says that the imports Twilio cannot be resolved. I have Java version 8, so it should be working like this. I also download the JARs and followed the instalation from this page. Still not working. Does any of you have an idea how to make it work?

Twilio developer evangelist here.
If you are using the version 7 Twilio Java library then you no longer need to import com.twilio.sdk.TwilioRestClient;. In fact, that is no longer there, so this might be causing your import issues.
Also, make sure you have only one version of the JAR in your project. And make sure to keep up to date, the current version, as of writing, is 7.14.4.
Check out the docs on making a call with Twilio in Java. You'll find the example looks like this:
import java.net.URI;
import java.net.URISyntaxException;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Example {
// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "your_account_sid";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+14155551212"), new PhoneNumber("+15017250604"),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
}
Give that a go and let me know if it helps.

If anyone trying to use twilio [ I am using with spring boot ], please always make sure you pick up the latest version <version>7.50.1</version> as of now.
I used earlier version and it didn't work. As soon as I upgraded that to 7.50.1
it worked.
I hope it helps you.

Related

Java imported .jar file but jar's class objects cannot resolved

I am trying to send data via usb with Java.And I decided to use the jSerialComm library. I downloaded the required jar file and imported it correctly.
The whole code :
import com.fazecast.jSerialComm.SerialPort;
public class Try{
public static void main(String[] args) throws IOException {
SerialComm ports[] = SerialComm.getCommPorts();
}
}
There is no problem with this row :
import com.fazecast.jSerialComm.SerialPort;
But there is a problem here :
SerialComm ports[] = SerialComm.getCommPorts();
Error message : SerialComm cannot be resolved to a type.
And this is advice : Create class 'SerialComm'
You
import com.fazecast.jSerialComm.SerialPort
But not
import com.fazecast.jSerialComm.SerialComm
SearialPort class is different than SerialComm, you need to import SerialComm class as well.
If you couldn't find it, it means your jar file is not compatible with your snippet code.
you should import this way :
import com.fazecast.jSerialComm.SerialComm
you could check out this if you want to more information :instalation
look at this is a example of send data via usb with Java
check out this :jSerialComm/package-summary

Apache KafkaServer create reporters from Java

Follow up of create `KafkaServer` from Java
I am creating a KafkaServer from Java (well Clojure really but given a working Java example it is straightforward to translate).
I am not able to pass anything but an empty sequence. How can I write the equivalent of this line in Java?
https://github.com/apache/kafka/blob/cb674e5487f3f56647546b323dfe4fd45ccf0186/core/src/main/scala/kafka/server/KafkaServerStartable.scala#L27
val reporters = KafkaMetricsReporter.startReporters(new VerifiableProperties(serverProps))
Or, better yet, is there a Java/Clojure API for creating reporters?
The code I gave you in create KafkaServer from Java should work. I just tried it in 0.11.0.1 (the version you mentioned in the other question) and it works fine.
Full snippet:
package main;
import java.util.Properties;
import kafka.metrics.KafkaMetricsReporter;
import kafka.metrics.KafkaMetricsReporter$;
import kafka.utils.VerifiableProperties;
import scala.collection.*;
public class Reporters {
public static void main(String[] args) {
Properties props = new Properties();
Seq<KafkaMetricsReporter> reporters = KafkaMetricsReporter$.MODULE$.startReporters(new VerifiableProperties(props));
}
}

NullPointerException when using multiple stepdefinitions in Cucumber-jvm

I am currently building a framework to test a Rest-API endpoint. As I am planning to write a lot of test cases, I decided to organize the project to allow me to reuse common Step Definition methods.
The structure is as follows;
FunctionalTest
com.example.steps
-- AbstractEndpointSteps.java
-- SimpleSearchSteps.java
com.example.stepdefinitions
-- CommonStepDefinition.java
-- SimpleSearchStepDefinition.java`
However when I try to call SimpleSearchSteps.java methods I get a NullPointerException
CommonStepDefinition Code
package com.example.functionaltest.features.stepdefinitions;
import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.AbstractEndpointSteps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class CommonStepDefinition {
#Steps
private AbstractEndpointSteps endpointSteps;
#Given("^a base uri \"([^\"]*)\" and base path \"([^\"]*)\"$")
public void aBaseUriAndBasePath(String baseURI, String basePath) {
endpointSteps.givenBasepath(baseURI, basePath);
}
#When("^country is \"([^\"]*)\"$")
public void countryIs(String country)
{
endpointSteps.whenCountry(country);
}
#Then("^the status code is (\\d+)$")
public void theStatusCodeIs(int statusCode) {
endpointSteps.executeRequest();
endpointSteps.thenTheStatusCodeIs200(statusCode);
}
}
SimpleSearchStepDefinition.java
package com.example.functionaltest.features.stepdefinitions;
import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.EndpointSteps;
import cucumber.api.java.en.When;
public class SimpleSearchStepDefinition {
#Steps
private SimpleSearchSteps searchSteps;
#When("^what is \"([^\"]*)\"$")
public void whatIs(String what) {
searchSteps.whenWhatIsGiven(what);
}
}
Looks like you are missing holder class for Cucumber annotation, something like this you should have so that cucumber knows and identified that steps and features of yours:
#RunWith(Cucumber.class)
#CucumberOptions(
glue = {"com.example.functionaltest.features.steps"},
features = {"classpath:functionaltest/features"}
)
public class FunctionalTest {
}
Note that, in your src/test/resources you should have functionaltest/features folder with your .feature files according to this sample, you can ofc, change it by your design
Can you take a look at Karate it is exactly what you are trying to build ! Since you are used to Cucumber, here are a few things that Karate provides as enhancements (being based on Cucumber-JVM)
built-in step-definitions, no need to write Java code
re-use *.feature files and call them from other scripts
dynamic data-driven testing
parallel-execution of tests
ability to run some routines only once per feature
Disclaimer: I am the dev.
I solved this issue by using a static instance of RequestSpecBuilder in the AbstractEndpointSteps instead of RequestSpecification.
Therefore, I was able to avoid duplication of StepDefinitions and NPE issues altogether

How to create a new Bundle object?

I'm trying to use Firebase Analytics for an Android application, and in order to log events I've followed https://firebase.google.com/docs/analytics/android/events. That is, in order to send my event, I have to create a new Bundle object (which I create by using the default constructor) and I call the logEvent function of Firebase Analytics. While testing my development with a simple unit test, I realized that there's no content set in the bundle, which makes me wonder if any information is sent at all. Incidentally, it also breaks my test case.
Here's a simplified test case that shows my problem:
import android.os.Bundle;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class SimpleTest {
#Test
public void test() {
Bundle params = new Bundle();
params.putString("eventType", "click");
params.putLong("eventId",new Long(5542));
params.putLong("quantity", new Long(5));
params.putString("currency", "USD");
assertEquals("Did not find eventType=click in bundle", "click", params.getString("eventType"));
}
}
This test case fails with the following message:
junit.framework.ComparisonFailure: Did not find eventType=click in bundle
Expected :click
Actual :null
Would someone know where the problem is? That is, how do I create a Bundle object from zero and populate it correctly so that I can use it in a unit test like this?
Please bear with me on this one as I'm discovering the specifics of the Android environment as we speak.
As pointed out by Tanis.7x in a comment to my original question, all Android framework classes need to be mocked as the android.jar used for running unit tests is empty as documented here.
Here's an updated version of my original simplified test case:
import android.os.Bundle;
import org.junit.Test;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
public class SimpleTest {
#Test
public void test() {
Bundle bundleMock = Mockito.mock(Bundle.class);
Mockito.doReturn("click").when(bundleMock).getString("eventType");
Mockito.doReturn(new Long(5542)).when(bundleMock).getLong("eventId");
Mockito.doReturn(new Long(5)).when(bundleMock).getLong("quantity");
Mockito.doReturn("USD").when(bundleMock).getString("currency");
assertEquals("Did not find eventType=click in bundle", "click", bundleMock.getString("eventType"));
}
}
The main difference is, that the variables I set earlier with simple getters are now set by using the appropriate functions of Mockito. The code is not as easy on the eyes, but it should allow me to obtain the wanted behaviour.
Try using .equals() to compare strings as assertEquals() also uses the .equal() method for its working.

Why do I get "Type okhttp3.Call does not have type parameters" when using Retrofit2?

I am trying to follow this tutorial for Retrofit2 Getting Started and Create an Android Client.
The imports are fine
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
and I can follow the tutorial fine except for one thing. I am trying to create the GitHubService Interface and I run into two problems: The Call<V> says that it doesn't take any type parameters and I am also unsure where to put the Contributor class since it's according to the tutorial only declared as static, does that mean it's nested somewhere?
import okhttp3.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface GitHubClient {
#GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
#Path("owner") String owner,
#Path("repo") String repo
);
}
static class Contributor {
String login;
int contributions;
}
I have the Contributor class in a separate file and have it as public.
Also, the Call class does not import automatically in Android Studio, I have to select it manually, but it's the only Call I got (except for Androids phone api)
Please help me with why I get this errors, from what I can see there is no one around with the same thing so I am missing something fundamental.
Accordingly to the compile time error you are getting you did import Call from the wrong package. Please, check your import and be sure that you have
import retrofit2.Call;
everything Retrofit related import should be from the package retrofit2.
On the other hand
Call contributors(
it can't guess what you want to return. A Contributor ? a List<Contributor> maybe? E.g.
public interface GitHubClient {
#GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
#Path("owner") String owner,
#Path("repo") String repo
);
}

Categories

Resources