Hello, there is a task: to write an application that displays the current time. The task also states: "The code must contain all possible unit tests and create a readable log file.". I have never come across unit tests, tell me where to find detailed information about them, found only a couple of articles, but didn't really understand the point. Created a Maven project, in the project structure there is a folder "test". Do you need to create these tests there? And also, what is meant by "ALL POSSIBLE unit tests"? What is a log file and how is it related to unit tests? thank
- oneWhat is meant by "ALL POSSIBLE unit tests" depends on the teacher. Because generally speaking all possible unit tests are for every minute or second of the day, in general you can start reading unit tests with Wikipedia, but it’s better in the training manual on the subject, it will be better to follow what the teacher expects to see - Schullz
- Who came up with these tests? He sits watching how everyone in the sweat of his face writes tests that are 100,500 times larger than the code under test, and then also tests tests and slyly rub his little hands. - Sergey
|
1 answer
Add according to mavena:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> Then in / src / test / java you can write a unit, tests. When building a project, they are executed. Simple example with logging:
import org.apache.log4j.FileAppender; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import java.io.IOException; public class MyClassTest { private static final Logger logger = Logger.getLogger(MyClassTest.class); static { try { logger.addAppender(new FileAppender(new PatternLayout("%d %m%n"), "my.log")); } catch (IOException e) { e.printStackTrace(); } } @Rule public TestWatcher watchman = new TestWatcher() { @Override protected void succeeded(Description description) { logger.info(description.getMethodName() + " - OK"); } @Override protected void failed(Throwable e, Description description) { logger.info(description.getMethodName() + " - FAIL"); } }; @Test public void myTest1() { //test } @Test public void myTest2() { //test } @Test public void myTest3() { //test } } TestWatcher - "catches" events by test, in addition to succeeded and failed, there are other overrides.
In the tests themselves, check the code of your classes. "ALL POSSIBLE" - can be understood as the maximum coverage.
|