From fd72129e4e723f9848461e329da8109a5b3b0e9c Mon Sep 17 00:00:00 2001 From: myh Date: Sat, 11 Nov 2023 20:08:35 +0800 Subject: [PATCH] =?UTF-8?q?Junit=E6=B5=8B=E8=AF=95=E5=8D=95=E5=85=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/springdemo/ControllerTests.java | 44 +++++++++++++++++++ .../SpringDemoApplicationTests.java | 13 ++++-- 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/example/springdemo/ControllerTests.java diff --git a/src/test/java/com/example/springdemo/ControllerTests.java b/src/test/java/com/example/springdemo/ControllerTests.java new file mode 100644 index 0000000..65a9724 --- /dev/null +++ b/src/test/java/com/example/springdemo/ControllerTests.java @@ -0,0 +1,44 @@ +package com.example.springdemo; + +import com.example.springdemo.controller.MerchantsController; +import com.example.springdemo.controller.UsersController; +import com.example.springdemo.service.MerchantsService; +import com.example.springdemo.service.UsersService; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +@WebMvcTest(UsersController.class) +class UsersControllerTests { + @Resource + private UsersController usersController; + + @MockBean + private UsersService usersService; + + @Test + public void contextLoads() { + // Test that the controller loads + assert usersController != null; + // Test that the service loads + assert usersService != null; + } +} + +@WebMvcTest(MerchantsController.class) +class MerchantsControllerTests { + @Resource + private MerchantsController merchantsController; + + @MockBean + private MerchantsService merchantsService; + + @Test + public void contextLoads() { + // Test that the controller loads + assert merchantsController != null; + // Test that the service loads + assert merchantsService != null; + } +} diff --git a/src/test/java/com/example/springdemo/SpringDemoApplicationTests.java b/src/test/java/com/example/springdemo/SpringDemoApplicationTests.java index e64d32a..d5cf3a5 100644 --- a/src/test/java/com/example/springdemo/SpringDemoApplicationTests.java +++ b/src/test/java/com/example/springdemo/SpringDemoApplicationTests.java @@ -1,13 +1,18 @@ package com.example.springdemo; import org.junit.jupiter.api.Test; +import org.springframework.boot.SpringApplication; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest -class SpringDemoApplicationTests { +public class SpringDemoApplicationTests { + @Test + void test(String[] args) { + SpringApplication.run(SpringDemoApplication.class, args); + } - @Test - void contextLoads() { - } + @Test + void contextLoads() { + } }