How to write test case for the given code using powermockito - junit4

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 .

Related

Automatically generate constructor in eclipse?

suppose i have the following class
public class foo{
String a;
String b;
String c;
String d;
... // more Variables
public String getA(){
return a;
}
public void setA(String a){
this.a = a;
}
... // more getters and setters
}
what is the easiest way in eclipse to generate the following constructor
public foo (String a, String b, String c, ...){
this.a = a;
this.b = b;
this.c = c;
...
}
Source > Generate Constructor using Fields... (Alt+Shift+S, O)
Create a model class complete with getters/setters and constructors in 35 seconds using the 'Source' menu (with shortcuts!). #EclipseTips

Using setters and getters between 3 classes

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

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

nunit/mbunit parameterized SetUp

I'd like to provide parameterized data for the setup of a test. Something like this:
[TestCase("1", "2")
[TestCase("a", "b")
public class TestFixture
{
[SetUp]
public void SetUp(string x, string y)
{
Console.WriteLine("Setup with {0}, {1}", x, y);
}
[Test]
public void Test() {...}
}
But I can only manage to get parameters passed to the testcase itself.
I prefer solutions for either nunit or mbunit but open for alternatives (yet to settle on which test framework to use). This is ultimately for a set of Selenium system-tests where the setup creates a browser session.
[edit]
Using NUnit I can get this working:
[TestFixture("1", "2")]
[TestFixture("a", "b")]
public class Tests
{
private string _x;
private string _y;
public Tests(string x, string y)
{
_x = x;
_y = y;
}
[SetUp]
public void SetUp()
{
Console.WriteLine("Setup with {0}, {1}", _x, _y);
}
[Test]
public void Test()
{
}
}
[/edit]
That seems to suit my needs, but I'll leave the question open for a few days to see if alternatives are suggested.
Using NUnit I can get this working:
[TestFixture("1", "2")]
[TestFixture("a", "b")]
public class Tests
{
private string _x;
private string _y;
public Tests(string x, string y)
{
_x = x;
_y = y;
}
[SetUp]
public void SetUp()
{
Console.WriteLine("Setup with {0}, {1}", _x, _y);
}
[Test]
public void Test()
{
}
}

is it right way to use interface?

how interface knows which class method to call?
it is the correct code? or not
namespace IntExample
{
interface Iinterface
{
void add();
void sub();
}
public partial class Form1 : Form,Iinterface
{
public Form1()
{
InitializeComponent();
}
public void add()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a + b;
txtresult.Text = c.ToString();
}
public void sub()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a - b;
txtresult.Text = c.ToString();
}
private void btnadd_Click(object sender, EventArgs e)
{
add();
}
private void button2_Click(object sender, EventArgs e)
{
sub();
}
class cl2 : Form1,Iinterface
{
public void add()
{
int a, b, c;
a = Convert.ToInt32(txtnum1.Text);
b = Convert.ToInt32(txtnum2.Text);
c = a + b;
txtresult.Text = c.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
Interface does not "know" which class method to call. It just defines what methods are available.
The code you posted would not compile since cl2 does not implement sub method but it's pretty much meaningless anyway.
I have no idea what you are trying to do so I will give example for valid usage of the interface.
You can have several forms implementing that interface, then in your main form you can choose which of those forms to show according to index or name.
So, to store all the forms you can use generic list:
List<Iinterface> forms = new List<Iinterface>();
Adding to the list all the forms implementing the interface:
forms.Add(new Form1());
forms.Add(new Form2());
forms.Add(new Form3());
//...
Then you can show specific form and call a method from the interface:
//find by index:
forms[index].Show();
forms[index].add();
//find by name:
string name="form 2";
Iinterface form = forms.Find(f => f.Name == name);
if (form != null)
{
form.Show();
form.add();
}