我是Spring的初学者,具有以下代码:
import org.springframework.stereotype.Service;
@Service("helloWorldService")
public class HelloWorldService {
private String name = "Hello World";
public String getName() {
return this.name;
}
}
/ ** * Hello类来启动此Java应用程序* /
public class Hello {
private static final Logger LOGGER = Logger.getLogger(Hello.class.getName());
@SuppressWarnings("resource")
public static void main(final String[] args) {
final ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
final HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
final String message = service.getName();
Hello.LOGGER.info(message);
}
}
我该如何编写测试?由于如果我使用this.log.getLog(),那么Junit中的实际输出将留空以进行比较。
我假设问题在于如何验证是否确实将某些值写入日志。如果您使用的是Spring Boot,则可以使用OutputCapture来验证已写入控制台的内容。Spring Boot文档中给出了一个示例。
但是,一般而言,您的单元测试应允许对照预期操作验证某些操作的实际结果。该方法getName
在HelloWorldService
对这种测试一个很好的候选人,因为你可以添加的Junit(或其他框架)断言返回的值等于你所期望的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句