我正在为以下课程编写单元测试
要测试的类:
public class RandomManager {
@Autowired
private ApplicationContext context;
@Autowired
private ClassA objectA;
public void methodToBeTested() {
objectA.methodToBeVerified(context.getBean(Random.class,"Yaswanth","Yaswanth"));
}
}
Run Code Online (Sandbox Code Playgroud)
下面是测试类:
public class RandomManagerTest {
@Mock
private ClassA objectA;
@Mock
private ApplicationContext context;
@InjectMocks
private RandomManager randomManager;
@BeforeTest
public void before() {
MockitoAnnotations.initMocks(this);
doReturn(any(Random.class)).when(context)
.getBean(any(Class.class), any(), any());
}
@Test
public void methodToBeTestedTest() {
Random randomObject = new RandomObject("Yaswanth", "Yaswanth");
randomManager.methodToBeTested();
verify(objectA).methodToBeVerified(randomObject);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试存根 applicationContext 模拟时,上面的代码在 before 方法中失败。我收到以下错误。
您不能在验证或存根之外使用参数匹配器。参数匹配器的正确使用示例:when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); 验证(模拟)。someMethod(包含(“foo”))
如果最后一个匹配器返回像 any() 这样的对象但存根方法签名需要原始参数,则此消息可能出现在 NullPointerException 之后,在这种情况下,使用原始替代方法。当(mock.get(any())); // 使用不当,会引发 …
我在一个用主键查询的表上有一个简单的查询。
dslContext.select(XXX.NAME)
.from(XXX)
.where(
XXX.ID.eq(id)
.and(XXX.LEVEL.eq(2))
.and(XXX.NAME.isNotNull())
)
.fetchOne().into(String.class);
Run Code Online (Sandbox Code Playgroud)
对于我的特定ID,查询将导致一个空集。但是jooq似乎抛出了NPE。当我进一步调查时,fetchOne()致电CursorImpl.fetchOne()。这将检查结果的大小,如果结果不为1,则返回null。我有一个链into(String.class),它被调用此空值,因此导致NPE。
我不想调用fetch()并遍历结果/获取列表的第一个元素。
是否存在另一种写查询的方式,以便org.jooq.exception.NoDataFoundException在没有数据的情况下抛出查询?
我有以下SQL查询。
select colA, sum(colB) as colB from tableA group by colA order by colB desc;
Run Code Online (Sandbox Code Playgroud)
我已经使用jpa条件构建器来动态构建查询。
List<String> selectCols = new ArrayList<>();
selectCols.add("colA");
List<String> sumColumns = new ArrayList<>();
sumColumns.add("colB");
List<String> groupByColumns = new ArrayList<>();
groupByColumns.add("colA");
List<String> orderingColumns = new ArrayList<>();
orderingColumns.add("colB");
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cQuery = builder.createQuery(Tuple.class);
Root<T> root = cQuery.from(getDomainClass());
List<Selection<?>> selections = new ArrayList<>();
for (String column : selectCols) {
selections.add(root.get(column).alias(column));
}
if (sumColumns != null) {
for (String sumColumn : sumColumns) {
selections.add(builder.sum(root.get(sumColumn)).alias(sumColumn));
}
}
cQuery.multiselect(selections); …Run Code Online (Sandbox Code Playgroud) 我有一个用例,我需要限制可以作为查询参数传递的值。
\n\n@Path("/foo")\npublic interface Foo {\n\n @GET\n @Path("/details/id/{id}")\n void getFooDetails(@PathParam("id") String id, @QueryParam("sort") String sortDirection);\n}\n\npublic class FooImpl {\n public void getFooDetails(String id, String sortDir) {\n //Implementation\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在上面的示例中,我想限制sort可以通过 API 传递到ASC, DESC.
是否有任何现有的 CXF 注释可以用来限制参数的值?我还没有找到任何解决方案,所以我尝试了以下解决方案。
\n\n我的方法:
\n\n@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})\n@Retention(RetentionPolicy.RUNTIME)\n@Inherited\npublic @interface ValueSet {\n\n String[] allowedValues();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n修改后的界面如下所示。
\n\n@Path("/foo")\npublic interface Foo {\n\n @GET\n @PathParam("/details/id/{id}")\n void getFooDetails(@PathParam("id") String id, @QueryParam("sort") @ValueSet(allowedValues = {"ASC", "DESC"}) String sortDirection);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我编写了一个 CXF 拦截器来拦截 API 调用。我使用反射来处理参数FooImpl.getFooDetails。但我面临的问题是,拦截器查看 FooImpl.getFooDetails …