小编Sam*_*nes的帖子

使用@InjectMocks注入String属性

我有一个@Controller带有这个构造函数的Spring MVC :

@Autowired
public AbcController(XyzService xyzService, @Value("${my.property}") String myProperty) {/*...*/}
Run Code Online (Sandbox Code Playgroud)

我想为这个Controller编写一个独立的单元测试:

@RunWith(MockitoJUnitRunner.class)
public class AbcControllerTest {

    @Mock
    private XyzService mockXyzService;

    private String myProperty = "my property value";

    @InjectMocks
    private AbcController controllerUnderTest;

    /* tests */
}
Run Code Online (Sandbox Code Playgroud)

有没有办法@InjectMocks注入我的String属性?我知道我不能模拟一个字符串,因为它是不可变的,但我可以在这里注入一个普通的字符串吗?

@InjectMocks在这种情况下,默认情况下会注入null.@Mock如果我戴上它,可以理解地抛出异常myProperty.是否有另外一个注释我错过了只是意味着"注入这个确切的对象而不是它的模拟"?

java spring unit-testing dependency-injection mockito

25
推荐指数
4
解决办法
2万
查看次数

如何在VIm中使用Eclipse格式化程序配置文件?

我的团队有一个标准的Eclipse代码格式化程序配置文件 我更喜欢和VIm一起工作.有什么办法可以转换这个文件并将其与VIm一起使用吗?

eclipse vim code-formatting editor

8
推荐指数
1
解决办法
1155
查看次数

导航不同对象的复杂树的最佳方法是什么?

例如:

class Vehicle {
    Collection<Axle> axles;
}

class Axle {
    Collection<Wheel> wheels;
}

class Wheel {
    // I think there are dually rims that take two tires -- just go with it
    Collection<Tire> tires;
}

class Tire {
    int width;
    int diameter;
}
Run Code Online (Sandbox Code Playgroud)

我有一个服务,通过它我可以获得我所知道的所有车辆对象的集合.现在说我有一个特定宽度和直径的轮胎,我想找一辆可以拿它的车.简单的方法是有一组四个嵌套循环,如下所示:

for (Vehicle vehicle : vehicles) {
    for (Axle axle : vehicle.getAxles()) {
        for (Wheel wheel : axle.getWheels()) {
            for (Tire tire : wheel.getTires()) {
                if (tire.width == targetWidth
                 && tire.diameter == targetDiameter) {
                    // do something …
Run Code Online (Sandbox Code Playgroud)

java loops design-patterns

7
推荐指数
1
解决办法
298
查看次数

在ESP8266WebServer中设置通配符

我想使用ESP8266WebServer.h将所有以“ / robot”开头的路径发送到某个处理程序。我尝试了一些变体:

server.on ( "/robot", handleDirection );
server.on ( "/robot/", handleDirection );
server.on ( "/robot/*", handleDirection );
Run Code Online (Sandbox Code Playgroud)

但在每种情况下,它仅侦听确切路径(包括该路径的*)。

该库是否仅不支持通配符路径?还是我错过了怎么做?

arduino esp8266

7
推荐指数
2
解决办法
1055
查看次数

使用 JSON 内容的 Spring MockMVC 测试从 Spring 代码中抛出 NullPointerException

我有一个小型 Spring MVC 项目。我正在为其编写 MockMvc 测试。它们中的大多数都可以工作,但是这个(我尝试使用纯 JSON 正文的第一个)给我带来了麻烦。我不断从 Spring 深处收到 NullPointerException 。我尝试通过它进行调试,但最终耗尽了附加的 Spring 源代码,而没有得到任何更接近的答案。

我的 JSON 块是从实时用户测试中捕获的,效果很好。但在测试中却抛出NPE。如果我将 JSON 块修改为格式错误(IE,在某处添加额外的逗号),那么它会按照预期抛出 400 Bad Request。删除多余的逗号,返回 NPE。使块无效(即,使域对象中标记为 @NotNull 的字段为 null)不会给出预期的 400 Bad Request。它只属于 NPE。

到目前为止,我的所有其他测试都是针对仅使用查询字符串参数的控制器,并且运行良好。另外,由于客户端的浏览器限制,我有一个必须将其 JSON 嵌入到 POST 参数中(IE,“json =”{blah:blah}”),我将其拉出并手动解析。这也很好用。

控制器:

@RestController
public class SaveController {

    @Autowired
    private MyDao myDao;

    @RequestMapping(value = "/path/to/controller", method = RequestMethod.POST)
    @PreAuthorize("hasAnyRole('myRole', 'myAdminRole')")
    public void updateThing(@Valid @RequestBody MyThing myThing) throws IOException {
        myDao.updateMyThing(myThing);
    }
}
Run Code Online (Sandbox Code Playgroud)

基础测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {TestDataAccessConfiguration.class, TestApplicationConfiguration.class})
public abstract class AbstractSpringTestCase {
    @Autowired
    protected …
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc

5
推荐指数
1
解决办法
3199
查看次数

有没有一种简单的方法可以在Spring Boot日志中启用堆栈跟踪?

我不仅要在日志中看到Exception的消息字符串,还希望有完整的堆栈跟踪记录。有没有简单的方法可以做到这一点,还是我必须深入研究Logback定制类?我宁愿在某个地方设置配置选项。

spring-boot

5
推荐指数
1
解决办法
3717
查看次数

如何使用大括号转义为Oracle

根据这个链接,我应该能够使用花括号来转义整个变量字符串.我的理解是Oracle(10g或更高版本 - 我被告知我们使用11g)应该对待这个(已消毒的)SQL查询:

SELECT * FROM customer WHERE name = 'Sam'
Run Code Online (Sandbox Code Playgroud)

就像对待这个一样:

SELECT * FROM customer WHERE name = '{Sam}'
Run Code Online (Sandbox Code Playgroud)

在尝试实际需要转义的字符串之前,我尝试过它作为一个健全性检查,但它没有用.顶部查询返回数据,但底部不返回数据.难道我做错了什么?

sql oracle

3
推荐指数
1
解决办法
3768
查看次数