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.
Related
I want to inherit a class, but I want to make some of its methods private, which allows mutation of the object. I do not want to override those methods, as that will make them still accessible and cause a runtime error. I want something like this:-
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass extends BaseClass {
//...
void read() {/*Implementation*/}
}
int main() {
final first = BaseClass(), second = ChildClass();
first.update(); //Works
second.update(); //Error: Method does not exist.
}
I don't think what you want it possible. The entire point of extending classes is that child classes can access all public methods of the superclass. You might want to use composition instead. That would be something like:
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass {
BaseClass _baseClass
//...
void read() { _baseClass.read() }
}
Single interface: IDoSomething {...}
Two classes implement that interface:
ClassA : IDoSomething {...}
ClassB : IDoSomething {...}
One class uses any of those classes.
public class DummyClass(IDoSomething doSomething) {...}
code without Autofac:
{
....
IDoSomething myProperty;
if (type == "A")
myProperty = new DummyClass (new ClassA());
else
myProperty = new DummyClass (new ClassB());
myProperty.CallSomeMethod();
....
}
Is it possible to implement something like that using Autofac?
Thanks in advance,
What you are looking for is, as I remember, the Strategy Pattern. You may have N implementations of a single interface. As long you register them all, Autofac or any other DI framework should provide them all.
One of the options would be to create a declaration of the property with private setter or only getter inside Interface then implement that property in each of the class. In the class where you need to select the correct implementation, the constructor should have the parameter IEnumerable<ICommon>.
Autofac or any other DI frameworks should inject all possible implementation. After that, you could spin foreach and search for the desired property.
It may look something like this.
public interface ICommon{
string Identifier{get;}
void commonAction();
}
public class A: ICommon{
public string Identifier { get{return "ClassA";} }
public void commonAction()
{
Console.WriteLine("ClassA");
}
}
public class A: ICommon{
public string Identifier { get{return "ClassB";} }
public void commonAction()
{
Console.WriteLine("ClassA");
}
}
public class Action{
private IEnumerable<ICommon> _common;
public Action(IEnumerable<ICommon> common){
_common = common;
}
public void SelectorMethod(){
foreach(var classes in _common){
if(classes.Identifier == "ClassA"){
classes.commonAction();
}
}
}
}
As mention here: if a function in an interface has no body it is abstract by default. But there is nothing about interface's function with body.
Example:
interface MyInterface {
fun foo() { print("Something") }
fun bar()
}
fun main(args: Array<String>) {
println(MyInterface::foo.javaMethod)
println(MyInterface::bar.javaMethod)
}
Output will be:
public abstract void MyInterface.foo()
public abstract void MyInterface.bar()
How it's possible, that method with defined body is abstract?
This has to do with the way default methods in Kotlin interfaces are implemented. The foo and bar methods in your interface really are both abstract.
However, there is an inner class inside the interface that looks something like this (simplified):
public interface MyInterface {
void foo();
void bar();
public static final class DefaultImpls {
public static void foo() {
System.out.print("Something");
}
}
}
This class is the one that contains the default implementations of any functions that you gave a body to inside the interface.
Then, if you create a class that implements this interface, and you don't override the foo method:
class MyClass: MyInterface {
override fun bar() {
println("MyClass")
}
}
Then you get one generated automatically, which just calls the implementation inside DefaultImpls:
public final class MyClass implements MyInterface {
public void bar() {
System.out.println("MyClass");
}
public void foo() {
MyInterface.DefaultImpls.foo();
}
}
You can find all these details by using the bytecode viewer that comes with the Kotlin plugin (Tools -> Kotlin -> Show Kotlin Bytecode, and then the Decompile option).
i was recently digging on new partial methods in c#3.0, i understood the use of partial class, that it could be chunked into multiple file one contain the definition and other declaration, but i wanted to know,i created a partial class like below:
in class1.cs
partial class A
{
partial void Method();
}
in class2.cs
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
}
now in class3.cs
class MainClass
{
static void Main()
{
A obj = new A();
obj.Method(); //Here i cannot call the "Method" method.
}
}
then whats the use of creating partial method, i read on MSDN that, at runtime, compiler compiles the class into one, in that case compiler should be getting the "Method" method implementation also, then why it dont allow me to call the "Method" method in the main method, can anyone correct me if i am wrong, and tell me why i am unable to call this partial method in main.
From MSDN
No access modifiers or attributes are allowed. Partial methods are
implicitly private.
It's a private method, so you can't call it from main.
You can call a partial method inside the constructor where the method is defined.
For example
public partial class classA
{
partial void mymethod();
}
public partial class classA
{
partial void mymethod()
{
Console.WriteLine("Invoking partial method");
}
public ClassA()
{
mymethod();
}
}
public class MainClass
{
static void Main()
{
ClassA ca=new ClassA();
}
}
That's it..now execute your code and see the result..
OutPut
Invoking partial method
Yes, we can't call it from Main(). Problem is not Partial method problem is method without specifier in a class is Private and private method can be called inside the class only.
Try creating a new public method in Partial class:
partial class A
{
partial void Method();
}
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
public void Study()
{
Console.WriteLine("I am studying");
Method();
}
}
class MainClass
{
static void Main()
{
A obj = new A();
obj.Study();
}
}
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>();