【问题标题】:How to write the test suite spring mvc project如何编写测试套件spring mvc项目
【发布时间】:2016-05-16 05:15:40
【问题描述】:

我已经编写了单个测试用例,但我不知道我会不会走对路。如果您有更好的选择,请建议我。请帮助我编写测试套件。

@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath*:WEB-INF/spring/appServlet/servlet-context.xml" }) 

 public class TestHelper {

    @Mock
    private UserDaoImpl userDaoImpl;
    @InjectMocks
    private Helper helper;

    private MockMvc mockMvc;
    String msg = "Success";
    int status = 200;
    ResponseMessage message = new ResponseMessage();

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.standaloneSetup(helper).build();
    }

@Test
    public void testsavetoken() throws Exception {

        Token token = new Token();
        token.setAccessToken("ya29.WgJ2glWhvEfcP-YE0ndPlZY3NDXGGpoK5Zp4-l2qlCKkepLjQDHKO68dfopGjvhkM_QY");
        token.setAuthToken("ya29.WgJ2glWhvEfcP-YE0ndPlZY3NDXGGpoK5Zp4-l2qlCKkepLjQDHKO68dfopGjvhkM_QY");
        token.setSessionId("ya29.WgJ2glWhvEfcP-YE0ndPlZY3NDXGGpoK5Zp4-l2qlCKkepLjQDHKO68dfopGjvhkM_QY");
        token.setUuid("test1");
        when(userDaoImpl.saveToken(any(Token.class))).thenAnswer(new Answer() {
            @Override
            public Token answer(InvocationOnMock invocation) throws Throwable {
                Token token1 = (Token) invocation
                        .getArguments()[0];
                token1.setUuid("hhhhh");
                return token1;
            }
        });
        message = helper.registerToken(token);
        assertNotNull(message);
        Assert.assertEquals(status, message.getStatus());
        Assert.assertEquals(msg, message.getMessage());
    }
}

但现在我很高兴编写测试套件,所以我无法添加更多测试用例!

【问题讨论】:

    标签: java spring spring-mvc junit junit4


    【解决方案1】:

    查看这套不错的教程,其中涵盖了 Spring MVC 测试

    http://www.petrikainulainen.net/spring-mvc-test-tutorial/


    基本上,有两个级别的控制器测试:

    单元测试

    您单独测试您的控制器,而不是作为一个整体测试系统。直接关注控制器登录,而不是与其他组件的交互。

    Unit Testing of Spring MVC Controllers: Configuration

    集成测试

    您测试您的控制器与系统其他部分的交互,系统作为一个整体进行测试。即使每个组件的独立单元测试成功,集成测试也可能会发现错误。

    Integration Testing of Spring MVC Applications: Configuration

    【讨论】:

    • 感谢我的建议。我已经完成了单元测试,因为我已经对控制器、助手和 dao 层进行了单独的测试。但是当我为它编写测试套件时我很困惑。我需要如何创建测试套件以及我可以应用哪种类型的测试套件。
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多