#DataPoints public static final Integer[] input1={1,2};
#Theory
#Test
public void test1(int input1){
}
#DataPoints public static final Integer[] input2={3,4};
#Theory
#Test
public void test2(int input2 ){
}
I want that test1 runs with data set input1 - {1,2} and test2 runs with input2 - {3,4}. But currently each test runs with both the data sets {1,2,3,4}. How to bind specific #DataPoints to specific #Theorys
With JUnit 4.12 (not sure when it was introduced) it is possible to name the DataPoints and assign them to parameters (i learned it from http://farenda.com/junit/junit-theories-with-datapoints/):
#RunWith(Theories.class)
public class TheoriesAndDataPointsTest {
#DataPoints("a values")
public static int[] aValues() {
return new int[]{1, 2};
}
#DataPoints("b values")
public static int[] bValues() {
return new int[]{3, 4};
}
#Theory
public void theoryForA(#FromDataPoints("a values") int a) {
System.out.printf("TheoryForA called with a = %d\n", a);
}
#Theory
public void theoryForB(#FromDataPoints("b values") int a) {
System.out.printf("TheoryForB called with b = %d\n", a);
}
}
Output:
TheoryForA called with a = 1
TheoryForA called with a = 2
TheoryForB called with b = 3
TheoryForB called with b = 4
DataPoints apply to the class. If you have a #Theory method which takes an int, and you have a DataPoint which is an array of ints, then it will be called with the int.
#RunWith(Theories.class)
public class TheoryTest {
#DataPoint public static int input1 = 45;
#DataPoint public static int input2 = 46;
#DataPoints public static String[] inputs = new String[] { "foobar", "barbar" };
#Theory public void testString1(String input) {
System.out.println("testString1 input=" + input);
}
#Theory public void testString2(String input) {
System.out.println("testString2 input=" + input);
}
#Theory public void test1(int input) {
System.out.println("test1 input=" + input);
}
#Theory public void test2(int input) {
System.out.println("test2 input=" + input);
}
}
This calls test1 with 45 & 46, and test2 with 45 & 46. It calls testString1 with "foobar" and "barbar" and testString2 with "foobar" and "barbar".
If you really want to use different data sets for different theories, you can wrap the data in a private class:
#RunWith(Theories.class)
public class TheoryTest {
public static class I1 { int i; public I1(int i) { this.i = i;} }
public static class I2 { int i; public I2(int i) { this.i = i;} }
#DataPoint public static I1 input1 = new I1(45);
#DataPoint public static I2 input2 = new I2(46);
#Theory
public void test1(I1 input) {
System.out.println("test1 input=" + input.i);
}
#Theory
public void test2(I2 input) {
System.out.println("test2 input=" + input.i);
}
}
This calls test1 with 45 and test2 with 46. This works, but in my opinion, it obscures the code, and it may be a better solution to just split the Test class into two classes.
In reference to Gábor Lipták's answer, named datapoints can be defined as a static fields (reference) which give us more concise code:
#RunWith(Theories.class)
public class TheoriesAndDataPointsTest {
#DataPoints("a values")
public static int[] aValues = {1, 2};
#DataPoints("b values")
public static int[] bValues = {3, 4};
#Theory
public void theoryForA(#FromDataPoints("a values") int a) {
System.out.printf("TheoryForA called with a = %d\n", a);
}
#Theory
public void theoryForB(#FromDataPoints("b values") int a) {
System.out.printf("TheoryForB called with b = %d\n", a);
}
}
Some of the references I have seen talking about using tests for specific values and theories for verifying behavior. As an example, if you have a class that has methods to add and subtract from an attribute, a test would verify correctness of the result (e.g., 1+3 returns 4) whereas a theory might verify that, for the datapoint values (x1, y1), (x2, y2), x+y-y always equals x, x-y+y always equals x, x*y/y always equals x, etc. This way, the results of theories are not coupled as tightly with the data. With theories, you also can filter out cases such as y == 0; they don't count as failure. Bottom line: you can use both. A good paper is: http://web.archive.org/web/20110608210825/http://shareandenjoy.saff.net/tdd-specifications.pdf
Related
This is the main method.
I want the integer that was asked to the user to go to the Cat class so it outputs the word "meow" until the counter is equal to the number
import java.util.Scanner;
public class Runnable {
**public static void main( String args[]) {
Scanner input = new Scanner(System.in);
Cat cat1 = new Cat();
System.out.print("Enter num :");
int num.sound()=input.nextInt();
System.out.println("Calling method with no parameters");
cat1.sound();
System.out.println("Calling method with one parameter");
cat1.sound();
}
}
this is a Class called Cat
public class Cat {
public void sound() {
System.out.println("Meow");
}
public void sound( int num){
int counter=0;
for(int num1=counter; num1>=num; counter++) {
System.out.println("Meow");
}
}
}
A couple of things first:
what you mean with the line:
int num.sound()=input.nextInt();
you not pass paramenters in
System.out.println("Calling method with no parameters");
cat1.sound();
System.out.println("Calling method with one parameter");
cat1.sound();
But the main flow is in the for loop that could be:
for (int num1 = 0; num1 < num; num1++)
{ ... }
I am trying to gather some information after every test method, and would like to analyze the gathered information after the test class completes. So, I have a private member variable, a list which I would like to add to after every test method completes. However, at the end of the day, the member variable always remains null.
Note: My test class implements Callable interface.
Here is my code snippet:
{
private List<String statisticsCollector;
private JUnitCore core = null;
private int x = 0;
public MyLoadTest() {
this.core = new JUnitCore();
this.statisticsCollector = new ArrayList<String>();
}
#Override
public List<String> call() {
log.info("Starting a new thread of execution with Thread# -" + Thread.currentThread().getName());
core.run(this.getClass());
return getStatisticsCollector(); // this is always returing a list of size 0
}
#After
public void gatherSomeStatistics() {
x = x+1;
String sb = new String("Currently executing ----" + x);
log.info("Currently executing ----" + x);
addToStatisticsCollector(sb);
}
#Test
#FileParameters(value = "classpath:folder/testB.json", mapper = MyMapper.class)
public void testB(MarsTestDefinition testDefinition) {
runTests(testDefinition);
}
#Test
#FileParameters(value = "classpath:folder/testA.json", mapper = MyMapper.class)
public void testA(MyDefinition testDefinition) {
runTests(testDefinition);
}
public List<String> getStatisticsCollector() {
return this.statisticsCollector;
}
public void addToStatisticsCollector(String sb) {
this.statisticsCollector.add(sb);
}
}
So, why is it always getting reset, even though I am appending to the list in my #After annotated method?
Any help will be highly appreciated. Thanks
Try with following code, is it working ?
private static List<String> statisticsCollector = new ArrayList<String>();
private JUnitCore core = null;
private int x = 0;
public MyLoadTest() {
this.core = new JUnitCore();
}
public List<String> getStatisticsCollector() {
return statisticsCollector;
}
I have this test:
#Test
public void shouldReturn2Hours() {
Float expectedHours = 2f;
WorkChronometer workChronometer = Mockito.mock(WorkChronometer.class);
Mockito.when(workChronometer.getAccumulatedMinutes()).thenReturn(120);
Assert.assertEquals(expectedHours, workChronometer.getAccumulatedHours());
}
and the implementation of WorkChronometer:
public class WorkChronometer {
private DateTime startingInstant;
private DateTime stoppingInstant;
private Boolean counting;
//More methods
public Integer getAccumulatedMinutes() {
if (counting)
throw new RuntimeException("Call stopCount first!");
if (startingInstant == null || stoppingInstant == null)
return 0;
return Minutes.minutesBetween(startingInstant, stoppingInstant).getMinutes();
}
public Float getAccumulatedHours() {
Integer accumulatedMinutes = getAccumulatedMinutes();
return accumulatedMinutes / 60f;
}
}
When I execute the test, it fails:
junit.framework.AssertionFailedError: expected:<2.0> but was:<0.0>
But I don't know why. It seems the mock is not returning what I want.
What am I doing wrong?
Thanks.
You're mocking the class under test. Doing that relaces all the methods by methods doing nothing, and returning default values.
If you want to do that, you'll need a spy, or a partial mock.
With a spy:
#Test
public void shouldReturn2Hours() {
Float expectedHours = 2f;
WorkChronometer workChronometer = new WorkChronometer();
WorkChronometer spy = Mockito.spy(workChronometer);
doReturn(120).when(spy).getAccumulatedMinutes();
Assert.assertEquals(expectedHours, spy.getAccumulatedHours());
}
With a partial mock:
#Test
public void shouldReturn2Hours() {
Float expectedHours = 2f;
WorkChronometer workChronometer = Mockito.mock(WorkChronometer.class);
Mockito.when(workChronometer.getAccumulatedHours()).thenCallRealMethod();
Mockito.when(workChronometer.getAccumulatedMinutes()).thenReturn(120);
Assert.assertEquals(expectedHours, workChronometer.getAccumulatedHours());
}
I'm confused about how new windsor3 perfmonace counter shows tracking of objects generated via TyepedFactory.
considering following scenario
public interface IBFactory
{
IB[] GetAll();
void FreeUp(IB cmps);
}
public class B1 : IB, IDisposable
{
public void Add(int i){}
public void Dispose()
{
Console.WriteLine("Disposing " + GetType().Name);
}
}
public class B2 : IB, IDisposable
{
public void Add(int i){}
public void Dispose()
{
Console.WriteLine("Disposing " + GetType().Name);
}
}
public class B3 : IB
{
public void Add(int i){}
public void Dispose()
{
Console.WriteLine("Disposing " + GetType().Name);
}
}
var container = new WindsorContainer();
var diagnostic = LifecycledComponentsReleasePolicy.GetTrackedComponentsDiagnostic(container.Kernel);
var counter = LifecycledComponentsReleasePolicy.GetTrackedComponentsPerformanceCounter(new PerformanceMetricsFactory());
container.Kernel.ReleasePolicy = new LifecycledComponentsReleasePolicy(diagnostic, counter);
Console.WriteLine("Enter number of iterations:");
int iterations = int.Parse(Console.ReadLine());
container.AddFacility<TypedFactoryFacility>();
container.Register
(
Component.For<IBFactory>()
.AsFactory()
.LifeStyle.Transient,
Classes.FromAssemblyContaining<IB>()
.BasedOn(typeof(IB))
.WithService.Base()
.Configure(c => c.LifestyleTransient())
);
Console.WriteLine("Create Memory Leak Y or N?");
var leak = Console.ReadLine().ToUpper() == "Y";
var sleepFor = 100;// int.Parse(Console.ReadLine());
for (var i = 1; i < iterations+1; i++)
{
var factory = container.Resolve<IBFactory>();
Console.WriteLine("Factory created.");
var cmp = factory.GetAll();
foreach (var b in cmp)
{
b.Add(i);
}
Console.WriteLine("Iteration {0} completed", i);
Thread.Sleep(sleepFor);
if (!leak)
{
foreach (var b in cmp)
{
factory.FreeUp(b);
}
}
Console.WriteLine("Releasing factory.");
container.Release(factory);
}
Console.WriteLine("container disposing.....");
container.Dispose();
Console.WriteLine("container disposed");
Console.ReadLine();
If I dispose objects, as I should, via FreeUp factory method, perf counter shows expected tracking.
Instead if I do not expliclty dispose objects, but if I'll do implicitly disposing the factory, created as transient for testing purpose, IB instances are disposed when I dispose the factory (as per documentation), but perf counter does not get updated and shows IB instance still as tracked...
What that means?
Perf counter has not been updated or objects are still tracked(that's would be very scary) even if Dispose has been called on IB instances due to factory disposing.
I know how to do it using simple recursion, but in order to complete this particular assignment I need to be able to accumulate on the stack and throw an exception that holds the answer in it.
So far I have:
public static int fibo(int index) {
int sum = 0;
try {
fibo_aux(index, 1, 1);
}
catch (IntegerException me) {
sum = me.getIntValue();
}
return sum;
}
fibo_aux is supposed to throw an IntegerException (which holds the value of the answer that is retireved via getIntValue) and accumulates the answer on the stack, but so far I can't figure it out. Can anyone help?
I don't know what your implementations for fibo_aux and IntegerException look like, but the following two implementations work with your existing code (I don't think there's anything wrong with the code you posted, so I assume something is awry in either fibo_aux or IntegerException). Maybe you'll find this helpful.
public static void fibo_aux(int index, int a, int b) throws IntegerException
{
if (--index > 0)
fibo_aux(index, b, a + b);
else
throw new IntegerException(a + b);
}
An implementation for IntegerException:
public class IntegerException extends Exception
{
private static final long serialVersionUID = -6795044518321782305L;
private Integer intValue;
public IntegerException(int i)
{
this.intValue = i;
}
public Integer getIntValue()
{
return intValue;
}
}
Here you go :
public class ExcFib {
/**
* #param args
*/
public static void main(String[] args) {
new ExcFib().fibo ( 10 );
}
class FiboException extends Throwable
{
public int n;
public FiboException(int n)
{
this.n = n;
}
private static final long serialVersionUID = 1L;
}
public void fibo(int idx) {
try {
fibo_aux(idx-1,1,1);
} catch (FiboException e) {
System.out.println ( "F(" + idx + ") = " + e.n );
}
}
private void fibo_aux(int i, int j, int k) throws FiboException {
if ( i < 1 )
{
throw new FiboException(k);
}
fibo_aux(i - 1, k, j + k );
}
}