nunit/mbunit parameterized SetUp - nunit

I'd like to provide parameterized data for the setup of a test. Something like this:
[TestCase("1", "2")
[TestCase("a", "b")
public class TestFixture
{
[SetUp]
public void SetUp(string x, string y)
{
Console.WriteLine("Setup with {0}, {1}", x, y);
}
[Test]
public void Test() {...}
}
But I can only manage to get parameters passed to the testcase itself.
I prefer solutions for either nunit or mbunit but open for alternatives (yet to settle on which test framework to use). This is ultimately for a set of Selenium system-tests where the setup creates a browser session.
[edit]
Using NUnit I can get this working:
[TestFixture("1", "2")]
[TestFixture("a", "b")]
public class Tests
{
private string _x;
private string _y;
public Tests(string x, string y)
{
_x = x;
_y = y;
}
[SetUp]
public void SetUp()
{
Console.WriteLine("Setup with {0}, {1}", _x, _y);
}
[Test]
public void Test()
{
}
}
[/edit]
That seems to suit my needs, but I'll leave the question open for a few days to see if alternatives are suggested.

Using NUnit I can get this working:
[TestFixture("1", "2")]
[TestFixture("a", "b")]
public class Tests
{
private string _x;
private string _y;
public Tests(string x, string y)
{
_x = x;
_y = y;
}
[SetUp]
public void SetUp()
{
Console.WriteLine("Setup with {0}, {1}", _x, _y);
}
[Test]
public void Test()
{
}
}

Related

Trying to read values returned on jsp form submission in springboot project by setters and use the combination to call another java class

So, I have values in getter setter variables when I click on form submit but now want to have those values in variables and check combination of them to run code from another java class
I have tried using parametrized constructor or may be having a common setter but that did not help.
package com.grt.dto;
import java.util.Set;
public class WDPayrollRecon {
public Set<String> dataType;
public String planCountry;
public String payPeriod;
public String currentPeriod;
public String lastPayPeriod;
Set<String> test;
public Set<String> getdataType() {
return dataType;
}
public void setdataType(Set<String> dataType) {
this.dataType = dataType;
System.out.println("this is dataType" +dataType);
test = dataType;
}
public String getPlanCountry() {
return planCountry;
}
public void setPlanCountry(String planCountry) {
this.planCountry = planCountry;
}
public String getPayPeriod() {
return payPeriod;
}
public void setPayPeriod(String payPeriod) {
this.payPeriod = payPeriod;
}
public String getCurrentPeriod() {
return currentPeriod;
}
public void setCurrentPeriod(String currentPeriod) {
this.currentPeriod = currentPeriod;
}
public String getlastPayPeriod() {
return lastPayPeriod;
}
public void setlastPayPeriod(String lastPayPeriod) {
this.lastPayPeriod = lastPayPeriod;
}
public WDPayrollRecon()
{
}
public WDPayrollRecon(Set<String> dataType,String planCountry,String payPeriod,String currentPeriod,String lastPayPeriod)
{
this.dataType = dataType;
this.planCountry = planCountry;
this.payPeriod = payPeriod;
this.currentPeriod = currentPeriod;
this.lastPayPeriod = lastPayPeriod;
if(dataType.contains("GTLI")& planCountry.equals("USA")){
System.out.println("This is test");
}
else{
System.out.println("This is not test");
}
}
}

Autofac: cannot resolve dependency using factory after ContainerBuilder.Update()

My problem is that I want to use Func<> factory to resolve dependency. And in if I use ContainerBuilder Update() (I need it for mocking some services in integration tests), this factories still resolve outdated instances.
I created simple scenario to reproduce the problem:
class Program
{
static void Main(string[] args)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Test>().As<ITest>();
containerBuilder.RegisterType<Test1Factory>().As<ITestFactory>();
containerBuilder.RegisterType<TestConsumer>().AsSelf();
var container = containerBuilder.Build();
var tc1 = container.Resolve<TestConsumer>();
var cbupdater = new ContainerBuilder();
cbupdater.RegisterType<Test2>().As<ITest>();
cbupdater.RegisterType<Test2Factory>().As<ITestFactory>();
cbupdater.Update(container);
var tc2 = container.Resolve<TestConsumer>();
Console.ReadLine();
}
}
public interface ITest
{
int Id { get; set; }
}
public class Test : ITest
{
public Test()
{
Id = 1;
}
public int Id { get; set; }
}
public class Test2 : ITest
{
public Test2()
{
Id = 2;
}
public int Id { get; set; }
}
public interface ITestFactory
{
ITest Create();
}
public class Test1Factory : ITestFactory
{
public ITest Create()
{
return new Test();
}
}
public class Test2Factory : ITestFactory
{
public ITest Create()
{
return new Test2();
}
}
public class TestConsumer
{
public TestConsumer(Func<ITest> testFactory, ITest test, ITestFactory customFactory)
{
Console.WriteLine("factory: " + testFactory().Id);
Console.WriteLine("direct: " + test.Id);
Console.WriteLine("MyCustomFactory: " + customFactory.Create().Id);
Console.WriteLine("*************");
Console.WriteLine();
}
}
The output is:
factory: 1 direct: 1 MyCustomFactory: 1
factory: 1 direct: 2 MyCustomFactory: 2
Notice "factory: 1" in both cases.
Am I missing something or I have to create my cusom factory in this scenario?
P.S.
Autofac 3.5.2 or 4.0 beta 8-157
.net 4.5.1
That's by design unfortunately, the reasons, I don't know. Looking at the Autofac code gives you a better insight on how they register items with the same interface definition, in short, all registrations are maintained but the last registration wins (ref). Wait...that's not all, weirdly, for Fun<...>, you actually get them in order. You can easily test by changing the constructor of the TestConsumer class to:
public TestConsumer(Func<ITest> testFactory, IEnumerable<Func<ITest>> testFactories, IEnumerable<ITest> tests, ITest test, ITestFactory customFactory)
{
// ...
}
Note that you get all the Funcs and the ITest registration. You are simply lucky that resolving ITest directly resolves to Test2.
Now, having said all of the above, there is a way described here. You have to create a container without the registration you want to override, therefore:
/// <summary>
/// This has not been tested with all your requirements
/// </summary>
private static IContainer RemoveOldComponents(IContainer container)
{
var builder = new ContainerBuilder();
var components = container.ComponentRegistry.Registrations
.Where(cr => cr.Activator.LimitType != typeof(LifetimeScope))
.Where(cr => cr.Activator.LimitType != typeof(Func<ITest>));
foreach (var c in components)
{
builder.RegisterComponent(c);
}
foreach (var source in container.ComponentRegistry.Sources)
{
builder.RegisterSource(source);
}
return builder.Build();
}
And you can simply change your main method to the following:
static void Main(string[] args)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Test>().As<ITest>();
containerBuilder.RegisterType<Test1Factory>().As<ITestFactory>();
containerBuilder.RegisterType<TestConsumer>().AsSelf();
var container = containerBuilder.Build();
var tc1 = container.Resolve<TestConsumer>();
container = RemoveOldComponents(container);
var cbupdater = new ContainerBuilder();
cbupdater.RegisterType<Test2>().As<ITest>();
cbupdater.RegisterType<Test2Factory>().As<ITestFactory>();
cbupdater.Update(container);
var tc2 = container.Resolve<TestConsumer>();
Console.ReadLine();
}
PS: Wouldn't it be great to have a method which does the exact opposite of PreserveExistingDefaults()

can't find method java

So I run the following program and my cmd prompt gives me an error saying that the getDescriptions() method is not found in the DataElements class. I'm sure there's a simple solution but I'm just stuck. Here's the DataElements class:
import java.io.*;
public class DataElements
{
File file;
private int columns;
private int row;
private int length;
private String name;
private String type;
private int position;
private String[] descriptions;
public File getFile(){
return file;
}
public void setFile(File f){
file = f;
}
public int getColumns(){
return columns;
}
public void setColumns(int c){
columns = c;
}
public int getRow(){
return row;
}
public void setRow(int r){
row = r;
}
public int getLength(){
return length;
}
public void setLength(int l){
length = l;
}
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public String getType(){
return type;
}
public void setType(String t){
type = t;
}
public int getPosition(){
return position;
}
public void setPosition(int p){
position = p;
}
public String[] getDescriptions(){
return description;
}
public void setDescriptions(String[] d){
description = d;
}
}
And here's the main method. If you need the CMSReader class let me know, but the problem seems to be stuck in these two classes
import java.util.Scanner;
import java.io.*;
public class Project2{
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
boolean fileParsed = false;
String inFile;
String outFile;
if(args.length != 1){
System.out.println("Error. Enter one argument: the file that needs to be parsed.");
System.exit(0);
}
Scanner scan = new Scanner(new File(args[0]));
DataElements storage = new DataElements();
CMSReader reader = new CMSReader(scan,storage);
reader.scanTopData();
System.out.println("Input File - " + storage.getName());
System.out.println("Output File - ");//*************Look at this*********************
System.out.println("Number of Variables - " + storage.getColumns());
System.out.println("Number of Records - " + storage.getRow());
System.out.println("Record Length - " + storage.getLength());
System.out.println("Variable information:");
reader.scanVariableData();
String[] variableData = storage.getDescriptions();
for(int i = 0; i < variableData.length ; i++){
System.out.println(variableData[i]);
}
}
}
I appreciate any help. Like I said, I'm sure it's something dumb but I've been looking at this for too long.
The variable description is not declared in your DataElements class, which is why DataElements file cannot compile, and my guess is that you have an older compiled version (.class file) of DataElements which does not contain that method.
Recommendation:
Start working with a good IDE (IntelliJ is my personal favorite, but Eclipse and Netbeans are also good options). A good IDE, on top of all other goodies it provides, will highlight such issues in a way you won't miss.

Bluej keeps rounding my double... How do I get it to stop?

I am doing a project, I have a test file and interface and I am told to make a "line" file that successfully implements the interface and checks out successful on all the tests.
It implements and is successful on 3 of the 4 tests but fails on the last because it rounds the slope to 1.0...
Here is the code for the tester:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* The test class LineTest.
*
* #author (your name)
* #version (a version number or a date)
*/
public class LineTest
{
/**
* Default constructor for test class LineTest
*/
public LineTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
#Before
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
#After
public void tearDown()
{
}
#Test
public void testConstructor()
{
Line line1 = new Line(10, 10, 25, 25);
assertEquals(10, line1.getXOne());
assertEquals(25, line1.getXTwo());
assertEquals(10, line1.getYOne());
assertEquals(25, line1.getYTwo());
}
#Test
public void testGetSlope()
{
Line line1 = new Line(10, 10, -25, -25);
assertEquals(0.0, line1.getSlope(), 0.1);
line1.print();
}
#Test
public void testCalcSlope()
{
Line line1 = new Line(10, 10, -25, -25);
line1.calculateSlope();
assertEquals(1.0, line1.getSlope(), 0.1);
line1.print();
}
#Test
public void testSetCoords()
{
Line line1 = new Line(10, 10, -25, -35);
line1.calculateSlope();
assertEquals(1.285, line1.getSlope(), 0.003);
line1.print();
line1.setCoordinates(10, 10, 25, 35);
line1.calculateSlope();
assertEquals(1.667, line1.getSlope(), 0.001);
line1.print();
}
}
Here is the line class:
public class Line
{
private int xOne,yOne, xTwo, yTwo;
private double slope;
public Line(int x1, int y1, int x2, int y2)
{
xOne=x1;
yOne=y1;
xTwo=x2;
yTwo=y2;
}
public void setCoordinates(int x1, int y1, int x2, int y2)
{
x1=xOne;
y1=yOne;
x2=xTwo;
y2=yTwo;
}
public void calculateSlope( )
{
slope = (((yTwo)-(yOne))/((xTwo)-(xOne)));
}
public void print( )
{
System.out.println("The slope of the line created by the points ("+xOne+","+yOne+"), and ("+xTwo+","+yTwo+") is "+slope+".");
}
public int getXOne(){
return xOne;
}
public int getXTwo(){
return xTwo;
}
public int getYOne(){
return yOne;
}
public int getYTwo(){
return yTwo;
}
public double getSlope(){
return slope;
}
}
Here is the interface:
public interface TestableLine
{
public void setCoordinates(int x1, int y1, int x2, int y2);
public void calculateSlope( );
public void print( );
public int getXOne();
public int getYOne();
public int getXTwo();
public int getYTwo();
public double getSlope();
}
What is wrong here? I have tried specifying the number of decimals to round to, it just makes test 3 fail as well.
You are calculating the slope only with int values. In the method which calculates the slope you can create double variables out of the int values and use those to make the calculation.

Checkstyle, no JavaDoc for getter and setter only works for getters

I am using the Eclipse Checkstyleplugin (v5.5). I want JavaDoc comments on all public methods except getters and setters. I know there is the option "allowMissingPropertyJavadoc" which does exactly what I want. But in some cases it works and in some it does not.
This works, no JavaDoc required on gettes and setters:
public class Test {
private String name;
private int number;
public Test() {
System.out.println("Test");
}
public String getName() {
return this.name;
}
public int getNumber() {
return this.number;
}
public void setName(String name){
this.name = name;
}
public void setNumber(int number) {
this.number = number;
}
}
And this does not, JavaDoc required on setters:
public class Test2 {
private Test test;
public Test2() {
System.out.println("Test2");
this.test = new Test();
this.test.setName("thename");
this.test.setNumber(1337);
}
public String getName() {
return this.test.getName();
}
public int getNumber() {
return this.test.getNumber();
}
public void setName(String name) {
this.test.setName(name);
}
public void setNumber(int number) {
this.test.setNumber(number);
}
}
It seems as if setters without an assignment are not recognized as setters. Is how can I fix this?
That's because it requires the body to be exactly "this.name = name;"
You can see the exactly line here:
http://checkstyle.hg.sourceforge.net/hgweb/checkstyle/checkstyle/file/a485366ec8c3/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java#l819
Dumb, I know.