Gauge - run #BeforeSpec in a superclass in Java - java

I am writing a testing framework using Gauge.
I want some initilization logic performed in one class, and the steps logic to reuse it, like this:
public class A {
protected String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B extends A {
#Step("...")
public void verifyProperty() {
assertEquals(property, "hello");
}
}
I can't seem to be able to achieve this. When performing the steps, the "property" is always null.
Placing the #BeforeSpec in class B and calling super.init() works, but I would like to avoid having this call in every test class that extends A.
Has anyone encountered and solved such an issue?

Try to use a static variable:
public class A {
public static String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B {
#Step("...")
public void verifyProperty() {
assertEquals(A.property, "hello");
}
}

Related

How to pass string to separate java.class

I'm trying to pass a string from my main activity to a separate class
that does not have a activity running.
I've looked into passing variables with intent and bundles but what i've read they use two activities
I've found a video of something close to what i'm trying to do but in reverse and can't get it to work. (https://www.youtube.com/watch?v=CSifkubnE-E)
Now the string changes so I can't use static
and my second.java has no context to pass to.
below is a basic representation of what i'd would like to do.
main.java
import second
public class Main extends Activity {
String mystring = "variable"
//mystring changes depending on the user
mystring = "userchangedvariable"
}
second.java
public class dosomething(){
String localvar;
localvar = mystring
}
To be clear as possible I want to pass a variable from the main.java to the second.java that has no context. I don't want to add the second.java class to my main.java, I want to keep them separate(some of the things I read say merge them). How can I do this?
I did not get the following statement.
Now the string changes so I can't use static
You can update the static values. You cannot update final values. Also, you need to somehow create a connection. You can create another class and share the static variables
class ThirdClass {
public static String sharedString;
}
class Main {
ThirdClass.sharedString = "somevalue";
}
class Second {
localVar = ThirdClass.sharedString;
}
You can do it in those ways:
class Activity {
onCreate() {
String stringToPass = "TEST";
Example example = new Example(stringToPass);
}
}
class Example {
private String stringToPass;
public Example(String stringToPass) {
this.stringToPass = stringToPass;
}
}
or
class Activity {
onCreate() {
String stringToPass = "TEST";
Example example = new Example();
example.setStringToPass(stringToPass);
}
}
class Example {
private String stringToPass;
public void setStringToPass(String stringToPass) {
this.stringToPass = stringToPass;
}
public Example() {
}
}
or
class Activity {
onCreate() {
String stringToPass = "TEST";
Example.stringToPass = stringToPass;
}
}
static class Example {
public static String stringToPass;
}
or (not the advised way)
class Activity {
onCreate() {
String stringToPass = "TEST";
Example example = new Example();
example.stringToPass = stringToPass;
}
}
class Example {
public String stringToPass;
public Example() {
}
}
If you create a new object and the string is required for creating -> great make it as a requirement in the constructor. (first version)
If you create a new object and the string is not required for creating -> great make a property (second version)
Third version is needed more rarely (you can set the string without having to create an object) and the fourth version should be avoided completely in Java.
In the main .java file, add the following:
second example = new second("variable");
This can then be referenced anywhere inside your main method. For example:
example.setString("variable);
Then, inside your second .java file, you'll need to add the following:
public class second
{
private String variable;
public void setString(String pass)
{
variable = pass
}
}
This way anything you pass to the example variable inside your main .java file
will be passed over to the setString method.

Print statement not called in anonymous class passed to class constructor

I have two classes and one interface.
Interface:
public interface MyBirthdayEvent {
void itsMyBirthday();
}
First class:
public class MyBirthdayButton
{
public void addOnClickedListener(MyBirthdayEvent mbe){}
}
Second class:
public class MyBirthday {
private MyBirthdayButton myBirthdayButton = new MyBirthdayButton();
MyBirthday() {
myBirthdayButton.addOnClickedListener(new MyBirthdayEvent() {
public void itsMyBirthday() {
System.out.println("Happy Birthday");
}
});
}
}
Then in main, I have this:
public class TestThisStuff {
public static void main(String[] args) {
MyBirthday myBirthday = new MyBirthday();
}
}
As can be seen from the code, I am using an anonymous class in the MyBirthday constructor. In doing so, I am trying to get the string "Happy Birthday" to print to the console.
My problem is, when I call the MyBirthday constructor in main by making a new myBirthday object, I am not seeing the string "Happy Birthday" print to the console. Shouldn't it print to the console? If not, what I am doing wrong?
What you can do is this:
public interface MyBirthdayEvent {
void itsMyBirthday();
default void invoke() {
itsMyBirthday();
}
}
...
public class MyBirthdayButton
{
public void addOnClickedListener(MyBirthdayEvent mbe){
mbe.invoke();
}
}
...
Also, it will work without it, but use a lambda rather than an anonymous inner class. This looks much better.
MyBirthday() {
myBirthdayButton.addOnClickedListener(() ->
System.out.println("Happy Birthday"));
}
you can move System.out.println("some words")statement to your MyBirthdayEventconstructor
it didn't show in your console because you haven't invoke the method

Accessing a particular instance of class inside the same class in java

I have a java code with the structure that is shown below:
public class x{
public static void main(string[] args)
{
ysample1 = new y(m)
ysample2 = new y(l)
....
}
}
public class y{
private int m_m
public y(int m)
{
m_m = m
}
public void control()
{
h h1 = new h(ysample2)
}
}
At some point when I want to call method control for ysample1 I may need to access ysample2 object.How can I define instance of class y global, so I can access ysample2 inside the control method in class y?
Does anyone know how can I fix this? Thanks.
You can't do what you want to do the way you wrote it.
I think you need to ridefine "control()" method like this:
public void control(Y ysample)
{
h h1 = new h(ysample)
}
So now you need to have an "ysample" as parameter and you can do from your main
control(ysample2);
and you will have what i understood from you question. If you need something else please comment.

Best way to pass a variable from outer class to inner class

I'm building an android app (but this is not important for the post) and I'm writing a method called scrollUntilSelectedAvatar() that contain a class:
public class AvatarManager {
private HorizontalScrollView avatarPageHorizontalScrollView;
//mehod that contains the inner class
public void scrollUntilSelectedAvatar(HorizontalScrollView avatarPageHorizontalScrollView){
this.avatarPageHorizontalScrollView = avatarPageHorizontalScrollView;
avatarPageHorizontalScrollView.post(new Runnable() {
#Override
public void run() {
AvatarManager.this.avatarPageHorizontalScrollView.scrollTo(100, 0);
}
});
}
}
My question is: what's the correct way to access avatarPageHorizontalScrollView (that I pass to the method as an argument) inside the inner class new Runnable().
The way I found (AvatarManager.this.avatarPageHorizontalScrollView) doesn't seems the best way.
Thank everybody for the help :)
The way you did it - is the one and only correct. Syntax of some thing about inner classes may seem quite strange. But it is just like it is.
public class A {
private int a;
private abstract class B {
public abstract void printA();
}
public B makeB() {
return new B() {
#Override
public void printA() {
System.out.println(A.this.a);
}
};
}
}

Get value from different class inside an event

I have two classes:
public jComboBox() {
... // this is a autocomplete jComboBox btw
...
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie) {
if(ie.getStateChange() == 1) {
String selectedItem = (String)getSelectedItem();
randomMethod(selectedItem);
}
}
});
}
private void randomMethod(String selectedItem){
someClass sc = new someClass();
String randomString = selectedItem;
sc.getRandomString(randomString);
}
and
public someClass() {
...
...
}
public void getRandomString(String randomString){
defaultTableModel.setRowCount(0);
.. do-something ..
}
Is this method fine? If not, I need some alternative on this one, because i'm having problems for example, using defaultTableModel.setRowCount(0) because the table wont empty, not unless I put the setRowCount(0) on other methods inside someClass class.
Basic java access specifier stuff..... how are you calling this private method getRandomString from randomMethod()? the visibility of private method of a class is only the class, not anywhere else. Therefore, your following code:
private void randomMethod(String selectedItem){
someClass sc = new someClass();
String randomString = selectedItem;
fs.getRandomString(randomString); // This will not work
}
is not going to work because of the access specifier private. If you can allow the access rights to be specific to the package you have, you can change it to:
protected void getRandromString(String randromString) {...}
Just to demonstrate what I mean:
package com.stackoverflow.solutionmaker;
public class Aclass {
public Aclass(){
somePrivMethod();
}
public void aMethod(){
System.out.println("Can see me from anywehre bcoz I am public");
}
private void somePrivMethod(){
System.out.println("Cannot find me from anywhere because I am private t Aclass");
}
}
Now the runner class:
package com.stackoverflow.solutionmaker;
public class StackOverflowSolutionsRunner {
public static void main(String[] args) {
Aclass aClass = new Aclass(); // It will display"Cannot find me from anywhere because I am private t Aclass"
aClass.aMethod(); // It will display "Can see me from anywehre bcoz I am public
aClass.somePrivMethod(); // Will throw a compile-time error
}
}
A good exercise for you now to compile these two from command line and see what error message you get. Alternatively, using Eclipse smart IDE or Jcreator, you can see that your private access specifier is causing red messages to appear.

Categories

Resources