I am trying to use Evosuite to generate tests for a single entry point in a project. It is only considering coverage in the entry point class.
Minimal example:
public final class A {
private A() {}
public static void test(int a) {
if (a == 100) System.out.println();
}
}
public final class B {
private B() {}
public static void test(int a) {
A.test(a);
}
}
evosuite -Dinstrument_parent=true -Dinstrument_context=true -Dinstrument_method_calls=true -Dinstrument_libraries=true -Dcriterion=BRANCH -generateSuite -projectCP . -class B
Evosuite yields only one test and is oblivious to the branch in A. Is there any way to make it consider coverage for dependency classes?
Related
I have made a sample application in android and included the aar file in it and i have peform unit testing for the application whether it is possible to carry out unit testing for the sample application?
Consider the below Sample Class for UnitTesting
public class SampleUnitTestClass {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
After creating the class use the shortcut,Ctrl+Shift+T to create a new Test class corresponding to your sample Class.
Click on Create new Test
Select the methods that you want in your Unit test class and click ok(You could also change the Class name, destination package, Testing Library if needed)
Select destination directory and click OK
A Unit test class will be created
public class SampleUnitTestClassTest {
#Test
public void add() throws Exception {
}
#Test
public void subtract() throws Exception {
}
}
Write your testing logic here and asset your answer.For eg:
public class SampleUnitTestClassTest {
#Test
public void add() throws Exception {
SampleUnitTestClass testClass = new SampleUnitTestClass();
int answer = testClass.add(2,7);
assertEquals("Addition of 2 positive integers",9,answer);
}
#Test
public void subtract() throws Exception {
SampleUnitTestClass testClass = new SampleUnitTestClass();
int answer = testClass.subtract(2,7);
assertEquals("Subtraction of 2 positive integers",-5,answer);
}
}
Add more methods to include negative , null values etc and assert the answer.
For Unit Testing you can use Mockito and if u require some Android resources as well you can read about Robolectric.
Currently I have a strange behavior of my test.
For my test I'm using Groovy and JUnit 4. Groovy has his own strang logic, so don't worry about my code :D
Example 1:
#Log
class Test {
private static def filledFormFields = [
a: 1,
b: 2
]
#Before
void check() {
filledFormFields.each { k, v ->
log.info("$k -> $v")
}
}
#Test
void testA() {
System.exit(0)
}
}
log.info prints all information of filledFormFields (even it is static, yes it has to be static in my case).
Example 2:
#Log
class Test extends Manager {
private static def filledFormFields = [
a: 1,
b: 2
]
#Test
void testA() {
System.exit(0)
}
}
#Log
abstract class Manager {
#Before
void check() {
filledFormFields.each { k, v ->
log.info("$k -> $v")
}
}
}
log.info prints nothing, but why?
Why is my filledFormFields static?
Currently I'm creating Selenium/Geb JUnit Tests with Groovy. Each test instantiates the filledFormFields variable new. In my case I don't want that, because of that it's static. Also it's private because I have multiple test classes that useses this variable just with other equally other information. That was the only solution that I found to keep the varibales only in a class and not instantiated on every test.
I have two classes that each implement something differently but the tests are the same for both. Each class does some things that would affect the other class if they run in parallel, so they can't run in parallel. (that's the rationale behind the code below)
If you run the class Both, see below, in Eclipse, as a TestNG test, one would expect it to run tests test1 and test2 of class ClassAAA first and then the same test methods for ClassBBB, because ClassBBB's group annotations specify that it depends on ClassAAA's annotation.
However, what you find out is that, seemingly, TestNG has a different way of looking at it, and, "seemingly", it ignores the group order and runs the tests of the two clases in parallel.
class Both {
#Test(groups={"base"})
public static abstract class BothBase {
#Test public void test1() { System.out.println("test1"+name()); }
#Test public void test2() { System.out.println("test2"+name()); }
protected String name() {
String s = getClass().getName();
s = s.substring( 1 + s.lastIndexOf("$"));
return " - " + s;
}
}
#Test(groups={"gr1"})
public static class ClassAAA extends BothBase { }
#Test(groups={"gr2"},dependsOnGroups={"gr1"})
public static class ClassBBB extends BothBase { }
}
The output is:
test1 - ClassAAA
test1 - ClassBBB
test2 - ClassAAA
test2 - ClassBBB
One way, which i don't like, to try to "force" it to honor the desired group order, is to add a dummy test method to the leaf classes, as follows:
#Test(groups={"gr1"})
public static class ClassAAA extends BothBase {
#Test public void dummyTestMustBeInAllLeavesToEnforceGroupOrder() {
System.out.println("dummyTestMustBeInAllLeavesToEnforceGroupOrder"+name());
}
}
#Test(groups={"gr2"},dependsOnGroups={"gr1"})
public static class ClassBBB extends BothBase {
#Test public void dummyTestMustBeInAllLeavesToEnforceGroupOrder() {
System.out.println("dummyTestMustBeInAllLeavesToEnforceGroupOrder"+name());
}
}
This still doesn't completely do what one would expect. The output is:
test1 - ClassAAA
test2 - ClassAAA
test2 - ClassBBB
dummyTestMustBeInAllLeavesToEnforceGroupOrder - ClassAAA
test1 - ClassBBB
dummyTestMustBeInAllLeavesToEnforceGroupOrder - ClassBBB
This means that it started running the tests of ClassBBB before finishing the tests of ClassAAA.
I don't like the fact that i have to add a dummy/unrelated method to each, to get TestNG to understand that they cannot be run in parallel. In fact, i don't care which class runs first... And, i didn't really accomplish what i wanted because they are still running in parallel.
The stupidest way to do this, which would accomplish my goal is to move the tests from the base class to each of the leaf classes - is that how you are suppose to do these things in TestNG?
Is there another way of doing that? i'm sure someone is going to suggest priorities - but again, that does not convey the true intention - i don't have any priority - just don't want them to run in parallel. Also i don't want to write XML files...
This problem is coming due to static inner classes. Try below code you will get required output
import org.testng.annotations.Test;
#Test(groups={"base"})
public abstract class BothBase {
#Test public void test1() { System.out.println("test1"+name()); }
#Test public void test2() { System.out.println("test2"+name()); }
protected String name() {
String s = getClass().getName();
s = s.substring( 1 + s.lastIndexOf("$"));
return " - " + s;
}
}
#Test(groups={"gr1"})
class ClassAAA extends BothBase { }
#Test(groups={"gr2"},dependsOnGroups={"gr1"})
class ClassBBB extends BothBase { }
Many ways
Simplest - priority
Bit harder( groups)
Check testng manual(for ex. this http://www.mkyong.com/tutorials/testng-tutorials/).
I am recently working on TDD with JUnit and Mockito. For some purpose within a method, I am using an Util class (an utility class having methods in project context). The thing is what I am facing is how to mock such classes in Mockito. I am not able to find as such implementation regarding static methods in Mockito. Some suggest using PowerMock on top of Mockito but wouldn't that replace my JunitMockitoRunner?
The way I am using the static function is:
public void doSomething(int x){
//Some code
Y y = Util.someStaticMethod(x);
//Some more code
}
EDIT : Also I read somewhere that using static methods is a code smell and is a sign of bad design. So how should I be refactoring the design and what advantages will I be having as a result?
How should I be refactoring the design and what advantages will I be having as a result?
Well, if you need to mock the static utility method, then make it an instance method of an injectable object, so that you can inject a mock implementation of this object. The advantage is that it makes your code more testable:
public class Util {
public Y someInstanceMethod(X x) {
...
}
}
public class ClassToBeTested {
private Util util;
public ClassToBeTested(Util util) {
this.util = util;
}
public void doSomething(int x){
//Some code
Y y = util.someInstanceMethod(x);
//Some more code
}
}
public class ClassToBeTestedTest {
public void testDoSomething() {
Util mockUtil = mock(Util.class);
ClassToBeTested t = new ClassToBeTested(mockUtil);
when(mockUtil.someInstanceMethd(3)).thenReturn(...);
...
}
}
That's the main selling point of dependency injection: it makes your code testable.
I do something like that for mocking static Util classes with jMockit.
public class UtilsTest {
#After
public void teardown() {
Mockit.restoreAllOriginalDefinitions();
}
#Test
public void testMyUtil() {
Mockit.setUpMocks( MockUtil.class );
}
}
#MockClass(realClass=Util.class)
public class MockUtil{
#Mock
public static MySpecialClass someStaticMethod(int x) {
return Mockito.mock(MySpecialClass.class);
}
}
I am running some JUnit tests programatically with JUnitCore, and I want to get some data out of the test class once it is finished (so #AfterClass). Here is a pseudocode example of the constraints I am working under:
public class A {
public static String testData;
public static void runTest() {
JUnitCore juc = new JUnitCore();
juc.run(B);
// This is where I would like to access testData for this
// particular run
}
public static void setTestData(String s) {
testData = s;
}
}
public class B {
// Some #Test methods and stuff omitted
#AfterClass
public static void done(String s) {
A.setTestData(someData);
}
}
My problem is that different threads might be calling runTest(), so testData might be wrong. How do I work around this? I'm so lost.
If you really need/want to go with this design, you can make testData a java.lang.ThreadLocal<String>. This will solve the multi-threading issue.