I am trying to run a unit test on a method in an abstract class. I have condensed the code below:
Abstract Class:
public abstract class TestAb
{
public void Print()
{
Console.WriteLine("method has been called");
}
}
Test:
[Test]
void Test()
{
var mock = new Mock<TestAb>();
mock.CallBase = true;
var ta = mock.Object;
ta.Print();
mock.Verify(m => m.Print());
}
Message:
Method is not public
What am I doing wrong here? My goal is to test the methods inside the abstract class using he Moq framework.
Getting started with MoqMoq can be used to mock both classes and interfaces.
The answer is: always test only concrete classes; don't test abstract classes directly . The reason is that abstract classes are implementation details. From the client perspective, it doesn't matter how Student or Professor implement their GetSignature() methods.
The Mockito. spy() method is used to create a spy instance of the abstract class. Step 1: Create an abstract class named Abstract1_class that contains both abstract and non-abstract methods. Step 2: Create a JUnit test case named Abstract1Test.
With JUnit, you can write a test class for any source class in your Java project. Even abstract classes, which, as you know, can't be instantiated, but may have constructors for the benefit of “concrete” subclasses.
The message is because your Test()
method is not public
. Test methods need to be public
. Even after making the test method public
it will fail as you can only verify abstract
/virtual
methods. So in your case you will have to make the method virtual
since you have implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With