I am trying to use the String returned by this method in another class.
This is the method:
public String toString(){ return String.format("(%f, %f, %f)",longitude, latitude, elevation);}
This is the other class method:
void addPoint(){
coordinates.add(Point.toSting);
}
I know that I could create an object, but neither of these classes has a main method.
Are you looking for something like this?
Your Main Class which runs everything
public class Test {
private void run() {
new Test2();
}
public static void main(String[] args) {
Test t = new Test();
t.run();
}
}
Some Class which gets string from another class
public class Test2 {
private void displayString() {
System.out.println(new Test3().toString());
}
Test2() {
displayString();
}
}
Final Class which contains String
public class Test3 {
public String toString() {
return "Hi";
}
Test3() {
}
}
Related
I would like to know if it is possible from a child class to get caller attributes. Follow a sample.
TestClass1.class
package test;
public class TestClass1 {
private String myTestClass1Var = "xxxxx";
public static void main(String[] args) {
new TestClass1().class2();
}
private void class2() {
new TestClass2().getClass1Attributes();
}
}
TestClass2.class
package test;
public class TestClass2 {
public void getClass1Attributes() {
//is something like this possible?
getCaller().myTestClass1Var
}
}
Thanks
Below is my class which has static methods.. one with void and another with String as return types.. Using PowerMockito.spy() to doNothing for void method is working.. But for String method it is not working.. Any help/suggestion would be appreciated..
****************************************************************************
public class ServiceImpl {
public static String getName(){
// some business logic
Utils.doSomething();
String result = Utils.getName();
return result;
}
}
****************************************************************************
public class Utils {
public static void doSomething(){
// some DB business logic
}
public static String getName(){
// some business logic
return "Static String";
}
}
****************************************************************************
PowerMockito.spy(Utils.class);
PowerMockito.doNothing().when(Utils.class); // working
PowerMockito.when(Utils.getName()).thenReturn("TESTER"); // not working
It works like this for me:
#RunWith(PowerMockRunner.class)
#PrepareForTest(Utils.class)
public class SimpleTest {
#Test
public void test() throws Exception {
PowerMockito.spy(Utils.class);
PowerMockito.doNothing().when(Utils.class, "doSomething"); // add method name
PowerMockito.when(Utils.getName()).thenReturn("TESTER");
assertEquals("TESTER", ServiceImpl.getName());
}
}
I have following class:
public class SomeClass {
public static void main(String[] args) {
//
}
private static void test() {
MyClass myClass = new MyClass(); //Doesn't work
myClass.print(); //Doesn't work
class MyClass {
void print() {
System.out.println("Some text");
}
}
}
}
How to create an object of type MyClass? Both lines produce compilation error.
You need to declare the class first before being able to use it:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
class MyClass {
void print() {
System.out.println("Some text");
}
}
MyClass myClass = new MyClass();
myClass.print();
}
If you are using Java version before then 11 then you can't define class inside method and you need to try to define it in scope of SomeClass and then access it through the SomeClass:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
MyClass myClass = new SomeClass().new MyClass(); // Doesn't work
myClass.print(); // Doesn't work
}
class MyClass {
void print() {
System.out.println("Some text");
}
}
}
However, if you are using Java 11+ you need to change the order of your code inside the test method to define the class first and access it next:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
class MyClass {
void print() {
System.out.println("Some text");
}
}
MyClass myClass = new MyClass(); // Doesn't work
myClass.print(); // Doesn't work
}
}
Suppose I have a class named Util with static fields:
public class Util {
public static field = Param.getValue("param1");
}
and the class Param look like this:
public class Param {
public static field = SomeClass.getValue("someValue");
}
I want to mock and stubb Param.getValue("param1") inside Util, but at the same time I want suppress static initialization for Param class. How can I achieve this?
This is my first attempt but it's not working
#RunWith(PowerMockRunner.class)
#PrepareForTest({Param.class})
#SuppressStaticInitializationFor("py.com.company.Param")
public class Test {
#Test
public void testSomeMethod() {
PowerMockito.mockStatic(Param.class);
when(Param.getValue("value1")).thenReturn("someValue1");
}
}
This is working for me. I get no output, and SomeClass#getValue if no #SuppressStaticInitializationFor:
#RunWith(PowerMockRunner.class)
#SuppressStaticInitializationFor({"so35047166.Param"})
#PrepareForTest({Param.class})
public class UtilTest {
#Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Param.class);
}
#Test
public void testFoo() throws Exception {
final Util util = new Util();
assertEquals("Util#foo", util.foo());
assertEquals(null, Util.field);
}
}
with:
// all in package so35047166;
public class Util {
public static String field = Param.getValue("param1");
public String foo() {
return "Util#foo";
}
}
public class Param {
public static String field = SomeClass.getValue("someValue");
public static String getValue(final String in) {
System.out.println("Param#getValue");
return "Param#getValue";
}
}
public class SomeClass {
public static String getValue(final String in) {
System.out.println("SomeClass#getValue");
return "SomeClass#getValue";
}
}
wondering how it is possible to call public m method?
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("m");
}
};
}
}
I don't believe you can. You'd have to create an interface or subclass. (Well, okay, that's probably not true. You could probably do it with reflection.)
E.g., like this (where you call it via test.m() after construction):
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
test.m();
}
private static abstract class SubTest1 extends Test1 {
public abstract void m();
}
}
Or like this, where it happens during construction:
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
}
private static abstract class SubTest1 extends Test1 {
public SubTest1() {
this.m();
}
public abstract void m();
}
}
You can't define an anonymous class constructor, so that last uses the constructor of the SubTest1 class and the abstract method.
You cannot directly invoke m since test is of type Test1 which does not contain a method called m, but you should never find yourself in a situation like this. The whole point of anonymous classes is to alter some already-existent aspect of the base class's functionality, so adding new methods makes no sense. Consider rethinking your design or using a named class instead.
Of course, if you won't care about test in the future you could do this:
new Test1() {
public void m() {
System.out.println("m");
}
}.m();
Although you would rarely want to do something like this, it could be useful if you're working with Thread or Runnable and need to invoke the run method.
If Test1 had a method called "m" you could just call test.m() after you instantiated the inner class:
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("New Behavior");
}
};
test.m();
}
public void m() {
System.out.println ("Default Behavior");
}
}
Running this would output:
New Behavior