Robotium setUp() and tearDown() methods - unit-testing

I have a test class as follows. The methods setUp() and tearDown() run before and after each test case. However, I want these methods to run once in the beginning of the test suite and in the end. I could not find an annotation like #BeforeClass, #AfterClass. How can I make these methods to run before the first test case and after the last test case in a test class?
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
public void test1{
do smt
}
public void test2{
do smt
}

The tearUp() method should be placed at the bottom, after your test cases. The tearDown() method will automatically be run last.
The setUp() method should be placed before your test cases and it will automatically be run first before your test cases start running.
Like this:
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void test1{
do smt
}
public void test2{
do smt
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

Related

Mockito uses method stubbing from other test method in the same class

I am trying to test a method for the happy path and an Exception scenario.
My class looks like this
class MyClass
{
#Autowired
AnotherClass anotherClass;
public Object myMethod() throws MyException
{
try{
//DO SOME STUFF
anotherClass.anotherMethod();
}
catch(Exception e)
{
throw MyException(e);
}
}
}
I am testing the above myMethod like this.
#RunWith(MockitoJUnitRunner.class)
class MyClassTest
{
#Mock
AnotherClass anotherClass;
#InjectMocks
MyClass myClass;
#Test
public void myMethodTest()
{
when(anotherClass.anotherMethod()).thenReturn("Mocked data");
myClass.myMethod();
}
#Test(expected=MyException.class)
public void myMethodExpTest()
{
when(anotherClass.anotherMethod()).thenThrow(MyException.class);
myClass.myMethod();
}
}
When I checked the code coverage using Jacoco, it does not coverage the exception catch block. I tried debugging the test in my Eclipse IDE. I am getting the "Mocked Data" for the exception test method. It appears to be the mocking to that method is not getting reset for the second method.
Is there a way to flush the method mocking/stubbing from previous test methods?
First I would consider this a bug. But you can manually reset mocks
#Before
public void resetMock() {
Mockito.reset(anotherClass);
}

Mstest [ClassCleanup] not executed at all in VS 2019

[ClassInitialize]
public static void BeforeCls(TestContext tc)
{
Console.WriteLine("ClassInitialize - Before Class");
}
[ClassCleanup]
public static void AfterCls()
{
Console.WriteLine("ClassCleanup - After Class");
}
[TestInitialize]
public void BeforeMethod()
{
Console.WriteLine("TestInitialize - Before Method");
}
[TestCleanup]
public void AfterMethod()
{
Console.WriteLine("TestCleanup - After Method");
}
[TestMethod]
public void TestMethod1()
{
Console.WriteLine("Test Method - 1");
}
Class cleanup is never executed in this code.
here is the Output
ClassInitialize - Before Class
TestInitialize - Before Method
Test Method - 1
TestCleanup - After Method
Invested a bit more time and discovered that using this will work within a test class:
[ClassCleanup(InheritanceBehavior.None))]
public static void ClassCleanup()
{
// Your code goes here
}
And using that will work for a derived test class:
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static void ClassCleanup()
{
// Your code goes here
}
You can read more about it here.
If the solution is not working for you no matter what, you can define IDisposable. The code will look something like this:
[TestClass]
public class DisposableBaseTest : IDisposable
{
// Your initialize methods
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~DisposableUserBaseTest()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Your cleanup code
Console.WriteLine("ClassCleanup - After Class");
}
}
}
P.S. And according to carlin.scott from this post, if you receive an exception during ClassInitialize, you want to handle this separately.
ClassCleanup is executed - put a breakpoint inside your method and verify it yourself. Problem is, that this method is executed after all of your tests are completed, so you cannot write message from that method in test output report. This is similar to ClassInitialize method. If you have multiple tests in your file, your ClassInitialize will write a message to only one of them.

How to deal with #BeforeEach #AfterEach for all test except one

A test class has several tests, each one depends on the #BeforeEach and is teared down by #AfterEach, except one black sheep!
How to deal with this in Juni5?
Is it legit to just have #AfterEach and #BeforeEach do an extra round for nothing?
If moving the test to another class is not practical, you can use a #Nested class to solve this. #BeforeEach and #AfterEach will be executed only for those tests defined inside. Here is an example:
#Test
void oneTEstWithNoBeforeAfterEach() {}
#Nested
class allOtherTestsGoHere {
#BeforeEach
void setUp() {}
#AfterEach
void tearDown() {}
#Test
void testOne() {}
#Test
void testTwo() {}
}

How to unit test functions that must occur in pairs?

Quite often I have some functions that must occur in pairs such as
//Example
startSomething()
...
stopSomething()
//Example
openSomething()
...
closeSomething()
while unit testing startSomething() and openSomething() is easy since they require no prior conditions / setup, how should I unit test their counterparts which require the prior to be called?
The majority of unit test frameworks have setup tests which are called before/after all, and/or each, of the test cases. Take NUnit as example:
[TestFixture]
public class MyTest
{
[OneTimeSetup]
public void GeneralSetup()
{ ... } //Called before starts all test cases
[OneTimeTearDown]
public void GeneralTearDown()
{ ... } //Called after all test cases are finished
[Setup]
public void Setup()
{ ... } //Called before each test case
[TearDown]
public void TearDown()
{ ... } //Called after each test case
[Test]
public void Test1()
{ ... }
[Test]
public void Test2()
{ ... }
}

Gallio unit test startup code

This is going to be an amateur level question. Is there a way to add start up code to a test project that uses MBUnit 3.4.0.0? I tried adding [TestFixture] and [FixtureSetUp] attributes to code that I wanted to run first, but unfortunately that didnt help.
The [FixtureSetUp] should be executed once before any test of the tests contained within the [TestFixture], but the two cannot be used interchangeably.
Here's a quick example. Admittedly the class doesn't have to be decorated with the [TestFixture] attribute, but it's good practice.
[TestFixture]
public class SimpelTest
{
private string value = "1";
[FixtureSetUp]
public void FixtureSetUp()
{
// Will run once before any test case is executed.
value = "2";
}
[SetUp]
public void SetUp()
{
// Will run before each test
}
[Test]
public void Test()
{
// Test code here
Assert.AreEqual("2", value);
}
[TearDown]
public void TearDown()
{
// Will run after the execution of each test
}
[FixtureTearDown]
public void FixtureTearDown()
{
// Will run once after every test has been executed
}
}