Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Execute @test n number of times incontainer 'injecting' a new value into the test instance pr @test iteration
rule - before 1028055937
rule - after 1028055937
19:31:48,196 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) rule - before 358139180
19:31:48,206 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) before
19:31:48,209 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) Test -> a
19:31:48,210 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) after
19:31:48,213 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) before
19:31:48,215 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) Test -> b
19:31:48,216 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) after
19:31:48,218 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) before
19:31:48,220 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) Test -> c
19:31:48,221 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) after
19:31:48,221 INFO [stdout] (RMI TCP Connection(3)-127.0.0.1) rule - after 358139180
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.acme.parameters;
import java.lang.reflect.Field;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class ParameterRule implements MethodRule
{
private final Object[] params;
private final String fieldName;
public ParameterRule(String fieldName, Object[]... params)
{
if(fieldName == null)
{
throw new IllegalArgumentException("fieldName must be specified");
}
if(params == null || params.length == 0)
{
throw new IllegalArgumentException("params must be specified and have more then zero length");
}
this.fieldName = fieldName;
this.params = params;
}
public ParameterRule(String fieldName, Object... params)
{
if(fieldName == null)
{
throw new IllegalArgumentException("fieldName must be specified");
}
if(params == null || params.length == 0)
{
throw new IllegalArgumentException("params must be specified and have more then zero length");
}
this.fieldName = fieldName;
this.params = params;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target)
{
return new Statement() {
@Override
public void evaluate() throws Throwable
{
System.out.println("rule - before " + target.hashCode());
if(isInContainer())
{
for(int i = 0; i < params.length; i++)
{
Object param = params[i];
Field targetField = target.getClass().getDeclaredField(fieldName);
if(!targetField.isAccessible())
{
targetField.setAccessible(true);
}
targetField.set(target, param);
base.evaluate();
}
}
else
{
base.evaluate();
}
System.out.println("rule - after " + target.hashCode());
}
};
}
private boolean isInContainer() {
Exception e = new Exception();
e.fillInStackTrace();
return e.getStackTrace()[e.getStackTrace().length-1].getClassName().equals("java.lang.Thread");
}
}
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.acme.parameters;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* ParametersTestCase
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
@RunWith(Arquillian.class)
public class ParametersTestCase
{
@Deployment
public static WebArchive deploy() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(AccountService.class, ParameterRule.class);
}
@Rule
public ParameterRule rule = new ParameterRule("dynamic", "a", "b", "c");
private String dynamic;
@Before
public void before() {
System.out.println("before");
}
@After
public void after() {
System.out.println("after");
}
@Test
public void shouldBeAbleToRun() throws Exception
{
System.out.println("Test -> " + dynamic);
}
}
@huangdihu

This comment has been minimized.

Copy link

commented Dec 3, 2014

I added 2 @rule ParameterRule in my test class, ParameterRule A has 10 set of test data, ParameterRule B has 3 set of test data.

The actual result is: each test case in my test class will run 30 times.

My expected result is: I only want test case A to run 10 times, using ParameterRule A; and test case B to run 3 times, using ParameterRule B.

Do you know how to acheive my expected result? Thanks!

@urbandroid

This comment has been minimized.

Copy link

commented Jul 16, 2017

for this piece of code it does not work as expected.

` @rule@
public ParameterRule rule = new ParameterRule("spec", new ImplementationOne(), new ImplementationTwo());

private Spec spec;

@Test
public void returnBoolean() throws Exception {
    System.out.println(spec);
}

`

it returns null for spec.

@defunkt
markdown interpreter is broke @rule is not a person but an annotation. there is no escaping this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.