父类 protected方法,通常应用于让子类自我重写,实现子类逻辑的差异化。
mockito
VSCode汇总
linux系列之防火墙iptable
iptables是一个命令行防火墙实用程序,它使用策略链来允许或阻止流量。 当连接尝试在您的系上建立自己时,iptables会在其列表中查找与其匹配的规则。 如果不到,则转到默认操作。
drop 掉所有类型的端口
1 | iptables -P INPUT DROP |
查看当前规则
1 | iptables -L -n |
只打开22端口的进出配置以及保存命令如下:
1 | iptables -A INPUT -p tcp --dport 26666 -j ACCEPT |
-A 参数就看成是添加一条 INPUT 的规则
-p 指定是什么协议 我们常用的tcp 协议
–dport 就是目标端口 当数据从外部进入服务器为目标端口
反之 数据从服务器出去 则为数据源端口 使用 –sport
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
删除规则
查看规则行标:
JUnit汇总
@RunWith
查阅官方API文档如下:
When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development. While it seems powerful we expect the runner API to change as we learn how people really use it. Some of the classes that are currently internal will likely be refined and become public. For example, suites in JUnit 4 are built using RunWith, and a custom runner named Suite:
1 | @RunWith(Suite.class) |
翻译:当一个类被@RunWith所注解或者继承了一个被@RunWith注解的父类,那么Junit将会调用到这个类它所引用到的测试类,而不是通过Junit构建的runner(运行器)去调用。
可以参考:
https://www.tutorialspoint.com/junit/junit_suite_test.htm
https://www.codejava.net/testing/junit-test-suite-example-how-to-create-and-run-test-suite-in-command-line-and-eclipse
结合Spring上下文做测试
在以往的Spring项目中,很多同学可能没有做到真正的单元测试,可能都是基于测试环境启动整个应用来针对功能做测试,这种方式其实效率是非常低的,利用JUnit的特性做Spring的单元测试可以如下:
1 | import org.junit.Test; |
上面的例子是指定一个Spring上下文配置文件的方式进行单元测试,这样子就加载很多本次单元测试不用到的注入,Spring官方文档给出了更加高效的方案:
Context Configuration with Annotated Classes
To load an ApplicationContext for your tests by using annotated classes (see Java-based container configuration), you can annotate your test class with @ContextConfiguration and configure the classes attribute with an array that contains references to annotated classes. The following example shows how to do so:
翻译:使用@ContextConfiguration里面的classes属性来指定加载的配置类信息
1 | @RunWith(SpringRunner.class) |