How to use attributes in JUnit Tests? - java

I have been working with JUnit for several years and I have found many examples where isolation within the test was not fullfil.
Most of the tests I develop follow the same structure
class ClassToTestTest {
// Attributes
private ClassToTest objectToTest;
// Methods
#Before
public void setup() {
objectToTest = new ClassToTest();
}
#Test
public void test1() {
//do something
Assert...
}
#Test
public void test2() {
//do something
Assert...
}
}
The internal state of the test object (not the ClassToTest object but the object which perform the test) are in its attributes. If there is any flaw at the setup() or teardown() methods some internal state of objectToTest might sneak to other test.
Just wondering, would it be better to have no shared state? you have no attributes
class ClassToTestTest {
// Attributes
// No attributes, no internal state
// Methods
#Before
public void setup() {
objectToTest = new ClassToTest();
}
#Test
public void test1() {
localObjectToTest = createObjectToTest();
//do something
Assert...
}
#Test
public void test2() {
localObjectToTest = createObjectToTest();
//do something
Assert...
}
}
I know the first code do pretty much the same than the second, but in the first code you are tempted to do something like
// Methods
#Before
public void setup() {
objectToTest = objectToTest.reset();
}
or even worse, rely on the previous test in order to use the state of the objectToTest in the previous test to "save" time and you end up with an empty setup() method
Following the second code is much dificult to reach this point because there is no shared state, every object is local.
What are your thoughts? Make sense? Is it worthy?

JUnit creates a new ClassToTestTest object for each of your test methods. Therefore no field can sneak to another test. For details: http://martinfowler.com/bliki/JunitNewInstance.html

In my opinion I think it is worthy to create a ClassToTestTest object in each test because it is possible to modify the internal state and each test should be independent of the other ones.
A fussier developer could tell you that you are also testing the Constructor of the object in each test. Then, you can also use the #Before and #After annotations in the methods for creating and destroying the ClassToTestTest object.
But anyway, I would rather go to create the object in each test.
Edit: Adding bold font to put the text more readable

Related

How to use mockito / powermockito to make an instantiated dependent class throw an exception on a specific method call

This is the code I want to test. It's pretty straight forward
class FileHandler {
public boolean deleteFiles(String path) {
// mock this to throw an exception
}
public static FileHandler instatiateNew(String location) {
// creates a FileHandler
}
}
class B {
public void action {
try {
FileHandler x = FileHandler.instantiateNew("asd");
x.deleteFiles();
} catch (Exception e) {
// untested code I want to reach
}
}
}
I now want to test method action and see how it handles x.deleteFiles() throwing an exception. I have tried doThrow, thenThrow and ran into errors (NullPointerException, probably because I stubbed the method wrongly) or the method not throwing the exception in the end.
I am also confused whether I need Powermockito or not. I will now try an approach, where I mock the whole FileHandler class. As I need to mock the static instantiation method I will need PowerMock for that. But I would prefer a less heavy handed solution. Does it exist?
my partial class mock is now:
FileHandler mockHandler = Mockito.mock(FileHandler.class)
Mockito.mock(mockHandler.deleteFiles(Mockito.anyString()).thenThrow(Exception.class);
PowerMockito.mockStatic(FileHandler.class);
PowerMockito.when(FileHandler.instantiateNew(Mockito.anyString())).thenReturn(mockHandler())
Which is still causing issues, maybe becasue FileHandler is used elsewhere and mockStatic kills all other usages.
Make sure all the necessary members are properly arranged so that the test can be exercised.
For example
RunWith(PowerMockRunner.class)
#PrepareForTest({FileHandler.class})
public class MyTestCase {
public void testdeleteFilesErrorHandling() throws Exception {
//Arrange
//instance mock
FileHandler handler = Mockito.mock(FileHandler.class);
Mockito.when(handler.deleteFiles(anyString())).thenThrow(new Exception("error message"));
//mock static call
PowerMockito.mockStatic(FileHandler.class);
Mockito.when(FileHandler.instantiateNew(anyString())).thenReturn(handler);
B subject = new B();
//Act
subject.action();
//Assert
//perform assertion
}
}
Reference: Using PowerMock with Mockito
using mockStatic was not an option for me as FileHandler was used in setup and teardown of the tests and this heavy handed approach would cause problems.
What saved me where stub and method from org.powermock.api.support.membermodification.MemberModifier.
FileHandler mock = Mockito.mock(FileHandler.class);
Mockito.when(mock.deleteFiles(anyString()))
.thenThrow(Exception.class);
stub(method(FileHandler.class, "instantiateNew", String.class)).toReturn(mock);
Note that it is necessary to prepare class FileHandler through a test class decorator and to use the PowerMockRunner. This is necessary as we are stubbing a static method on FileHandler. This is done so:
#PrepareForTest({FileHandler.class})
#RunWith(PowerMockRunner.class)
public class MyTest extends MyBaseTestClass {
#Test
public void myTest() {
// write test code from above here.
}
}

How to embed every test in a wrapper logic?

Given the requirement that every junit test have to run in the following wrapper:
#Test
public void testFooBar() {
SpecialLogic.runWith(new SpecialLogic("blah", "foo", ANYTHING), () -> {
// my test
});
}
I am trying to avoid adding SpecialLogic.runWith(...) for each test.
Is there any possibility by using #BeforeEach or any other way?
Otherwise, there is much of duplicated code:
#Test
public void testFooBar_2() {
SpecialLogic.runWith(new SpecialLogic("blah", "foo", ANYTHING), () -> {
// my test logic 2
});
}
#Test
public void testFooBar_3() {
SpecialLogic.runWith(new SpecialLogic("blah", "foo", ANYTHING), () -> {
// my test logic 3
});
}
There are two ways of doing this:
Write your custom Runner, all the tests will have to run with this runner.
This may be inappropriate if you already use another runner (say for spring or mockito)
Write your own Rule. The rule is a little bit newer way of doing what you've asked for,
and it doesn't "occupy" the slot of a runner which can be only one.
public final class SampleRule implements TestRule {
#Override public Statement apply(final Statement base,
final Description description) {
return new Statement() {
#Override public void evaluate() throws Throwable {
// do your stuff before actually running the test
try {
base.evaluate(); // This line actually runs the test.
} finally {
// do your stuff after running a test
}
}
};}}
Here is one of numerous guides for writing Rules:
Looks like you should implement your own TestRunner to wrap your custom logic around each test method call. There is an article over at Baelung explaining how this works.
#Before and #After? It won't use closures but should be functionally the same.
https://junit.org/junit4/javadoc/latest/org/junit/Before.html
https://junit.org/junit4/javadoc/latest/org/junit/After.html

How to add Grouping and Dependency in #BeforeMethod On TestNG

Is there any way to give dependency to #BeforeMethod on #Test,because in my scenario different TestMethod have different setup and I need one setup depends on TestMethod. I add here some code snippet for better understanding
#BeforeMethod(groups = {"gp2"})
public void setUp1() {
System.out.println("SetUp 1 is done");
}
#BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
public void setUp2() {
System.out.println("SetUp 2 is done");
}
#Test(timeOut = 1, groups = {"tgp1"})
public void testMethod() throws InterruptedException {
Thread.sleep(2000);
System.out.println("TestMethod() From Group1");
}
#Test(dependsOnMethods = {"testMethod"}, groups = {"tgp2"})
public void anotherTestMethod() {
System.out.println("AnotherTestMethod()From Group1 and Group2");
}
OutPut
SetUp 1 is done
SetUp 2 is done
but I need setUp1() should be executed not setUp2() because it is depends on tgp1 group.
Another thing I observe that,If I change dependencies from
#BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
to
#BeforeMethod(groups = {"gp1"}, dependsOnMethods = {"testMethod"})
then I got an Exception like
setUp2() is depending on method public void testMethod() throws java.lang.InterruptedException, which is not annotated with #Test or not included.
I need execution should be on this steps
SetUp1---->testMethod1()------->SetUp2--------->testMethod2()
I need this because different TestMethods have different work and it have different SetUp().
In your case, you can simply call respective setUp method from respective Test method as you really don't have common #BeforeMethod for each Test.
Or you can separate these two tests into different Test classes each having a #BeforeMethod.
Or you can do conditional setup call based on test method name with one #BeforeMethod method as below. Here #BeforeMethod can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this #BeforeMethod finishes
#BeforeMethod
public void setUp(Method method) {
if (method.getName().equals("testMethod")) {
setUp1();
} else if (method.getName().equals("anotherTestMethod")) {
setUp2();
}
}
public void setUp2() {
System.out.println("SetUp 2 is done");
}
public void setUp1() {
System.out.println("SetUp 1 is done");
}
The purpose of the #BeforeMethod is to run before each test method annotated with #Test and do some set-up work. Therefore it is somewhat confusing why you would want to create dependency that works the other way - execute the test method before set-up method.
The usual approach would be following:
#BeforeMethod
public void setUp() {
}
#Test
public void testMethod1() {
}
#Test
public void testMethod2() {
}
Which most likely generate following execution list:
setUp()
testMethod1()
setUp()
testMethod2()
If there multiple #BeforeMethods all of them will be run before the #Test method.
Now if you want to run different groups, you would annotate different methods with different groups and specify, which groups to run. However, for that you will need to provide either testng.xml or specify which groups to execute.
EDIT (based on additional comment):
I would suggest one of the following approaches:
Separate test methods into different classes and have #BeforeMethod
in each class perform required setup. This especially makes sense if
the tests are covering different areas.
Define different groups and
put corresponding test and set-up methods into same group. This
option lets you mix and match set-up methods with test method.
Some things that are unclear from your question, but if relevant could benefit from suggested approaches:
Is there need for dependency?
Does the order or the tests matter?

Mockito - how to verify that a mock was never invoked

I'm looking for a way to verify with Mockito, that there wasn't any interaction with a given mock during a test. It's easy to achieve that for a given method with verification mode never(), but I haven't found a solution for the complete mock yet.
What I actually want to achieve: verify in tests, that nothing get's printed to the console. The general idea with jUnit goes like that:
private PrintStream systemOut;
#Before
public void setUp() {
// spy on System.out
systemOut = spy(System.out);
}
#After
public void tearDown() {
verify(systemOut, never()); // <-- that doesn't work, just shows the intention
}
A PrintStream has tons of methods and I really don't want to verify each and every one with separate verify - and the same for System.err...
So I hope, if there's an easy solution, that I can, given that I have a good test coverage, force the software engineers (and myself) to remove their (my) debug code like System.out.println("Breakpoint#1"); or e.printStacktrace(); prior to committing changes.
Use this :
import static org.mockito.Mockito.verifyZeroInteractions;
// ...
private PrintStream backup = System.out;
#Before
public void setUp() {
System.setOut(mock(PrintStream.class));
}
#After
public void tearDown() {
verifyZeroInteractions(System.out);
System.setOut(backup);
}
verifyZeroInteractions(systemOut);
As noted in comments, this doesn't work with a spy.
For a roughly equivalent but more complete answer, see the answer by gontard to this question.
Since the original correct answer, verifyZeroInteractions has been deprecated, use verifyNoInteractions instead:
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
public class SOExample {
#Test
public void test() {
Object mock = mock(Object.class);
verifyNoInteractions(mock);
}
}
You could try a slightly different tack:
private PrintStream stdout;
#Before public void before() {
stdout = System.out;
OutputStream out = new OutputStream() {
#Override public void write(int arg0) throws IOException {
throw new RuntimeException("Not allowed");
}
};
System.setOut(new PrintStream(out));
}
#After public void after() {
System.setOut(stdout);
}
If you preferred, you could switch the anonymous type for a mock and verify as Don Roby suggests.
One way of solving this problem is to refactor the class that you're testing, to allow for the injection of a PrintStream that can be used for output. This will let you unit test it, without relying on the behaviour of the System class. You could use a package-private constructor for this injection, since you'll only ever use it from the corresponding test class. So it might look something like this.
public class MyClass{
private PrintWriter systemOut;
public MyClass(){
this(System.out);
}
MyClass(PrintWriter systemOut){
this.systemOut = systemOut;
// ...any other initialisation processing that you need to do
}
}
and within the class itself, use the systemOut variable instead of System.out wherever you call the latter.
Now, within the test class, make a mock PrintStream, and pass it to the package-private constructor, to get the object that you're going to test. Now you can run any actions you like from your tests, and use verify to check their effects on your mock PrintStream.

How to run tearDown type method for a specific test in JUnit class with multiple tests?

I have a junit testCase class with multiple test methods in it ( As requirement , we don't want to create separate class for each test.)
I wanna create a tearDown type method for EACH test method , which will run specifically for that test. Not for ALL test.
My problem is , in many tests i Insert record in database, test it and delete it after test.
But, If a test fails mid way , control don't reaches till end my dummy record ain't deleting.
I think only ONE tearDown() is allowed for one class, and this tearDown() don't know what object/record i created or inserted and what to delete!!!
I want to create a tearDown() or #After method just for one specific test. Something like finally{} in java for each method.
For Eg:
public class TestDummy extends TestCase {
public void testSample1(){
InsertSomeData1();
assertFalse(true);
runTearDown1();
}
public void testSample2(){
InsertSomeData2();
assertFalse(true);
runTearDown2();
}
public void runTearDown1(){
deleteDummyDatafromTestSample1....
}
public void runTearDown2(){
deleteDummyDatafromTestSample2....
}
}
Here control will never go to runTearDown1() or runTearDown2() and I don't a one common tearDown() because it won't know what data I inserted and thats specific to each method.
It seems your test relies on a fixed database, and future tests will break if your current test breaks. What I'd recommend is not to focus on this particular problem (a test-specific tearDown method that runs for each test), but your main problem - borken tests. Before your test run, it should always work with a clean database, and this should be the case for each test. Right now, your first test has a relationship with the second (through the database).
What the right approach would be is that you recreate your database before each test, or at the very least reset it to a basic state. In this case, you'll want a test like this:
public class TestDummy {
// this code runs (once) when this test class is run.
#BeforeClass
public void setupDatabase() {
// code that creates the database schema
}
// this code runs after all tests in this class are run.
#AfterClass
public void teardownDatabase() {
// code that deletes your database, leaving no trace whatsoever.
}
// This code runs before each test case. Use it to, for example, purge the
// database and fill it with default data.
#Before
public void before() {
}
// You can use this method to delete all test data inserted by a test method too.
#After
public void after() {
}
// now for the tests themselves, we should be able to assume the database will
// always be in the correct state, independent from the previous or next test cases.
#Test
public void TestSample2() {
insertSomeData();
assertTrue(someData, isValid());
}
}
Disclaimer: JUnit 4 tests (using annotations), might not be the right annotations, might not even be the right answer(s).
You could have smth like this:
interface DBTest {
void setUpDB();
void test();
void tearDownDB();
}
class DBTestRunner {
void runTest(DBTest test) throws Exception {
test.setUpDB();
try {
test.test();
} finally {
test.tearDownDB();
}
}
}
public void test48() throws Exception {
new DBTestRunner().runTest(new DBTest() {
public void setUpDB() {...}
public void test() {...}
public void tearDownDB() {...}
});
}
#iluxa . Gr8.. Your solution is perfect!!! In one test class i created two tests test48 and test49 (same as required in my code above testSample1 and testSample2) and viola! every test method now gets its own setup() and tearDown. Only this solution looks little complicated as need to use DBTestRunner in each method, but I don't see any better solution. I was thinking Junit may have some direct solution. like #After or tearDown() with some parameter or something.
Tks a lot.
Use MethodRule:
public class MyRule implements MethodRule {
#Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (AssertionError e) {
doFail();
} finally {
doAnyway();
}
}
};
}
}
Then declare it in your test class:
public class TestDummy{
public MethodRule rule = new MyRule();
......
}

Categories