I have the following code in my onCreate method:
Bundle appleData = getIntent().getExtras();
if(appleData==null) {
return;
}
String appleMessage = appleData.getString("appleMessage");
final TextView texttest = (TextView) findViewById(R.id.texttest);
texttest.setText(appleMessage);
I want to use the string stored in appleMessage in a different method on the same activity that is outside of onCreate, how do I go about doing this?
You should declare a String attribute outside of the onCreate method. For example:
private String appleMessage;
Then, inside your onCreate, simply change this line:
appleMessage = appleData.getString("appleMessage");
When you want to use it on other method, just call it. Its value will be the value setted on the onCreated method.
If you need it in several places in the class then you can make it a member variable by declaring it outside of a method
public class SomeClass extends Activity {
String appleMessage = "default text";
but if you only need it in one method you can simply pass it to the method call if you are calling the method in onCreate()
public void onCreate(Bundle bundle) {
...
String appleMessage = appleData.getString("appleMessage");
final TextView texttest = (TextView) findViewById(R.id.texttest);
texttest.setText(appleMessage);
// here
myMethod(appleMessage);
}
assuming myMethod takes a String as a parameter.
I also suggest you read the java docs and some basic tutorials before continuing with Android. It will make your life much easier.
Create a member variable on the class, which is set inside the onCreate method.
See here for more information on instance/member variables.
So:
class SomeClass extends Activity {
private String appleMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
appleMessage = ...
}
public void otherMethod() {
Log.e("LOG", "Here is appleMessage: " + appleMessage);
}
}
Declare it inside your class and no in the method.
e.g.
private String appleMessage;
and use getters and setters for it
public String getAppleMessage(){
return appleMessage;
}
Related
I ran into such a problem today that I cannot pass the View to the method.
I doing all this in order to get rid of the numerous dubbing code.
For example, I will show how I see it and if there is such the ability to pass to the method, then how to implement it the ability to pass to the method
error when passing to the method
how can I fix this problem?
Try this and see if it gets you anywhere.
public class Test
{
private SelectFrom<Account>.View testView;
public void Test()
{
Method1(testView.View);
Method2(testView);
}
public void Method1(PXView test)
{
}
private void Method2(FbqlSelect<SelectFromBase<Account,TypeArrayOf<IFbqlJoin>.Empty>, Account>.View view)
{
var current = view.Current;
}
}
Unit test noob here.
I have three classes: Db1Dao, Db2Dao, ExecuteClass where Db1Dao, Db2Dao are database access objects for two different databases. My goal is to fetch some data from db1 using Db1Dao and run executeClass.execute() to "put" the processed data into db2 using Db2Dao.
My ExecuteClass looks like this:
class ExecuteClass {
private Db1Dao db1Dao;
private Db2Dao db2Dao;
public void execute() {
...
List<String> listOfString = getExternalData(someParam);
List<Metadata> metadatum = db1Dao.get(someInputs);
... I do something to generate a list of new class `A` based on listOfString & metadatum ...
try {
db2Dao.put(listOfA);
} catch (PutException e){
...
}
}
public List<String> getExternalData(SomeClass someParam){
... do something
return listOfString;
}
}
Now I want to test:
Given a specific listOfString (returned by getExternalData) and a specific metadatum (returned by db1Dao.get):
Will I get the desired listOfA?
Am I able to call db2Dao.put and its input parameter is listOfA?
Particularly, I have hard-coded sample listOfString and metadatum and desired listOfA (and they will be passed via an object MockData, see the following code) but I don't know how to write the test using Mockito. The following is a test class I wrote but it does not work:
class TestClass extends BaseTest {
#Mock
private Db1Dao db1Dao;
#Mock
private Db2Dao db2Dao;
private ExecuteClass executeClass;
#BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
executeClass = new ExecuteClass(db1Dao, db2Dao);
}
#ParameterizedTest
#MethodSource("MockDataProvider")
public void executeClassTest(final MockData mockData) throws PutException {
Mockito.when(db1Dao.get(Mockito.any(), ...))
.thenReturn(mockData.getMetadatum());
ExecuteClass executeClassSpy = Mockito.spy(executeClass);
Mockito.when(executeClassSpy.getExternalData(Mockito.any()))
.thenReturn(mockData.getListOfString());
executeClassSpy.execute();
// executeClass.execute(); not working neither...
List<A> listOfA = mockData.getDesiredListOfA();
Mockito.verify(db2Dao).put(listOfA);
}
}
Could anyone please let me know? Thank you in advance!!
You should not create a spy of the same class you want to test. Instead, try to write a unit test for the smallest amount of code (e.g. a public method) and mock every external operator (in your case Db1Dao and Db2Dao).
If testing a public method involves calling another public method of the same class, make sure to mock everything inside the other public method (in your case getExternalData). Otherwise, this other public method might be a good candidate for an extra class to have clear separation of concerns.
So, remove the ExecuteClass executeClassSpy = Mockito.spy(executeClass); and make sure you setup everything with Mockito what's called within getExternalData.
To now actually, verify that Db2Dao was called with the correct parameter, either use your current approach with verifying the payload. But here it's important to 100% create the same data structure you get while executing your application code.
Another solution would be to use Mockito's #Captor. This allows you to capture the value of why verifying the invocation of a mock. Later on, you can also write assertions on the captured value:
#Captor
private ArgumentCaptor<ClassOfListOfA> argumentCaptor;
#Test
public void yourTest() {
Mockito.verify(db2Dao).put(argumentCaptor.capture());
assertEquals("StringValue", argumentCaptur.getValue().getWhateverGetterYouHave);
}
The following code worked for me.
I partially accepted #rieckpil's answer. I used #Captor which is very handy.
The reason I had to mock getExternalData() is because its implementation is still a "TODO".
class TestClass extends BaseTest {
#Mock
private Db1Dao db1Dao;
#Mock
private Db2Dao db2Dao;
#Captor
private ArgumentCaptor<List<A>> argumentCaptor;
private ExecuteClass executeClass;
#BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
executeClass = new ExecuteClass(db1Dao, db2Dao);
}
#ParameterizedTest
#MethodSource("MockDataProvider")
public void executeClassTest(final MockData mockData) throws PutException {
Mockito.when(db1Dao.get(Mockito.any(), ...))
.thenReturn(mockData.getMetadatum());
ExecuteClass executeClassSpy = Mockito.spy(executeClass);
Mockito.when(executeClassSpy.getExternalData(Mockito.any()))
.thenReturn(mockData.getListOfString());
executeClassSpy.execute();
List<A> listOfA = mockData.getDesiredListOfA();
Mockito.verify(db2Dao).put(argumentCaptor.capture());
assertEquals(listOfA, argumentCaptor.getValue());
}
}
I will try to be as clear as possible. I have an object ButtonData which have setters and getters. In class InfoController I create an object and, set data to ButtonData. I want to get this data in another class Controllerand use already stored data in class ButtonData, but I can't create a new Object and get information, because it will be clear. How when access to data which is stored in ButtonData?
InfoController:
#FXML
void onSaveClick(ActionEvent event) {
if(parseInputs()) {
ButtonData setData = new ButtonData();
setData.setCode(code);
setData.setCompany(company);
setData.setCountry(country);
}
}
Controller (here I want to get data):
#FXML
void addFlag(ActionEvent event) {
ButtonData setData = new ButtonData(); //CAN'T DO LIKE THIS!
addingFlag.pinFlag(imageView, anchPane);
addingFlag.setButtonID(setData.getCode());
String getCompany = setData.getCompany();
}
I would like to override some Liferay's modules tranlations. I am fallowing: https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/overriding-a-modules-language-keys
It works but not for all strings. First of all I would like to override some strings in journal-lang module (com.liferay.journal.lang), but this module doesn't have servlet context name. I have tried to skip that but it doesn't work. How can I override these strings?
I'm also trying to override some core strings (from portal-impl) but some of them remains untranslated. For example "Add Field" (add-field) from defining new form view. Any possible solutions?
journal-lang is a language components. In order to override some string from them you have to create a component for bundle com.liferay.journal.web or com.liferay.journal.service.
You've to create a CustomResourceBundle with extends ResourceBundle
#Component(immediate = true, property = { "language.id=en_US" }, service = ResourceBundle.class)
public class DefaultCustomResourceBundle extends ResourceBundle {
#Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
#Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle("content.Language", UTF8Control.INSTANCE);
}
And this should override translations accross the portal.
QueueItems[] items = ....
how i can mock object for QueueItems[] using mockito?
i tried : ArrayList workItems = mock(ArrayList.class);
QueueItems = mock(QueueItems.class);
but not working.
If you want to customly create an array of items, you should create such an array and then insert mocks into the array. You should not try to mock an array, as this is core Java implementation.
Also, Lists are better to use as they allow more flexibility. An example:
#RunWith(MockitoJUnitRunner.class)
public class TestClass {
#Mock
private QueueItem item1;
private List<QueueItem> items;
#Before
public void setUp() {
items = new ArrayList<QueueItem>();
// Your mocked QueueItem is the first entry in the list
items.add(item1);
}
#Test
public void simpleTest() {
invokeSomeMethodWithList(items);
Mockito.verify(item1).someMethod();
}
}