Using multiple #parameters for JUNIT4 - junit4

I am trying to write parameterize test in JUNIT4 and I don't know how to make multiple parameters for instance :
#parameter1
{1,2,3,4}
#test1
run test using #parameter1
#parameter2
{3,55,66,77}
#test2
run test using #parameters2
Could anyone provide me with a sample snippet, that would be greatly appreciated.
thank you.

Looks like you could take advantage of the #Theories and #TestedOn.
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.experimental.theories.suppliers.TestedOn;
import org.junit.runner.RunWith;
#RunWith(Theories.class)
public class SuppliedByTest {
#Theory
public void test1(#TestedOn(ints = { 2, 3, 4, 7, 13, 23, 42 }) int i) {
System.out.println(i);
}
#Theory
public void test2(#TestedOn(ints = { 6, 3, 4, 7, 13, 23, 42 }) int i) {
System.out.println(i);
}
}

Related

Unity3d Accessing Gameobject Function And Return String Value

I have two scripts; Localization.cs and LocalizeText.cs. I'm trying to access Localization.cs from LocalizeText.cs. When I access the GetLocalString() function, I receive an error. The error says :
"NullReferenceException: Object reference not set to an instance of an
object Localization.GetLocalString (Int32 menuId) (at
Assets/Scripts/Localization.cs:162) LocalizeText.Awake () (at
Assets/Scripts/LocalizeText.cs:46)"
I'm accessing Localization.cs from LocalizeText.cs using Getcomponent<>. Could you help me please.
Thank you for your time.
Localization.cs Script :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JsonFx.Json;
using UnityEngine.UI;
using System.IO;
using System;
public class Localization : MonoBehaviour {
public TextAsset LanguageFile;
private List<Language> _languages ;
private enum Languages
{
Turkish = 0,
English = 1,
German = 2,
French = 3,
Russian = 4,
Portuguese = 5,
Spanish = 6
}
private void Awake()
{
_languages = new List<Language>(JsonReader.Deserialize<Language[]>(LanguageFile.text));
PlayerPrefs.SetInt("Language", 0);
Debug.Log(GetLocalString(6));//it works
}
public string GetLocalString(int menuId)
{
string value = _languages[PlayerPrefs.GetInt("Language")].menu.MenuStrings[Convert.ToInt32(menuId)];
return value;
}
public class Language
{
public int LanguageId;
public Menu menu;
public Language()
{
menu = new Menu();
}
}
public class Menu
{
public List<string> MenuStrings;
public Menu()
{
MenuStrings = new List<string>();
}
}
}
LocalizaText.cs Script :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LocalizeText : MonoBehaviour {
private Text _text;
private Localization _localization;
public enum MenuItems
{
Start = 0,
Garage = 1,
WatchVideo = 2,
TiltControl = 3,
GamePadControl = 4,
Yes = 5,
Continue = 6,
CityRoad = 7,
SnowyRoad = 8,
Ridgeway = 9,
MainMenu = 10,
Exit = 11,
Speed = 12,
Acceleration = 13,
Brake = 14,
Play = 15,
Loading = 16,
LeaderBoard = 17,
Quests = 18,
Achivements = 19,
No = 20,
AreYouSure = 21
}
public MenuItems MenuItemStrings;
private void Awake()
{
_text = GetComponent<Text>();
_localization = GameObject.Find("Localization").GetComponent<Localization>();
Debug.Log(_localization);
Debug.Log(_localization.GetLocalString(System.Convert.ToInt32(MenuItemStrings)));
}
}
Output :
private List<Language> _languages; gets filled in the Awake method, and the other script tries to access it also in an Awake method. So what happens when the Script trying to access a language from the list has it's Awake method run first? Before the method to populate the list?
It gets a NullReferenceException.
So try either throwing the accessing block into the Start method, and if that doesn't work, then theres either something wrong with the json parsing, or the key just doesn't exist.
Ill assume what LOLslowSTi said its the problem, you have both methods in Awake.
You can change it to Start as he said. Or you can change the script execution order.
Documentation: http://docs.unity3d.com/Manual/class-ScriptExecution.html

Current InvocationCount in TestNG

I have a method to be tested using TestNG and I have marked it with below annotations:
#Test(invocationCount=10, threadPoolSize=5)
Now, in my test method I would like to get the current invocationCount that is being executed. Is that possible? If yes, then I would be glad to know how.
More proper example:
#Test(invocationCount=10, threadPoolSize=5)
public void testMe() {
System.out.println("Executing count: "+INVOCATIONCOUNT); //INVOCATIONCOUNT is what I am looking for
}
For reference, I am using TestNG plugin in Eclipse.
You can use TestNG dependency injection feature by adding ITestContext parameter in your test method. Please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection.
From the ITestContext parameter, you can call its getAllTestMethods() which returns array of ITestNGMethod. It should returns array of only one element, which refers to the current/actual test method. Finally, you can call getCurrentInvocationCount() of ITestNGMethod.
Your test code should be more-less like the following sample,
#Test(invocationCount=10, threadPoolSize=5)
public void testMe(ITestContext testContext) {
int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
System.out.println("Executing count: " + currentCount);
}
You can get the current invocation count as mentioned below
public class getCurrentInvocationCount {
int count;
#BeforeClass
public void initialize() {
count = 0;
}
#Test(invocationCount = 10)
public void testMe() {
count++;
System.out.println("Current Invocation count "+count)
}
}
I know this is a some kind of stupid way. However it will server your purpose. You can refer testNG source class to get actual current invocationCount
You can use something like this:
public class getCurrentInvocationCount {
AtomicInteger i = new AtomicInteger(0);
#Test(invocationCount = 10, threadPoolSize=5)
public void testMe() {
int count= i.addAndGet(1);
System.out.println("Current Invocation count "+count)
}
}
You can get by calling getCurrentInvocationCount() method of ITestNGMethod
Try to put 2 parameters in #Test method:
java.lang.reflect.Method
Use .getName() to get current method name.
ITestContext
Use .getAllTestMethods() to get all test methods. Then use forEach to extract them by ITestNGMethod and compare with .getName() in point 1.
Finally, use .getCurrentInvocationCount() to achieve this.
#Test(invocationCount=10)
public void testMe(ITestContext context, Method method) {
int invCountNumber = 0;
for(ITestNGMethod iTestMethod: context.getAllTestMethods()) {
if(iTestMethod.getMethodName().equals(method.getName())){
invCountNumber = iTestMethod.getCurrentInvocationCount();
break;
}
}
System.out.println(invCountNumber);
}
Following import:
import java.lang.reflect.Method;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
When you use invocationCount the test is run like for loop.
I found this to be the easiest way to get the count of test executions.
int count;
#Test(invocationCount = 3)
public void yourTest() {
count++;
System.out.println("test executed count is: " + count)
}

A query called many times with different parameters always returns the same first result

I'm in my first Java EE project working with jboss-4.2.3, hibernate-3.2, jpa, ejb-3
I have a session bean that looks like this:
package ...
import javax.persistence.EntityManager;
import javax.persistence.Query;
import ...
#Stateless
public class myBean implements myBeanLocal {
#PersistenceContext(unitName = "dbPU")
private EntityManager em;
#Override
public List<Item> retrieveItems (int someId) {
String nativeQuery = "SELECT * FROM myFunc(:someId)";
Query query = em.createNativeQuery(nativeQuery, Item.class);
query.setParameter("someId", someId);
List<Item> items = (List<Item>) query.getResultList();
for (Item item : items) {
System.out.println(String.format(
"[%s, %s]", item.getId(), item.getQty()
));
}
return items;
}
#Override
public void test () {
for (int i = 1; i <= 3; i++) {
retrieveItems(i);
}
}
}
So, when I call the test method, the native query executes 3 times with different parameters; the problem is after the first execution the result is the same as the first.
I checked using jdbc and the results are as expected.
// results using jdbc
[1, 5]
[2, 7]
[3, 6]
// results using NativeQuery (or NamedQuery)
[1, 5]
[1, 5]
[1, 5]
I spent the whole day trying to figure out and had no success. Any ideas?

nUnit rowtest extension skips my tests

When I use the NUnitExtension.RowTest.dll it ignores my tests in Resharper/VS2008 and Gallio Icarus. Does anyone have a config that works?
[RowTest]
[Row(5, 6, 11)]
public void Should_Do_RowTest(int a, int b, int expected)
{
Assert.AreEqual(a+b, expected);
}
Clunkier I know, but how about?
[Test, Sequential]
public void Should_Do_RowTest(
[Values(5,7)] int a,
[Values(6,9)] int b,
[Values(11,16)] int expected)
{
Assert.AreEqual(a+b, expected);
}
You won't need the Extensions DLL with this code. It will perform:
Should_Do_RowTest(5,6,11);
Should_Do_RowTest(7,9,16);

Does #Test(enabled = false) work for a class in TestNG?

From the TestNG doc I can see that (enabled = false) can be applied to a class or method. But it seems it only works when applied to a method.
Anybody seen the same, found a solution?
It seems to work for me:
#Test(enabled = false)
public class B {
public void btest1() {
System.out.println("B.btest1");
}
}
Result:
===============================================
SingleSuite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
Changing false to true:
B.btest1
===============================================
SingleSuite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Possible reason
Here is what might be tripping you (hard to tell since you didn't provide any code):
#Test(enabled = false)
public class B {
#Test
public void btest1() {
System.out.println("B.btest1");
}
}
This case will run the test because by repeating the #Test annotation on the method, you are also overriding the enabled attribute to its default value, which is true.
The solution is to reiterate enabled=false at the method level:
#Test(enabled = false)
public class B {
#Test(enabled = false)
public void btest1() {
System.out.println("B.btest1");
}
}
I'm aware it's a bit counterintuitive but it's necessary in order to be consistent in the way method annotations can override class annotations.