所以,我有一个看起来像这样的Scala类:
class TestClass {
var value: Option[Int] = None
}
Run Code Online (Sandbox Code Playgroud)
我正在解决一个问题,我有一个String值,我想在运行时使用反射将其强制转换为Option [Int].所以,在另一段代码中(对TestClass一无所知)我有一些这样的代码:
def setField[A <: Object](target: A, fieldName: String, value: String) {
val field = target.getClass.getDeclaredField(fieldName)
val coercedValue = ???; // How do I figure out that this needs to be Option[Int] ?
field.set(target, coercedValue)
}
Run Code Online (Sandbox Code Playgroud)
为此,我需要知道该字段是一个Option,并且Option的type参数是Int.
我可以选择在运行时(即使用反射)确定'value'的类型是Option [Int]?
我已经看到通过注释字段解决了类似的问题,例如@OptionType(Int.class).如果可能的话,我更喜欢不需要在反射目标上进行注释的解决方案.
我正在创建一个基于JRE 6的Java应用程序.我使用JUnit 4来生成参数化测试.我收到此错误:
The annotation @Parameterized.Parameters must define the attribute value
在包含注释的行上:
@Parameterized.Parameters
Run Code Online (Sandbox Code Playgroud)
以下是我认为与此问题相关的代码:
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import calc.CalculatorException;
import calc.ScientificCalculator;
@RunWith(Parameterized.class)
public class ScientificCalculatorTest extends BasicCalculatorTest{
/** Provides an interface to the scientific features of the calculator under test */
private ScientificCalculator sciCalc;
private double a, b;
@Before
@Override
public void setUp() throws Exception {
sciCalc = new ScientificCalculator();
//Make sure that the basic functionality of the …Run Code Online (Sandbox Code Playgroud) 我执行它时有一个存储过程我遇到了错误
将varchar值'+ @ dptId +'转换为数据类型int时转换失败
我得到DepartmentId一个像字符串,(1,3,5,77)并将其传递给我的存储过程.
create table dummy (id int,name varchar(100),DateJoining Datetime, departmentIt int)
insert into dummy values (1,'John','2012-06-01 09:55:57.257',1);
insert into dummy values(2,'Amit','2013-06-01 09:55:57.257',2);
insert into dummy values(3,'Naval','2012-05-01 09:55:57.257',3);
insert into dummy values(4,'Pamela','2012-06-01 09:55:57.257',4);
insert into dummy values(5,'Andrea','2012-09-01 09:55:57.257',3);
insert into dummy values(6,'Vicky','2012-04-01 09:55:57.257',4);
insert into dummy values(7,'Billa','2012-02-01 09:55:57.257',4);
insert into dummy values(8,'Reza','2012-04-01 09:55:57.257',3);
insert into dummy values (9,'Jacob','2011-05-01 09:55:57.257',5);
Run Code Online (Sandbox Code Playgroud)
查询我尝试过:
declare @startdate1 varchar(100) ='20120201'
declare @enddate1 varchar(100)='20130601'
declare @dptId varchar(100)='3,4'
select …Run Code Online (Sandbox Code Playgroud) 我的参数确定了参数化pytest的名称.我将使用一些随机化的参数进行这些测试.为了使我在junit中的报告名称不被搞砸,我想为每个参数化测试创建一个静态名称.
可能吗?
JUnit似乎有一个参数:更改参数化测试的名称
class TestMe:
@pytest.mark.parametrize(
("testname", "op", "value"),
[
("testA", "plus", "3"),
("testB", "minus", "1"),
]
)
def test_ops(self, testname, op, value):
Run Code Online (Sandbox Code Playgroud)
我尝试覆盖request.node.name但是我只能在测试执行期间重命名它.
我几乎肯定我需要写一个插件或夹具.您认为最好的方法是什么?
我过去见过这个讨论,比如这里.但是我想知道是否在某个地方,可能是10g或11g(我们使用的是11g),ORACLE已经为"参数化视图"引入了任何更好的支持,而不需要使用各种用户定义的类型来丢弃数据库和/或游标定义或sys_context变量.
我希望ORACLE可能会添加对"只是工作"的东西的支持,如T-SQL中的以下示例所示:
CREATE FUNCTION [dbo].[getSomeData] (@PRODID ROWID)
RETURNS TABLE AS
RETURN SELECT PRODID, A, B, C, D, E
FROM MY_TABLE
WHERE PRODID = @PRODID
Run Code Online (Sandbox Code Playgroud)
然后只需选择它:
SELECT * FROM dbo.getSomeData(23)
Run Code Online (Sandbox Code Playgroud) 我在尝试使用ParameterizedThreadStart创建线程时遇到问题.这是我现在的代码:
public class MyClass
{
public static void Foo(int x)
{
ParameterizedThreadStart p = new ParameterizedThreadStart(Bar); // no overload for Bar matches delegate ParameterizedThreadStart
Thread myThread = new Thread(p);
myThread.Start(x);
}
private static void Bar(int x)
{
// do work
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,因为我在网上找到的例子似乎做了同样的事情.
我已经使用hibernate与我的数据库交互,现在我想使我的数据库层对SQL注入安全,所以我做了一些研究,我发现我的查询应该参数化,所以这是否意味着我只是构建我的HQL查询如:
List mothers = session.createQuery(
"select mother from Cat as cat join cat.mother as mother where cat.name = ?")
.setString(0, name)
.list();
Run Code Online (Sandbox Code Playgroud)
然后它被参数化并受到SQL注入的保护,或者还有其他我需要做的事情......
另外一件事是提到 - " 总是逃避你的数据 "怎么能实现?
我目前有一个抽象BaseTest类,它包含几个泛型@Test.
public abstract class BaseTest {
private String expected;
private String actual;
public BaseTest(String expected, String actual) {
this.expected = expected;
this.actual = actual;
}
public String methodToTest(String line) {
return line.trim();
}
@Test
public void testNull() {
assertNull(methodToTest(null));
}
// more @Tests...
}
Run Code Online (Sandbox Code Playgroud)
SomeTest扩展BaseTest,我定义了各种测试用例.
@RunWith(Parallelized.class)
public class SomeTest extends BaseTest {
// @Parameters (test cases) goes here...
}
Run Code Online (Sandbox Code Playgroud)
我有许多测试可以扩展BaseTest,然后我将其Suite放入RunAllTests.
@RunWith(Suite.class)
@SuiteClasses({ SomeTest.class, AnotherTest.class, etc... })
public …Run Code Online (Sandbox Code Playgroud) 我很恼火地在参数化文档中找到"当运行参数化测试类时,为测试方法和测试数据元素的交叉产品创建实例." 这意味着构造函数对于每个单独的测试运行一次,而不是在运行所有测试之前运行.我有一个昂贵的操作(1-5秒),我把它放在构造函数中,现在操作重复了太多次,不必要地减慢了整个测试套件.只需一次操作即可设置所有测试的状态.如何使用参数化测试的一个实例运行多个测试?
我想做一个ParameterizedTest,@ValueSource我已经阅读了很多教程,包括JUnit 5 参数化测试指南:
/**
* The {@link Class} values to use as sources of arguments; must not be empty.
*
* @since 5.1
*/
Class<?>[] classes() default {};
Run Code Online (Sandbox Code Playgroud)
所以我尝试了以下方法:
@ParameterizedTest
@ValueSource(classes = {new Mandator("0052", "123456", 79)})
void testCalculateMandateI(Mandator value) {
//code using Mandator here
}
Run Code Online (Sandbox Code Playgroud)
委托人类别:
public class Mandator {
private String creditorPrefix;
private String retailerCode;
private int mandateIdSequence;
public Mandator(final String creditorPrefix, final String retailerCode, final int mandateIdSequence) {
this.creditorPrefix = creditorPrefix;
this.retailerCode …Run Code Online (Sandbox Code Playgroud) parameterized ×10
java ×4
junit ×3
unit-testing ×2
c# ×1
fixtures ×1
hibernate ×1
hql ×1
junit4 ×1
junit5 ×1
oracle ×1
oracle11g ×1
pytest ×1
python ×1
reflection ×1
rename ×1
scala ×1
scala-option ×1
types ×1
views ×1