Using setters and getters between 3 classes - class

I know this is a basic question but I really struggle with this :(
First class:
public class A{
C c= new C();
B b= new B();
public static void main(String[] args) {
b.start();
System.out.println(c.getSomething());
}
}
Second Class:
public class B{
C c= new C();
public void start(){
c.setSomething(2);
}
}
Third Class:
public class C{
int x;
public int getSomething() {
return x;
}
public void setSomething(int x) {
this.x = x;
}
}
Now I know I make a new object in class A thats why the sysout returns null.
How can I make it so that I GET the value 2 instead of null in class A, and that I'm able to SET things in class B.
So I stay at the same object so to say.
I just want to be able to SET things in class B and GET that same value from the setter-getter-class-C, in class A. Please help
Thanks in advance, Jimme

Related

encounter a crash with generic function in unity5.3/5.4

I want simulate a functional programming Option class with map and flatMap but It can't work in Unity engine. as a example, I create three .cs file as follow:
1 .NET 2.0 default delegate is't covariance and contravariance, so I override it.
using System;
namespace Functional
{
public delegate R Func<out R>();
public delegate R Func<in A, out R>(A a);
public delegate R Func<in A, in B, out R>(A a, B b);
public delegate R Func<in A, in B, in C, out R>(A a, B b, C c);
public delegate R Func<in A, in B, in C, in D, out R>(A a, B b, C c, D d);
public delegate R Func<in A, in B, in C, in D, in E, out R>(A a, B b, C c, D d, E e);
public delegate void Action<in A>(A a);
}
2 define Option classes
using UnityEngine;
using System.Collections;
using Functional;
namespace Lorance.RxScoket.Util {
public interface IOption<out T> {
void Foreach (Action<T> func);
IOption<R> Map<R> (Func<T, R> func);
IOption<R> FlatMap<R> (Func<T, IOption<R>> func);
bool IsEmpty();
T Get ();
}
public static class IOptionEx {
public static T GetOrElse<T> (this IOption<T> opt, Func<T> defaultValue) {
return opt.IsEmpty() ? defaultValue() : opt.Get();
}
}
public abstract class Option<T>: IOption<T> {
public abstract bool IsEmpty ();
public abstract T Get ();
public void Foreach (Action<T> act) {
if (!IsEmpty ()) {
act (Get());
}
}
public IOption<R> Map<R>(Func<T, R> func) {
if (IsEmpty ())
return new None<R> ();
else
return new Some<R>(func (Get()));
}
public IOption<R> FlatMap<R>(Func<T, IOption<R>> func) {
if (IsEmpty ())
return new None<R> ();
else
return func (Get());
}
public static Option<T> apply(T value){
if (value == null)
return new None<T> ();
else
return new Some<T> (value);
}
public static Option<T> Empty = new None<T>();
}
public class Some<T> : Option<T> {
T value;
public Some(T value){
this.value = value;
}
public override bool IsEmpty() {return false;}
public override T Get() {
return value;
}
}
public class None<T> : Option<T> {
public override bool IsEmpty() {return true;}
public override T Get() {
throw new NoSuchElementException ("None.Get()");
}
}
public class NoSuchElementException : System.SystemException {
public NoSuchElementException(string message)
: base(message)
{
}
}
}
3 crash example
using UnityEngine;
using System.Collections;
using Lorance.RxScoket.Util;
public class TestFlatMap : MonoBehaviour {
// Use this for initialization
void Start () {
Dog d = new Dog ();
IOption<Dog> dogOpt = new Some<Dog> (d);
//ok
Dog dog = dogOpt.GetOrElse<Dog> (() => new Dog());
//crash at this point. does it care about polymorphism? but it works on pure .net project
Animal animal = dogOpt.GetOrElse<Animal> (() => new Cat());
print ("r2 -" + animal);
}
}
public class Animal {}
public class Dog: Animal {}
public class Cat: Animal {}
Does someone also meets the problem and how to solve it?
a complete crash example at : https://github.com/Unlimited-Works/crash-eg
Get the crash with this step:
1. Open the project with unity editor.
2. Open scene 'eg.unity'.
3. Press play.
4. result: Unity crashes.
it crashes when executing 'TestFlapMap.cs':
Animal animal = dogOpt.GetOrElse (() => new Cat());
if you also encounter the promblem please give me a vote at unity issues, it has delay one month ago and not any response from Unity official.
thanks

Differences between class method and object method

In C#, why when we can define a class method and access to a method by class name directly, we should/some people define an object instead and then create an instance of that object?
Class1:
class Class1
{
public static int PrintX(int x)
{
return x;
}
private int y;
public int PrintY(int z)
{
return this.y = z;
}
}
Main Method:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class1.PrintX(9));
Class1 newClass = new Class1();
Console.WriteLine(newClass.PrintY(9));
}
}
Both ways print out 9. Why should I use an object method and then create an instance of it?!
If you have to ask the question, then you shouldn't (use an object method). However, if you create two classes, they will affect each other.
Class1 newClassA = new Class1();
Class1 newClassB = new Class1();
Console.WriteLine(newClassA.PrintY(1));
Console.WriteLine(newClassB.PrintY(9));
After this code, newClassA's y is 9.

How to write test case for the given code using powermockito

I just want to know if i have a code like -
public class A {
static String b = "hello";
public static String a() {
String b = b();
String d = d(b);
String e = e(d, b);
return e;
}
public static String b() {
return b;
}
public static void c(String c) {
b = c;
}
public static String d(String d) {
return d;
}
public static String e(String e1, String e2) {
return e1 + e2;
}
}
than how will i write the test cases for this using power mockito since all the methods are static and a method is even void too.
The first thing i got is that i only need to write the test case for method a since it is using all the other methods so all others will also get cover in it.
Can anybody help me and tell me how i can test this .

Junit using Mock Objects

MyClass{
public void myfunction(){
AnotherClass c=new AnotherClass();
c.somethod();//This method sets some values of the AnotherClass object c;
}
}
I have the above scenario to be tested.How do I check if the value of AnotherClass Object c is set properly.I understand that I have to use Mock Objects for these.But couldn't figure out how because here I cannot pass the mock object of AnotherClass to myfunction because of the above design.Can anybody help me?
if you really want to do this should do a redesign like follows (as Dan has also suggested)
import org.junit.Test;
import org.mockito.Mockito;
public class TestingMock {
#Test
public void test() {
MyClass target = Mockito.spy(new MyClass());
AnotherClass anotherClassValue = Mockito.spy(new AnotherClass());
Mockito.when(target.createInstance()).thenReturn(anotherClassValue);
target.myfunction();
Mockito.verify(anotherClassValue).somethod();
}
public static class MyClass {
public void myfunction(){
AnotherClass c = createInstance();
c.somethod();//This method sets some values of the AnotherClass object c;
}
protected AnotherClass createInstance() {
return new AnotherClass();
}
}
public static class AnotherClass {
public void somethod() {
}
}
}
You will see that commenting out c.somethod() makes the test fail.
I'm using Mockito.

Testing mocked objects rhino mocks

I am new to RhinoMocks, and I am trying to write a test as shown
I have classes like these
public class A
{
public void methodA(){}
}
public class B
{
public void methodB(A a)
{
a.methodA();
}
}
And i am trying to test it like this
A a = MockRepository.GenerateMock<A>();
public void ShouldTest()
{
B b = new B();
b.methodB(a);
a.AssertWasCalled(x=>x.methodA());
a.VerifyAllExpectations();
}
But it is giving the error as shown:
System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call.
How do I test methodB then?? Can someone help??
Rhino mock creates proxy class when you call MockRepository.Generate *** method. This means that it extends your type. If you don't declare any abstraction you cannot make any derivation which is essential in any mocking framework.
You can do two things
Create an interface (better design)
Make the member virtual (this will allow RhinoMocks to derive from your type and create a proxy for the virtual member
Sample code
public interface IA { void methodA();}
public class A:IA{public void methodA() { }}
public class B
{
public void methodB(IA a)
{
a.methodA();
}
}
[TestFixture]
public class Bar
{
[Test]
public void BarTest()
{
//Arrange
var repo = MockRepository.GenerateMock<IA>();
//Act
B b = new B();
b.methodB(repo);
//Assert
repo.AssertWasCalled(a => a.methodA());
repo.VerifyAllExpectations();
}
}
You have concrete classes with no virtual methods and no interfaces. You can't mock anything.
Update:
Here's one way to do it:
public interface IA
{
void methodA();
}
public class A : IA
{
public void methodA(){}
}
public class B
{
public void methodB(IA a)
{
a.methodA();
}
}
Then use
IA a = MockRepository.GenerateMock<IA>();