Java: Calling in main implemented Interface Methods from a jUnit - Test - java

I have the following function:
public static String s(B b) {
int t = b.t();
String r ="Hello ";
for(String z:s) {
boolean x=b.f(t,5);
if(x) {
r+=z;
}
}
return r;
}
Which takes in B - Interface
The Interface B - Methods int t() and boolean f(int a, int b) were implemented in the same class within main as the following:
public static void main(String[] args) {
A.s(new B() { //A - Class
#Override //B - Interface
public int t() {
return 15;
}
#Override
public boolean f(int a, int b) {
return true;
}
});
}
Issue: How can i test the public static String s(B b) - function from a jUnit - test when the function asks for a interface as a parameter, when the interface methods were implemented in main?
The Class is called A, Interface: B

When you want to test your s() method you can provide any reference to an object which implements the B interface. You can define an anonymous class which implements the interface as you did in your main() method. Or you can define a "normal" class which implements the interface as well. So you can write something like this:
public class Whatever implements B
{
/* your methods from B */
}
Then you use this class like any other class inside your unit test:
#Test
public void checkSomething() {
String result = A.s(new Whatever());
Assertions.assertEquals("my string", result);
}

Related

The Test class require the method from a concrete class to be static

The test class requires a method to be defined as static in the concrete class. But the concrete class implements a method from an interface.
The interface does not allow the implemented method to be static.
Interface:
public interface ArithmeticSkeleton {
public int operation(int a, int b);
}
Concrete Class
public class Divide implements ArithmeticSkeleton{
public int operation(int a, int b) {
return (a / b);
}
}
jUnit test case:
public class ArithmeticSkeletontest {
private ArithmeticSkeleton as;
#Test
public void testDivision() throws Exception {
assertEquals("5", Divide.operation(10, 2));
}
}
However, the Test code does not allow Divide.operation to be accessed.
You need to initialize object of class Divide to access its methods:
public void testDivision() throws Exception {
Divide divide = new Divide();
assertEquals(5, divide.operation(10, 2));
// you need to change "5" to 5 to pass this test
}
The operation method is not static. Therefor you must instantiate an object of the Divide class like this in your test
#Test
public void testDivision() throws Exception {
assertEquals("5", new Divide().operation(10, 2));
}

How do I implement two abstract methods in Java using lambdas?

I have a Functional Interface in the code below which has one abstract method and one object method override. So when I write Lambda expression for that , how can I implement my equals method.
import static java.lang.System.out;
public class Test {
public static void main(String[] args) {
AddToString test = a -> (a + " End");
out.println(test.stringManipulation("some string"));
out.println(test.increment(5));
out.println(test.equals(null));
}
}
#FunctionalInterface
interface AddToString {
String stringManipulation(String a);
default int increment(int a) { return a+1; }
#Override
public boolean equals(Object obj);
}
One way to do that is to create Anonymous class like given below, but is there a better method using lambda expressions -
public class Test {
public static void main(String[] args) {
AddToString test = new AddToString() {
public String stringManipulation(String a) {
return a + " End";
}
#Override
public boolean equals(Object Obj) {
//Just testing whether it overrides
return 5==5;
}
};
out.println(test.stringManipulation("some string"));
out.println(test.increment(5));
out.println(test.equals(null));
}
}
You can't. If you need to override equals, you'll need to create a class (anonymous or otherwise), you can't do it with a lambda.

How to use Google Guice to inject a dependancy

I've gone through the user guide and everything but yet I still don't understand exactly how to modify existing code to use Google Guice when trying to inject dependencies. So to make it easier I created this simple example and if someone could explain with this simple example I would really appreciate it!
Say I have a
public Class A {
private int count = 0;
public A() {
}
public int getCount() {
return count;
}
public void setCount(int newCount) {
this.count = newCount;
}
}
and another class
public Class B {
private A objectA;
public B() {
objectA = new A();
}
public void messWithCount() {
int tempCount = objectA.getCount();
objectA.setCount(tempCount+1);
}
}
So basically my question is: how would I go about using Google Guice to extract creation of objectA in the constructor B() and instead inject it as a dependency in Class B where it would amount to something like
#Inject
public B() {
}
and how would I actually inject an instance of A into it?
First, B should not be bound to class A but rather use an interface (such as AInterface).
The main point of Guice is to bind different implementations of the same interface, without being tied to some class.
So let's assume Class A implements AInterface
interface AInterface {
public int getCount();
public void setCount(int newCount);
}
class A implements AInterface {
private int count = 0;
public A() {
System.out.println("done!");
}
#Override
public int getCount() {
return count;
}
#Override
public void setCount(int newCount) {
this.count = newCount;
}
}
Now you tell it to inject your variable:
class B {
#Inject
private AInterface objectA;
public B() {}
public void messWithCount() {
int tempCount = objectA.getCount();
objectA.setCount(tempCount + 1);
}
}
I removed the static modifier, but if you insist in having it static you'd need to bind using requestStaticInjection instead
you tie the implementation A to the interface AInterface in a special class called module:
class SimpleModule extends AbstractModule {
#Override
protected void configure() {
bind(AInterface.class).to(A.class);
}
}
Now you ask Guice to generate B for you.
public class Temptemp {
public static void main(String[] args) {
Injector i = Guice.createInjector(new SimpleModule());
B b = i.getInstance(B.class);
}
}
You can inject A into B in two ways, actually many ways but with in the context of your question I would say two.
Make sure both A and B class is configured in a Module. Follow condit example code/class that extends AbstractModule.
1.a
class B {
#Inject
private A a;
public B() {
}
}
1.b
class B {
private A a;
#Inject
public B(A a) {
this.a = a;
}
}
These both works fine but 1.b is useful if you want to write test for class B. Where your test will mock A class and creates instance of B. like
class BTest {
#Test
public void testSomeMethodOfB() {
A a = mock(A.class);
B b = new B(a);
//run some test on b;
}
}
Here's an example based on what you already have:
public class GuiceExample {
static class A {
private int count = 0;
public A() {}
public int getCount() {
return count;
}
public void setCount(int newCount) {
this.count = newCount;
}
}
static class B {
#Inject
private static A objectA;
public B() {}
public void messWithCount() {
int tempCount = objectA.getCount();
objectA.setCount(tempCount+1);
}
}
static class Module extends AbstractModule {
#Override
protected void configure() {
requestStaticInjection(B.class);
}
}
#Test
public void test() {
Injector i = Guice.createInjector(new Module());
B b = i.getInstance(B.class);
//Do something with b
}
}
Note, though, that static injection is not preferred. You could make A non static and Guice will still inject the field. The even more "correct" way would be to drop the requestStaticInjection call and add A as construction argument like:
static class B {
private A objectA;
#Inject
public B(A objectA) {
this.objectA = objectA;
}
...
}

Replace empty method with method from a class where it is used

In the following scenario, I want to replace Class B's (similarly D, E, F, etc.) method doSomething() with the method in Class A where it will be used. How would I go about this? Made up some example, hope it gets the message across
public class B implements GetNames{
public void getNameA(){ return "NameA"; }
public void getNameB() { return "NameB"; }
public void doStuff(){
//print names
doSomething(getNameA(), getNameB());
//print names
}
public void doSomething(String a, String b){}
}
public class A{
public void someMethod(){
B b = new B();
b.doStuff(); //*So I want it to call the method in B but somehow replace the doSomething method in B with the doSomething method in A
}
public void doSomething(String a, String b){
//print 'blabla' + a
//print 'blablabla' + b
//concatenate and print
}
}
Made abstract class A implement interface GetNames and then extend it in class B:
public abstract class A implements GetNames {
public void doSomething(String a, String b){
//print 'blabla' + a
//print 'blablabla' + b
//concatenate and print
}
}
public class B extends A {
public void getNameA(){ return "NameA"; }
public void getNameB() { return "NameB"; }
public void doStuff(){
// class A's doSomething will be called
doSomething(getNameA(), getNameB());
//print names
}
}
Class A should extend class B. If you make B an abstract class, the B.java file would look something like this:
public abstract class B {
...
public abstract void doSomething(String a, String b);
...
}
An abstract class has some functionality, like the getNameA method, which is already defined, but other methods like doSomething are left to its subclasses to implement.
Change class A to read:
public class A extends B {
...
#Override
public void doSomething(String a, String b) {
// custom behaviour
}
}
If what you want is to just make an instance of class B that has a different implementation of the method doSomething then what you could do is this:
B myBInstance = new B() {
#Override
public void doSomething(String a, String b) {
// custom behaviour here
}
};
myBInstance.doStuff();
Style-wise and design-wise though, this is only a quick-and-dirty way to define behaviour for a one-time use of B.

String concatenation in inherited class

I have a class called A and there's a String declared in it. And i have 2 other classes B and C which is inherited from A
public abstract class A {
protected String ss="";
public abstract String someMethod();
}
public class B extends A{
public String someMethod(){
int i=8;
return ss+="$"+i;
}
}
public class C extends A {
public String someMethod() {
int i=9;
return ss+="$"+i;
}
}
Test Code:
A aa = new B();
aa.someMethod();
A aaa = new C();
aaa.someMethod();
When I print aaa.someMethod(); - why haven't the strings from class B and C been appended? I want them to be appended. How can I do this ?
/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac A.java B.java C.java Test.java
/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test
$8
$9
nothing surprising here, B method someMethod() calls B method, C method someMethod() calls C method...
file A.java:
public abstract class A
{
protected String ss="";
public abstract String someMethod();
}
file B.java
public class B extends A
{
public String someMethod()
{
int i=8;
return ss+="$"+i;
}
}
file C.java
public class C extends A
{
public String someMethod()
{
int i=9;
return ss+="$"+i;
}
}
file Test.java
public class Test
{
public static void main(String pArgs[])
{
A aa = new B();
System.out.println(aa.someMethod());
A aaa = new C();
System.out.println(aaa.someMethod());
}
}
Overridden methods in Java do not automatically invoke their superclass parents. So, in your C subclass, calling someMethod does not invoke the method from its parent A, unless you explicitly call super.
public class C extends A
public String someMethod(){
int i=9;
return ss+= super.someMethod()+"$"+i;
}
}
I assume you are doing this to learn, because otherwise this is a pretty terrible way to manage your inherited classes and their properties.

Categories